BrightSide Workbench Full Report + Source Code
ContractContentIterator.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2018 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.financials.content;
20 
21 import java.io.Writer;
22 import java.util.ArrayList;
23 import java.util.Arrays;
24 import java.util.Collection;
25 import org.turro.string.Strings;
26 import org.turro.attach.www.AttachCtrl;
27 import org.turro.elephant.context.ElephantContext;
28 import org.turro.elephant.context.IConstructor;
29 import org.turro.elephant.db.WhereClause;
30 import org.turro.elephant.impl.repository.Repository;
31 import org.turro.elephant.impl.repository.RepositoryFile;
32 import org.turro.elephant.web.SocialImageMap;
33 import org.turro.elephant.web.SocialNet;
34 import org.turro.entities.Entities;
35 import org.turro.file.util.FileAttach;
36 import org.turro.financials.command.ContractPortfolio;
37 import org.turro.financials.db.FinancialsPU;
38 import org.turro.financials.entity.Contract;
39 import org.turro.financials.entity.ContractInterventionType;
40 import org.turro.jpa.content.EntityDaoContentIterator;
41 import org.turro.jpa.search.DaoHtmlSearch;
42 import org.turro.jpa.search.DaoSearchKey;
43 import org.turro.marker.ElephantMarker;
44 import org.turro.plugin.contacts.IContact;
45 import org.turro.www.commentit.CommentItCtrl;
46 import org.turro.www.describeit.DescribeItCtrl;
47 
52 public class ContractContentIterator extends EntityDaoContentIterator<Contract, Long> {
53 
54  private boolean publicOnly, checkParticipation;
55  private int type, status;
56 
57  public ContractContentIterator(IConstructor constructor, Writer writer, IContact contact, boolean mail, String pubPath) {
58  super(new FinancialsPU(), constructor, writer, contact, mail, pubPath);
59  this.checkParticipation = true;
60  }
61 
62  public int getType() {
63  return type;
64  }
65 
66  public void setType(int type) {
67  this.type = type;
68  }
69 
70  public boolean isCheckParticipation() {
71  return checkParticipation;
72  }
73 
74  public void setCheckParticipation(boolean checkParticipation) {
75  this.checkParticipation = checkParticipation;
76  }
77 
78  public int getStatus() {
79  return status;
80  }
81 
82  public void setStatus(int status) {
83  this.status = status;
84  }
85 
86  public int count() {
87  return getTotalCount();
88  }
89 
90  public Collection<Contract> items() {
91  return getTotalItems();
92  }
93 
94  @Override
96  WhereClause wc = new WhereClause();
97  wc.addClause("select distinct ctc from Contract ctc");
98  if(checkParticipation) {
99  wc.addClause("left join ctc.participants par");
100  }
101  addCriteria(wc);
102  wc.addClause("order by ctc.name");
103  return wc;
104  }
105 
106  @Override
108  WhereClause wc = new WhereClause();
109  wc.addClause("select count(distinct ctc) from Contract ctc");
110  if(checkParticipation) {
111  wc.addClause("left join ctc.participants par");
112  }
113  addCriteria(wc);
114  return wc;
115  }
116 
117  @Override
118  protected void renderSummary(ElephantMarker marker, Contract e, int page) {
119  if(e != null) {
120  marker.put("contract", e);
121  prepareValues(e, page);
122  }
124  }
125 
126  @Override
127  protected void renderItem(ElephantMarker marker, Contract e, int page) {
128  marker.put("contract", e);
129  prepareValues(e, page);
131  }
132 
133  @Override
134  protected String entityRoot() {
135  return "contract";
136  }
137 
138  @Override
139  protected Contract entity(Long value) {
140  return dao.find(Contract.class, value);
141  }
142 
143  @Override
144  protected boolean isValid(Contract e) {
145  return super.isValid(e) && (!checkParticipation || e.getIParticipants().contains(getContact()));
146  }
147 
148  private void addCriteria(WhereClause wc) {
149  wc.addClause("where 1=1");
150  if(isSearchOption()) {
152  if(dhs != null) {
153  DaoSearchKey dsk = dhs.get("search-value");
154  if(dsk != null) {
155  dsk.applyToQuery(wc, Arrays.asList("ctc.name"), true);
156  }
157  }
158  }
159  if(status == 1) {
160  wc.addClause("and ctc.active = TRUE");
161  } else if(status == 2) {
162  wc.addClause("and ctc.active = FALSE");
163  }
164  if(checkParticipation) {
165  String idContact = getContact().getId();
166  wc.addClause("and (ctc.contractor = :contractor");
167  wc.addNamedValue("contractor", idContact);
168  for(ContractInterventionType cit : ContractInterventionType.values()) {
169  if(cit.isShowDocuments()) {
170  wc.addClause("or (par.interventionType = :cit" + cit.toString());
171  wc.addNamedValue("cit" + cit.toString(), cit);
172  wc.addClause("and par.idContact = :con" + cit.toString() + ")");
173  wc.addNamedValue("con" + cit.toString(), idContact);
174  }
175  }
176  wc.addClause(")");
177  }
178  if(type > 0) {
179  wc.addClause("and ctc.contractDefinition.id = :ctcdef");
180  wc.addNamedValue("ctcdef", type);
181  }
182  }
183 
184  private void prepareValues(Contract contract, int page) {
185  prepareControls(contract, page);
186 
187  marker.put("contractClass", "contract" +
188  (contract.isActive() ? " active" : "") +
189  (contract.isStock() ? " store" : ""));
190  marker.put("portfolio", new ContractPortfolio(contract));
191  }
192 
193  @Override
194  protected String title(Contract e) {
195  return e.getFullDescription();
196  }
197 
198  @Override
199  protected Collection<String> metas(Contract e) {
200  ArrayList<RepositoryFile> files = new ArrayList<>();
201  String path = getItemLink(e);
202  if(!SocialImageMap.hasImage(path)) {
205  files.addAll(repository.getRepositoryFiles("*_social.png,*_social.jpg"));
206  files.addAll(repository.getRepositoryFiles("*.png,*.jpg"));
207  }
208  SocialNet sn = new SocialNet(path, e.getName(), e.getFullDescription(), files);
209  return sn.getMetas();
210  }
211 
212  @Override
213  protected String getTemplateRoot() {
214  return isMail() ? "content/newsletter/sections/contracts" : "contract";
215  }
216 
217  @Override
218  protected Object doVotesCtrl(Contract e) {
219  throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
220  }
221 
222  @Override
223  protected Object doInterestCtrl(Contract e) {
224  throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
225  }
226 
227  @Override
228  protected Object doCommentsCtrl(Contract e) {
231  }
232 
233  @Override
234  protected Object doAttachmentsCtrl(Contract e) {
237  }
238 
239  @Override
240  protected Object doFilesCtrl(Contract e) {
243  }
244 
245  @Override
246  protected Object doDescriptionsCtrl(Contract e) {
249  }
250 
251  @Override
252  protected Object doPollsCtrl(Contract e) {
253  throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
254  }
255 
256  @Override
257  protected String getItemLink(Contract e) {
258  return doItemLink(e, e.getId(), true);
259  }
260 
261  @Override
262  protected String getReadAllLink() {
263  String path = getContextPath();
264  if(Strings.isBlank(path)) {
265  return getRestrictedLink();
266  }
267  return path;
268  }
269 
270  @Override
271  protected String getRestrictedLink() {
272  return "/user/contracts";
273  }
274 
275  /* Porfolio */
276 
277  public Object getPortfolioCtrl(Contract e) {
278  return new ContractPortfolio(e);
279  }
280 
281 }
static String getContextVariable(IConstructor constructor)
void addNamedValue(String name, Object value)
static boolean hasImage(String url)
static IElephantEntity getController(String path)
Definition: Entities.java:78
Repository getPublishableRepository(IConstructor constructor)
Definition: FileAttach.java:47
ContractContentIterator(IConstructor constructor, Writer writer, IContact contact, boolean mail, String pubPath)
void renderItem(ElephantMarker marker, Contract e, int page)
void renderSummary(ElephantMarker marker, Contract e, int page)
static String getObjectPath(Object object)
Collection< IContact > getIParticipants()
Definition: Contract.java:462
String doItemLink(E entity, ID id, boolean obfuscated)
static DaoHtmlSearch getInstance(IConstructor constructor, String context)
boolean applyToQuery(WhereClause wc, List< String > fields, boolean withSynonyms)
void process(String rootTmpl, String tmpl)
Object put(Object key, Object value)
Object configureCtrl(Object ctrl, IContact contact)