BrightSide Workbench Full Report + Source Code
CrmContactsSet.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2016 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.crm.command;
20 
21 import java.util.Collection;
22 import java.util.List;
23 import org.turro.string.Strings;
24 import org.turro.action.IMyContact;
25 import org.turro.annotation.MyContact;
26 import org.turro.auth.Authentication;
27 import org.turro.contacts.util.DefaultContact;
28 import org.turro.crm.db.CrmPU;
29 import org.turro.elephant.context.IConstructor;
30 import org.turro.elephant.db.WhereClause;
31 import org.turro.i18n.I_;
32 import org.turro.jpa.Dao;
33 import org.turro.plugin.contacts.ContactSortedSet;
34 import org.turro.plugin.contacts.IContact;
35 
40 @MyContact
41 public class CrmContactsSet implements IMyContact {
42 
43  @Override
44  public Collection<IContact> getMyContacts(IConstructor constructor, String search) {
45  ContactSortedSet css = new ContactSortedSet(I_.api().used());
46  if(!Strings.isBlank(search)) {
48  if(user.isValid()) {
49  Dao dao = new CrmPU();
50  for(String c : (List<String>) dao.getResultList(createCriteria(user, search))) {
51  DefaultContact dc = new DefaultContact();
52  dc.loadById(c);
53  if(dc.isValid()) {
54  css.add(dc);
55  for(IContact cc : dc.getRelations()) {
56  css.add(cc);
57  }
58  }
59  }
60  }
61  }
62  return css;
63  }
64 
65  private WhereClause createCriteria(IContact user, String search) {
66  WhereClause wc = new WhereClause();
67  wc.addClause("select distinct c.idContact from Customer as c");
68  wc.addClause("left join c.ownedBy as o");
69  wc.addClause("where o.vendor.idContact = :idUser");
70  wc.addNamedValue("idUser", user.getId());
71  wc.addLikeFields(new String[] { "c.name" }, search);
72  return wc;
73  }
74 
75 }
List< IContact > getRelations(String id)
Collection< IContact > getMyContacts(IConstructor constructor, String search)
void addLikeFields(String[] fields, String value)
void addNamedValue(String name, Object value)
static I18nApiWrapper api()
Definition: I_.java:65