BrightSide Workbench Full Report + Source Code
ContractIterator.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.financials.contract.www;
20 
21 import java.util.ArrayList;
22 import java.util.Collection;
23 import org.turro.string.Strings;
24 import org.turro.attach.www.AttachCtrl;
25 import org.turro.auth.Authentication;
26 import org.turro.elephant.context.IConstructor;
27 import org.turro.elephant.db.WhereClause;
28 import org.turro.elephant.impl.repository.Repository;
29 import org.turro.elephant.impl.repository.RepositoryFile;
30 import org.turro.elephant.web.SocialImageMap;
31 import org.turro.elephant.web.SocialNet;
32 import org.turro.file.util.FileAttach;
33 import org.turro.financials.command.ContractPortfolio;
34 import org.turro.financials.db.FinancialsPU;
35 import org.turro.financials.entity.Contract;
36 import org.turro.financials.entity.ContractInterventionType;
37 import org.turro.jpa.iterator.DaoHtmlIterator;
38 import org.turro.mail.recipients.MailContact;
39 import org.turro.marker.ElephantMarker;
40 
45 @Deprecated
46 public class ContractIterator extends DaoHtmlIterator<Contract, Long> {
47 
48  private String pubPath, actualParameters;
49  private boolean publicOnly, checkParticipation;
50  private int type;
51 
52  public ContractIterator(IConstructor constructor, String pubPath) {
53  super(new FinancialsPU(), new ElephantMarker(constructor));
54  this.pubPath = pubPath;
55  this.checkParticipation = true;
56  }
57 
58  public boolean isPublicOnly() {
59  return publicOnly;
60  }
61 
62  public void setPublicOnly(boolean publicOnly) {
63  this.publicOnly = publicOnly;
64  }
65 
66  public int getType() {
67  return type;
68  }
69 
70  public void setType(int type) {
71  this.type = type;
72  }
73 
74  public boolean isCheckParticipation() {
75  return checkParticipation;
76  }
77 
78  public void setCheckParticipation(boolean checkParticipation) {
79  this.checkParticipation = checkParticipation;
80  }
81 
82  public int count() {
83  return getTotalCount();
84  }
85 
86  public Collection<Contract> items() {
87  return getTotalItems();
88  }
89 
90  @Override
92  WhereClause wc = new WhereClause();
93  wc.addClause("select distinct ctc from Contract ctc");
94  if(checkParticipation) {
95  wc.addClause("left join ctc.participants par");
96  }
97  addCriteria(wc);
98  wc.addClause("order by ctc.name");
99  return wc;
100  }
101 
102  @Override
104  WhereClause wc = new WhereClause();
105  wc.addClause("select count(distinct ctc) from Contract ctc");
106  if(checkParticipation) {
107  wc.addClause("left join ctc.participants par");
108  }
109  addCriteria(wc);
110  return wc;
111  }
112 
113  public String getActualParameters() {
114  return Strings.isBlank(actualParameters) ? "?a=b" : actualParameters;
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, false);
122  }
123  marker.process("contract", getSummaryTemplate());
124  actualParameters = getPageLink(page);
125  }
126 
127  @Override
128  protected void renderItem(ElephantMarker marker, Contract e, int page) {
129  marker.put("contract", e);
130  prepareValues(e, page, false);
131  marker.process("contract", getFullTemplate());
132  actualParameters = getPageLink(page);
133  }
134 
135  @Override
136  protected String entityRoot() {
137  return "contract";
138  }
139 
140  @Override
141  protected Contract entity(Long value) {
142  return dao.find(Contract.class, value);
143  }
144 
145  @Override
146  protected boolean isValid(Contract e) {
147  return super.isValid(e) && (!checkParticipation || e.getIParticipants().contains(Authentication.getIContact()));
148  }
149 
150  private void addCriteria(WhereClause wc) {
151  wc.addClause("where ctc.active = TRUE");
152  if(checkParticipation) {
153  String idContact = Authentication.getIContact().getId();
154  wc.addClause("and (ctc.contractor = :contractor");
155  wc.addNamedValue("contractor", idContact);
157  if(cit.isShowDocuments()) {
158  wc.addClause("or (par.interventionType = :cit" + cit.toString());
159  wc.addNamedValue("cit" + cit.toString(), cit);
160  wc.addClause("and par.idContact = :con" + cit.toString() + ")");
161  wc.addNamedValue("con" + cit.toString(), idContact);
162  }
163  }
164  wc.addClause(")");
165  }
166  if(type > 0) {
167  wc.addClause("and ctc.contractDefinition.id = :ctcdef");
168  wc.addNamedValue("ctcdef", type);
169  }
170  }
171 
172  private void prepareValues(Contract contract, int page, boolean asMail) {
173 
174  if(asMail && pubPath != null) {
175  marker.put("name", MailContact.createLink(contract.getFullDescription(), getItemLink(contract, page), false));
176  } else {
177  marker.put("name", contract.getFullDescription());
178  }
179  if(pubPath != null) {
180  marker.put("path", getItemLink(contract, page));
181  marker.put("all", getPageLink(page));
182  }
183  if(!Strings.isBlank(pubPath)) {
184  marker.put("readall", getItemLink(contract, page));
185  }
186  marker.put("contractClass", "contract" +
187  (contract.isActive() ? " active" : "") +
188  (contract.isStock() ? " store" : ""));
189  marker.put("portfolio", new ContractPortfolio(contract));
190 
191  AttachCtrl ac = new AttachCtrl(constructor);
192  ac.setEntityPath(FinancialsPU.getObjectPath(contract));
193  ac.setPublicOnly(publicOnly);
194  marker.put("ac", ac);
195 
196  }
197 
198  private String getItemLink(Contract issue, int page) {
199  return pubPath + "?item=" + issue.getId() + "&page=" + page;
200  }
201 
202  private String getPageLink(int page) {
203  return pubPath + "?page=" + page;
204  }
205 
206  @Override
207  protected String title(Contract e) {
208  return e.getFullDescription();
209  }
210 
211  @Override
212  protected Collection<String> metas(Contract e) {
213  ArrayList<RepositoryFile> files = new ArrayList<>();
214  String path = getItemLink(e, 0);
215  if(!SocialImageMap.hasImage(path)) {
218  files.addAll(repository.getRepositoryFiles("*_social.png,*_social.jpg"));
219  files.addAll(repository.getRepositoryFiles("*.png,*.jpg"));
220  }
221  SocialNet sn = new SocialNet(path, e.getName(), e.getFullDescription(), files);
222  return sn.getMetas();
223  }
224 
225 }
void addNamedValue(String name, Object value)
static boolean hasImage(String url)
Repository getPublishableRepository(IConstructor constructor)
Definition: FileAttach.java:47
void renderSummary(ElephantMarker marker, Contract e, int page)
void setCheckParticipation(boolean checkParticipation)
ContractIterator(IConstructor constructor, String pubPath)
void renderItem(ElephantMarker marker, Contract e, int page)
static String getObjectPath(Object object)
Collection< IContact > getIParticipants()
Definition: Contract.java:462
void process(String rootTmpl, String tmpl)
Object put(Object key, Object value)