BrightSide Workbench Full Report + Source Code
AbstractUserContent.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2020 Lluis TurrĂ³ Cutiller <http://www.turro.org/>
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU Affero General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU Affero General Public License for more details.
14  *
15  * You should have received a copy of the GNU Affero General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  */
18 
19 package org.turro.user.content;
20 
21 import java.io.Serializable;
22 import java.io.StringWriter;
23 import java.io.Writer;
24 import java.util.Set;
25 import java.util.TreeSet;
26 import org.turro.action.content.IContentIterator;
27 import org.turro.elephant.context.IConstructor;
28 import org.turro.plugin.contacts.IContact;
29 import org.turro.util.CompareUtil;
30 
35 public abstract class AbstractUserContent implements IElephantUserContent, Comparable<IElephantUserContent>, Serializable {
36 
37  private static final long serialVersionUID = 9999999L;
38 
39  private transient final IContact contact;
40  private transient final Set<String> tags = new TreeSet<>();
41  private transient String caption;
42  private transient boolean direct;
43  private transient int priority;
44  private transient String parsedContent;
45 
46  public AbstractUserContent(IContact contact) {
47  this.contact = contact;
48  priority = 3;
49  }
50 
51  @Override
52  public String getId() {
53  return this.getClass().getName();
54  }
55 
56  @Override
57  public IContact getContact() {
58  return contact;
59  }
60 
61  @Override
62  public int getPriority() {
63  return priority;
64  }
65 
66  public final void setPriority(int priority) {
67  this.priority = priority;
68  }
69 
70  @Override
71  public boolean isDirect() {
72  return direct;
73  }
74 
75  public final void setDirect(boolean direct) {
76  this.direct = direct;
77  }
78 
79  @Override
80  public String getCaption() {
81  return caption;
82  }
83 
84  protected final void setCaption(String caption) {
85  this.caption = caption;
86  }
87 
88  @Override
89  public Set<String> getTags() {
90  return tags;
91  }
92 
93  protected final void addTag(String tag) {
94  tags.add(tag);
95  }
96 
97  @Override
98  public String render(IConstructor constructor) {
99  if(parsedContent == null) {
100  IContentIterator ci = createIterator(constructor);
101  if(!showIfEmpty() && ci.isEmpty()) {
102  parsedContent = "";
103  } else {
104  ci.addAttribute("ucId", getId());
105  ci.addAttribute("ucTags", tags);
106  ci.addAttribute("ucCaption", caption);
107  ci.addAttribute("ucPriority", priority);
108  ci.addAttribute("ucRelation", direct ? 0 : 1);
109  ci.load();
110  ci.render();
111  parsedContent = ci.getWriter().toString();
112  }
113  }
114  return parsedContent;
115  }
116 
117  @Override
118  public String getContent() {
119  return parsedContent;
120  }
121 
122  public void reset() {
123  parsedContent = null;
124  }
125 
126  protected String getFullPath() {
127  // Uses ContentIterator path
128  return null;
129  }
130 
131  protected abstract boolean showIfEmpty();
132  protected abstract IContentIterator createIterator(IConstructor constructor);
133 
134  /* Writer */
135 
136  protected Writer createWriter() {
137  return new StringWriter();
138  }
139 
140  /* Comparable */
141 
142  @Override
144  int result = CompareUtil.compare(getPriority(), o.getPriority());
145  if(result == 0) {
146  result = CompareUtil.compare(!isDirect(), !o.isDirect());
147  }
148  if(result == 0) {
149  result = CompareUtil.compare(getId(), o.getId());
150  }
151  return result;
152  }
153 
154 }
abstract IContentIterator createIterator(IConstructor constructor)
String render(IConstructor constructor)
Object addAttribute(String key, Object value)