BrightSide Workbench Full Report + Source Code
AbstractElement.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2011 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 package org.turro.elephant.impl.abstracts;
19 
20 import java.io.IOException;
21 import java.util.ArrayList;
22 import java.util.Collection;
23 import java.util.Collections;
24 import java.util.Iterator;
25 import java.util.List;
26 import javax.servlet.ServletException;
27 import org.turro.string.Strings;
28 import org.jdom.Element;
29 import org.turro.elephant.context.IConstructor;
30 import org.turro.elephant.context.IElement;
31 import org.turro.elephant.impl.context.*;
32 import org.turro.elephant.impl.repository.Repository;
33 import org.turro.elephant.impl.repository.RepositoryFile;
34 import org.turro.elephant.layout.IManageable;
35 import org.turro.elephant.layout.IRenderable;
36 import org.turro.elephant.security.IDefendable;
37 import org.turro.elephant.web.ElContext;
38 import org.turro.elephant.web.SocialImageMap;
39 import org.turro.elephant.web.SocialNet;
40 import org.turro.i18n.I_;
41 
46 public abstract class AbstractElement implements IRenderable, IManageable, IDefendable, IElement {
47 
49  protected ElContext context;
51  protected Element conf = null, factory = null;
52  protected String[] types;
53  protected String title;
54  protected Collection<String> metas;
55 
57  public AbstractElement() {
59  }
60 
61  @Override
62  public String getId() {
63  return (this instanceof DefaultElement) ? null : this.getClass().getSimpleName();
64  }
65 
66  @Override
67  public String getType() {
68  return attrSupport.getAttributeValue("type", "default-element");
69  }
70 
71  @Override
72  public String getTitle() {
73  return title;
74  }
75 
76  @Override
77  public void setTitle(String title) {
78  this.title = title;
79  }
80 
81  @Override
82  public Collection<String> getMetas() {
83  return metas == null ? Collections.EMPTY_LIST : metas;
84  }
85 
86  @Override
87  public void setMetas(Collection<String> metas) {
88  this.metas = metas;
89  }
90 
91  @Override
92  public ElContext getContext() {
93  return context;
94  }
95 
96  @Override
98  return constructor;
99  }
100 
101  @Override
102  public void loadData() throws ServletException, IOException {
103  }
104 
105  @Override
106  public abstract void startConstruction() throws ServletException, IOException;
107 
108  @Override
110  this.constructor = constructor;
111  this.context = context;
112  }
113 
114  @Override
115  public String getStart() {
116  return "";
117  }
118 
119  @Override
120  public String getEnd() {
121  return "";
122  }
123 
124  @Override
125  public String getStartTitle() {
126  return "";
127  }
128 
129  @Override
130  public String getEndTitle() {
131  return "";
132  }
133 
134  @Override
135  public String getStartBody() {
136  initTypes();
137  String result = "";
138  for(int i = 0; types != null && i < types.length; i++) {
139  result += "<div class='" + types[i] + "'>";
140  }
141  return result;
142  }
143 
144  @Override
145  public String getEndBody() {
146  String result = "";
147  for(int i = 0; types != null && i < types.length; i++) {
148  result += "</div>";
149  }
150  return result;
151  }
152 
153  @Override
154  public String getStartFooter() {
155  return "";
156  }
157 
158  @Override
159  public String getEndFooter() {
160  return "";
161  }
162 
163  @Override
164  public String getStartOption() {
165  return "";
166  }
167 
168  @Override
169  public String getEndOption() {
170  return "";
171  }
172 
173  @Override
174  public boolean canDisplay() {
175  return isSelectable();
176  }
177 
178  @Override
179  public boolean isSelectable() {
180  return isInRole();
181  }
182 
183  @Override
184  public boolean isInRole() {
185  if(Strings.isBlank(context.getWebContext().getElement().getRole())) {
186  return true;
187  }
189  }
190 
191  @Override
192  public boolean canPrint() {
193  return true;
194  }
195 
196  @Override
197  public String getElementFolder() {
198  return context.getPath();
199  }
200 
201  @Override
202  public boolean isOriginalContext() {
203  return getConstructor().isOriginal();
204  //return context.getPath().equals(getConstructor().getCurrentContext().getPath());
205  }
206 
207  @Override
208  public void setRootCategories(ElContext root) {
209  // do nothing
210  }
211 
212  @Override
213  public String getTypeName() {
214  return I_.get("element." + getType());
215  }
216 
217  @Override
219  return attrSupport;
220  }
221 
222  @Override
223  public void applyChanges() {
224  }
225 
226  @Override
227  public void populateFromParent() {
228  }
229 
230  public void setDefaultMetas() {
231  ArrayList<RepositoryFile> files = new ArrayList<>();
232  //ElContext elContext = ElContextMap.getContext(context);
233  if(context != null) {
235  Repository repository = context.getRepository();
236  files.addAll(repository.getRepositoryFiles("*_social.png,*_social.jpg"));
237  files.addAll(repository.getRepositoryFiles("*.png,*.jpg"));
238  }
239  setMetas(new SocialNet(context.getWebPath(), context.getName(), null, files).getMetas());
240  }
241  }
242 
243  private void initTypes() {
244  String type = attrSupport.getAttributeValue("attrib:type", "default-body");
245  if(type != null) {
246  types = type.split(" *, *");
247  }
248  }
249 
250  // Migration purposes
251  public void setConfiguration(Element conf, Element factory) {
252  this.conf = conf;
253  this.factory = factory;
255  (conf != null ? conf.getAttributeValue("type") : null), AttributeItem.STRING_ATTR, "default-body"));
256  if(conf != null) {
257  List attribs = conf.getChildren("attrib");
258  Iterator it = attribs.iterator();
259  Element tmp;
260  while(it.hasNext()) {
261  tmp = (Element)it.next();
263  "attrib:" + tmp.getAttributeValue("name"),
264  tmp.getAttributeValue("value"),
266  null));
267  }
268  }
269  }
270 
271 }
void setConfiguration(Element conf, Element factory)
void setConfiguration(IConstructor constructor, ElContext context)
String getAttributeValue(String name, String defaultValue)
static boolean hasImage(String url)
static String get(String msg)
Definition: I_.java:41