BrightSide Workbench Full Report + Source Code
AttachResults.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.attach.search;
19 
20 import java.util.ArrayList;
21 import org.turro.attach.db.AttachPU;
22 import org.turro.attach.entity.Attachment;
23 import org.turro.attach.version.AttachVersionMap;
24 import org.turro.attach.version.AttachVersionSet;
25 import org.turro.auth.Authentication;
26 import org.turro.elephant.context.Application;
27 import org.turro.elephant.db.SQLHelper;
28 import org.turro.elephant.db.WhereClause;
29 import org.turro.jpa.Dao;
30 import org.turro.plugin.contacts.IContact;
31 import org.zkoss.lang.Strings;
32 
37 public class AttachResults {
38 
39  private final Application app = Application.getApplication();
40 
41  private boolean ckOthers, ckNonValidated = false, ckExactPath, publicOnly = false;
42  private String attachValue = "*", attachPath;
43  private IContact byParticipant;
44 
45  public AttachResults() {
46  }
47 
48  public void markAll() {
49  attachValue = "*";
50  ckOthers = app != null && app.isInRole("attach-show:all");
51  ckNonValidated = false;
52  }
53 
54  public boolean isPublicOnly() {
55  return publicOnly;
56  }
57 
58  public void setPublicOnly(boolean publicOnly) {
59  this.publicOnly = publicOnly;
60  }
61 
62  public java.util.List<Attachment> getAttachmentList() {
63  Dao dao = new AttachPU();
64  WhereClause wc = new WhereClause();
65  wc.addClause("select distinct attachment from Attachment as attachment");
66  wc.addClause("where 1=1");
67  addCriteria(wc);
68  wc.addClause("order by attachment.fileName, attachment.modification DESC");
69  return new AttachmentList(dao.getResultList(wc), ckOthers && app!= null && app.isInRole("attach-show:all"));
70  }
71 
72  public java.util.List<Attachment> getVersionedAttachmentList() {
74  for(Attachment a : getAttachmentList()) {
75  avm.addAttachment(a);
76  }
77  ArrayList<Attachment> list = new ArrayList<>();
78  for(AttachVersionSet avs : avm.values()) {
79  list.add(avs.first());
80  }
81  return list;
82  }
83 
84  public java.util.Set<String> getPathList(String currPath) {
85  Dao dao = new AttachPU();
86  WhereClause wc = new WhereClause();
87  wc.addClause("select distinct attachment.path from Attachment as attachment");
88  wc.addClause("where 1=1");
89  addCriteria(wc);
90  wc.addClause("order by attachment.path");
91  return new PathSet(dao.getResultList(wc), currPath, ckOthers && app!= null && app.isInRole("attach-show:all"));
92  }
93 
94  public void addCriteria(WhereClause wc) {
95 
96  if(Strings.isEmpty(attachValue) || (ckOthers && app!= null && !app.isInRole("attach-show:all"))) {
97  return;
98  }
99 
100  if(byParticipant == null) {
101  byParticipant = Authentication.getIContact();
102  }
103 
104  wc = SQLHelper.getWhereClause(wc, new String[] {
105  "attachment.fileName"
106  }, (attachValue == null ? "" : attachValue.replaceAll("\\*", "%")));
107 
108  if(!(ckOthers && app!= null && app.isInRole("attach-show:all")) && byParticipant != null) {
109  wc.addClause("and (");
110 
111  wc.addClause("attachment.onlyOwner = false");
112 
113  wc.addClause("or");
114 
115  wc.addClause("(attachment.onlyOwner = true and attachment.owner = :idOwner)");
116  wc.addNamedValue("idOwner", byParticipant.getId());
117 
118  wc.addClause(")");
119  }
120 
121  if(ckNonValidated) {
122  wc.addClause("and attachment.validated = false");
123  }
124 
125  if(publicOnly) {
126  wc.addClause("and attachment.publishable = true");
127  }
128 
129  if(attachPath != null) {
130  wc.addClause("and UCASE(attachment.path) " +
131  (ckExactPath ? "=" : "like") +
132  " UCASE(:path)");
133  wc.addNamedValue("path", attachPath);
134  }
135  }
136 
137  public String getAttachPath() {
138  return attachPath;
139  }
140 
141  public void setAttachPath(String attachPath) {
142  this.attachPath = attachPath;
143  }
144 
145  public String getAttachValue() {
146  return attachValue;
147  }
148 
149  public void setAttachValue(String attachValue) {
150  this.attachValue = attachValue;
151  }
152 
154  return byParticipant;
155  }
156 
157  public void setByParticipant(IContact byParticipant) {
158  this.byParticipant = byParticipant;
159  }
160 
161  public boolean isCkExactPath() {
162  return ckExactPath;
163  }
164 
165  public void setCkExactPath(boolean ckExactPath) {
166  this.ckExactPath = ckExactPath;
167  }
168 
169  public boolean isCkNonValidated() {
170  return ckNonValidated;
171  }
172 
173  public void setCkNonValidated(boolean ckNonValidated) {
174  this.ckNonValidated = ckNonValidated;
175  }
176 
177  public boolean isCkOthers() {
178  return ckOthers;
179  }
180 
181  public void setCkOthers(boolean ckOthers) {
182  this.ckOthers = ckOthers;
183  }
184 
185  public static long getCountAttachments(String root) {
186  Dao dao = new AttachPU();
187  return (Long) dao.getSingleResult(
188  " select count(attachment) " +
189  " from Attachment as attachment" +
190  " where attachment.path like '" + root + "/%'");
191  }
192 }
java.util.Set< String > getPathList(String currPath)
void setByParticipant(IContact byParticipant)
static long getCountAttachments(String root)
java.util.List< Attachment > getVersionedAttachmentList()
void setCkExactPath(boolean ckExactPath)
void setPublicOnly(boolean publicOnly)
java.util.List< Attachment > getAttachmentList()
void setAttachValue(String attachValue)
void setAttachPath(String attachPath)
void setCkNonValidated(boolean ckNonValidated)
void addAttachment(Attachment attachment)
static WhereClause getWhereClause(String[] fields, String value)
Definition: SQLHelper.java:64
void addNamedValue(String name, Object value)
Object getSingleResult(WhereClause wc)
Definition: Dao.java:380