BrightSide Workbench Full Report + Source Code
www/AttachTree.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2013 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.attach.www;
20 
21 import java.util.HashSet;
22 import java.util.Iterator;
23 import java.util.List;
24 import org.turro.string.Strings;
25 import org.turro.action.AcceptanceSingleStatus;
26 import org.turro.action.IAcceptances;
27 import org.turro.action.Plugins;
28 import org.turro.attach.db.AttachPU;
29 import org.turro.attach.entity.Attachment;
30 import org.turro.collections.Item;
31 import org.turro.elephant.context.Application;
32 import org.turro.elephant.context.IConstructor;
33 import org.turro.elephant.db.WhereClause;
34 import org.turro.html.FontAwesome;
35 import org.turro.http.HttpUtil;
36 import org.turro.jpa.iterator.DaoHtmlTree;
37 import org.turro.marker.ElephantMarker;
38 import org.turro.path.Path;
39 
44 public class AttachTree extends DaoHtmlTree<Object, Object> {
45 
46  private boolean publicOnly, lastOnly;
47  private final HashSet<String> restrictedFolders = new HashSet<>();
48 
50  super(new AttachPU(), new ElephantMarker(constructor));
51  }
52 
53  public boolean isPublicOnly() {
54  return publicOnly;
55  }
56 
57  public void setPublicOnly(boolean publicOnly) {
58  this.publicOnly = publicOnly;
59  }
60 
61  public boolean isLastOnly() {
62  return lastOnly;
63  }
64 
65  public void setLastOnly(boolean lastOnly) {
66  this.lastOnly = lastOnly;
67  }
68 
69  public HashSet<String> getRestrictedFolders() {
70  return restrictedFolders;
71  }
72 
73  public void addRestrictedFolder(String restrictedFolder) {
74  if(!Strings.isBlank(restrictedFolder)) {
75  restrictedFolders.add(restrictedFolder);
76  }
77  }
78 
79  public void addRestrictedFolders(HashSet<String> restrictedFolders) {
80  if(restrictedFolders != null) {
81  this.restrictedFolders.addAll(restrictedFolders);
82  }
83  }
84 
85  @Override
86  protected Object getEntity(Object value) {
87  return value instanceof Long ? getDao().find(Attachment.class, (Long) value) : value;
88  }
89 
90  @Override
91  protected void renderTree(ElephantMarker marker, List<Item<Object, Object>> items) {
92  clean(items);
93  marker.put("items", items);
94  marker.process("attachment", getTreeTemplate());
95  }
96 
97  @Override
98  protected void renderItem(ElephantMarker marker, Object item) {
99  marker.put("item", item);
100  marker.process("attachment", getFullTemplate());
101  }
102 
103  @Override
104  protected String parseTree(ElephantMarker marker, List<Item<Object, Object>> items) {
105  clean(items);
106  marker.put("items", items);
107  return marker.parse("attachment", getTreeTemplate());
108  }
109 
110  @Override
111  protected String parseItem(ElephantMarker marker, Object item) {
112  marker.put("item", item);
113  return marker.parse("attachment", getFullTemplate());
114  }
115 
116  @Override
117  protected WhereClause getChildrenClause(String currPath) {
118  WhereClause wc = new WhereClause();
119 // wc.addClause("select distinct a.path from Attachment as a");
120  wc.addClause("select distinct case when locate('/', a.path, " + (currPath.length() + 2) + ") = 0 then a.path else substring(a.path, 1, locate('/', a.path, " + (currPath.length() + 2) + ") - 1) end from Attachment as a");
121 // wc.addClause("select distinct substring(a.path, 1, locate('/', a.path, " + (currPath.length() + 2) + ") - 1) from Attachment as a");
122  wc.addClause("where UCASE(a.path) like UCASE(:path)");
123  wc.addNamedValue("path", currPath + "/_%");
124 // wc.addClause("and UCASE(a.path) not like UCASE(:npath)");
125 // wc.addNamedValue("npath", currPath + "/_%/%");
126  if(publicOnly) {
127  wc.addClause("and a.publishable = true");
128  }
129  wc.addClause("order by 1");
130  return wc;
131  }
132 
133  @Override
134  protected WhereClause getLeafClause(String currPath) {
135  WhereClause wc = new WhereClause();
136  wc.addClause("select distinct a from Attachment as a");
137  if(Strings.isBlank(currPath)) {
138  wc.addClause("where a.path is null");
139  } else {
140  wc.addClause("where UCASE(a.path) = UCASE(:path)");
141  wc.addNamedValue("path", currPath);
142  }
143  if(publicOnly) {
144  wc.addClause("and a.publishable = true");
145  }
146  if(lastOnly) {
147  wc.addClause("and a.modification = (select max(a2.modification) from Attachment as a2 where a2.path = a.path and a2.fileName = a.fileName)");
148  }
149  wc.addClause("order by a.fileName");
150  return wc;
151  }
152 
153  @Override
154  protected Object getId(Object item) {
155  return item instanceof Attachment ? ((Attachment) item).getId() : new Path((String) item).getLastNode();
156  }
157 
158  @Override
159  protected Object getParentId(Object item) {
160  return null;
161  }
162 
163  public String getImage(Item item) {
164  Attachment attachment = (Attachment) item.getValue();
165  return FontAwesome.getMimeIcon(attachment.getFileExtension());
166 // if(!new File(ElephantContext.getRealPath("/_internal/system/mime/" + attachment.getFileExtension() + ".png")).exists()) {
167 // return ElephantContext.getRootWebPath() + "/_internal/system/mime/empty.png";
168 // } else {
169 // return ElephantContext.getRootWebPath() + "/_internal/system/mime/" + attachment.getFileExtension() + ".png";
170 // }
171  }
172 
173  public String getFolderImage() {
174  return FontAwesome.getMimeIcon("folder");
175 // return ElephantContext.getRootWebPath() + "/_internal/system/mime/folder.png";
176  }
177 
178  public String getShowURL(Item item) {
179  Attachment attachment = (Attachment) item.getValue();
180  return attachment.getShowURL(constructor);
181  }
182 
183  public String getROShowURL(Item item) {
184  Attachment attachment = (Attachment) item.getValue();
185  return attachment.getROShowURL(constructor);
186  }
187 
188  public String getByteString(Item item) {
189  Attachment attachment = (Attachment) item.getValue();
190  return attachment.getByteString();
191  }
192 
193  public String getDateString(Item item) {
194  Attachment attachment = (Attachment) item.getValue();
195  return attachment.getDateString(constructor);
196  }
197 
198  public String getDeleteLink(Item item) {
199  Attachment attachment = (Attachment) item.getValue();
200  return AttachAction.delAttachment(attachment, HttpUtil.getCurrentUrl(constructor.getRequest()));
201  }
202 
203  public String createPetition(AcceptanceSingleStatus status, Item item) {
204  if(AcceptanceSingleStatus.SINGLE_FORBIDDEN.equals(status)) {
206  Attachment attachment = (Attachment) item.getValue();
207  return acceptances.createPetition(AttachPU.getObjectPath(attachment), attachment.getIOwner(),
208  Application.getApplication().getConstructor().getRequest().getServletPath());
209  }
210  return "";
211  }
212 
213  private void clean(List<Item<Object, Object>> items) {
214  if(restrictedFolders.contains("*") || Application.getApplication().isInRole("attach-show:all")) return;
215  Iterator<Item<Object, Object>> it = items.iterator();
216  while(it.hasNext()) {
217  Item<Object, Object> item = it.next();
218  if(!item.isLeaf()) {
219  Path p = new Path(item.getPath());
220  String tmp = p.getLastNode();
221  if(!Strings.isBlank(tmp) && tmp.startsWith("#")) {
222  boolean allowed = false;
223  for(String restrictedFolder : restrictedFolders) {
224  if(("#" + restrictedFolder).equalsIgnoreCase(tmp)) {
225  allowed = true;
226  break;
227  }
228  }
229  if(!allowed) {
230  it.remove();
231  continue;
232  }
233  }
234  clean(item.getChildren());
235  }
236  }
237  }
238 
239 }
static< T > T loadImplementation(Class< T > jclass)
Definition: Plugins.java:57
static String getObjectPath(Object object)
Definition: AttachPU.java:57
String getDateString(IConstructor constructor)
String getShowURL(IConstructor constructor)
String getROShowURL(IConstructor constructor)
static String delAttachment(Attachment attachment, String link)
HashSet< String > getRestrictedFolders()
void setLastOnly(boolean lastOnly)
void renderTree(ElephantMarker marker, List< Item< Object, Object >> items)
void renderItem(ElephantMarker marker, Object item)
String parseTree(ElephantMarker marker, List< Item< Object, Object >> items)
Object getEntity(Object value)
AttachTree(IConstructor constructor)
WhereClause getLeafClause(String currPath)
WhereClause getChildrenClause(String currPath)
void setPublicOnly(boolean publicOnly)
void addRestrictedFolders(HashSet< String > restrictedFolders)
void addRestrictedFolder(String restrictedFolder)
String createPetition(AcceptanceSingleStatus status, Item item)
String parseItem(ElephantMarker marker, Object item)
void addNamedValue(String name, Object value)
void process(String rootTmpl, String tmpl)
String parse(String rootTmpl, String tmpl)
Object put(Object key, Object value)
String createPetition(String path, IContact contact, String link)