BrightSide Workbench Full Report + Source Code
WebItem.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2022 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.elephant.entities.web;
20 
21 import java.io.Serializable;
22 import java.util.HashSet;
23 import java.util.Objects;
24 import java.util.Set;
25 import java.util.TreeSet;
26 import java.util.stream.Collectors;
27 import javax.persistence.AttributeOverride;
28 import javax.persistence.AttributeOverrides;
29 import javax.persistence.Column;
30 import javax.persistence.ElementCollection;
31 import javax.persistence.Embedded;
32 import javax.persistence.Entity;
33 import javax.persistence.FetchType;
34 import javax.persistence.Id;
35 import javax.persistence.ManyToMany;
36 import javax.persistence.OrderBy;
37 import org.turro.elephant.context.Application;
38 import org.turro.elephant.context.IConstructor;
39 import org.turro.elephant.web.actions.WebActions;
40 import org.turro.elephant.web.actions.WebLink;
41 import org.turro.html.HtmlContent;
42 import org.turro.icon.Icons;
43 import org.turro.jpa.embeddables.Wiki;
44 import org.turro.jpa.entity.IDaoEntity;
45 import org.turro.string.Phrases;
46 import org.turro.string.Strings;
47 
52 @Entity
53 public class WebItem implements IDaoEntity, Serializable {
54 
55  @Id private String webTag;
56 
57  private WebItemType type;
58 
59  private String caption, image;
60  private int ordering;
61 
62  @Embedded
63  @AttributeOverrides({
64  @AttributeOverride( name = "text", column = @Column(name = "summary")),
65  @AttributeOverride( name = "wiki", column = @Column(name = "wsummary"))
66  })
67  private Wiki summary;
68 
69  @Embedded
70  @AttributeOverrides({
71  @AttributeOverride( name = "text", column = @Column(name = "description")),
72  @AttributeOverride( name = "wiki", column = @Column(name = "wdescription"))
73  })
74  private Wiki description;
75 
76  @ManyToMany(fetch = FetchType.EAGER)
77  @OrderBy("ordering")
78  private Set<WebGoal> goals = new TreeSet<>();
79 
80  @ManyToMany(fetch = FetchType.EAGER)
81  @OrderBy("ordering")
82  private Set<WebItem> nexts = new TreeSet<>();
83 
84  @ElementCollection(fetch=FetchType.EAGER)
85  private Set<String> externals = new HashSet<>();
86 
87  public String getWebTag() {
88  return webTag;
89  }
90 
91  public void setWebTag(String webTag) {
92  this.webTag = webTag;
93  }
94 
95  public WebItemType getType() {
96  return type;
97  }
98 
99  public void setType(WebItemType type) {
100  this.type = type;
101  }
102 
103  public String getCaption() {
104  return caption;
105  }
106 
107  public void setCaption(String caption) {
108  this.caption = caption;
109  }
110 
111  public String getImage() {
112  return image;
113  }
114 
115  public void setImage(String image) {
116  this.image = image;
117  }
118 
119  public int getOrdering() {
120  return ordering;
121  }
122 
123  public void setOrdering(int ordering) {
124  this.ordering = ordering;
125  }
126 
127  public Wiki getSummary() {
128  if(summary == null) summary = new Wiki();
129  return summary;
130  }
131 
132  public void setSummary(Wiki summary) {
133  this.summary = summary;
134  }
135 
136  public Wiki getDescription() {
137  if(description == null) description = new Wiki();
138  return description;
139  }
140 
141  public void setDescription(Wiki description) {
142  this.description = description;
143  }
144 
145  public Set<WebGoal> getGoals() {
146  return goals;
147  }
148 
149  public void setGoals(Set<WebGoal> goals) {
150  this.goals = goals;
151  }
152 
153  public Set<WebItem> getNexts() {
154  return nexts;
155  }
156 
157  public void setNexts(Set<WebItem> nexts) {
158  this.nexts = nexts;
159  }
160 
161  public Set<String> getExternals() {
162  return externals;
163  }
164 
165  public void setExternals(Set<String> externals) {
166  this.externals = externals;
167  }
168 
169  /* Utils */
170 
171  public String getPlainCompendium() {
172  return summary == null ? "" : HtmlContent.plain(summary.getText(), 100);
173  }
174 
175  public String getCompendium() {
176  return summary == null ? "" : summary.getText();
177  }
178 
179  public String getContent() {
180  return description == null ? "" : description.getText();
181  }
182 
183  public boolean isIcon() {
184  return !Strings.isBlank(image) && !image.endsWith(".png");
185  }
186 
187  public boolean isValid() {
188  return webTag.matches("[A-Za-z0-9_-]+");
189  }
190 
191  public Icons getIcons() {
192  return Icons.from(image);
193  }
194 
195  public String getItemLabel() {
196  return Phrases.start(caption).add(webTag, "()").add(type.toString()).toString();
197  }
198 
199  public void compose() {
201  if(constructor != null) {
202  getSummary().compose(constructor);
203  getDescription().compose(constructor);
204  }
205  }
206 
207  public Set<WebLink> getExternalLinks() {
208  return externals.stream().map(ext -> WebActions.of(ext).link())
209  .filter(wl -> !wl.isEmpty()).collect(Collectors.toSet());
210  }
211 
212  @Override
213  public int hashCode() {
214  int hash = 7;
215  hash = 71 * hash + Objects.hashCode(this.webTag);
216  return hash;
217  }
218 
219  @Override
220  public boolean equals(Object obj) {
221  if (this == obj) {
222  return true;
223  }
224  if (obj == null) {
225  return false;
226  }
227  if (getClass() != obj.getClass()) {
228  return false;
229  }
230  final WebItem other = (WebItem) obj;
231  return Objects.equals(this.webTag, other.webTag);
232  }
233 
234  /* IDaoEntity */
235 
236  @Override
237  public Object entityId() {
238  return webTag;
239  }
240 
241  @Override
242  public boolean isEmpty() {
243  return Strings.isBlank(webTag) || Strings.isBlank(caption);
244  }
245 
246 }
void setNexts(Set< WebItem > nexts)
Definition: WebItem.java:157
void setGoals(Set< WebGoal > goals)
Definition: WebItem.java:149
void setDescription(Wiki description)
Definition: WebItem.java:141
void setType(WebItemType type)
Definition: WebItem.java:99
void setExternals(Set< String > externals)
Definition: WebItem.java:165
static WebActions of(String action)
void compose(IConstructor constructor)
Definition: Wiki.java:66