BrightSide Workbench Full Report + Source Code
ProfileContext.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2021 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.contacts.profile;
20 
21 import java.util.Date;
22 import org.turro.contacts.ContactType;
23 import org.turro.contacts.db.ContactsPU;
24 import org.turro.contacts.relation.FuzzyRelationTypes;
25 import static org.turro.contacts.service.ServiceContext.getServiceContextAttribute;
26 import org.turro.elephant.context.ElephantProperties;
27 import org.turro.elephant.db.WhereClause;
28 import org.turro.jpa.DaoUtil;
29 
34 public class ProfileContext {
35 
36  private static final String PROFILE_CONTEXT = "profile-context";
37 
38  public static String getAttribute(String attribute) {
39  return getAttribute(attribute, (String) null);
40  }
41 
42  public static String getAttribute(String attribute, String defaultValue) {
43  return ElephantProperties.getContextProperty(PROFILE_CONTEXT, attribute, defaultValue);
44  }
45 
46  public static Boolean getAttribute(String attribute, Boolean defaultValue) {
47  return ElephantProperties.getContextProperty(PROFILE_CONTEXT, attribute, defaultValue);
48  }
49 
50  /* Profile's context */
51 
52  public static boolean getWebOnUsers() {
53  return "true".equals(getServiceContextAttribute("web-on-users", "true"));
54  }
55 
56  public static boolean getShowCompletion() {
57  return "true".equals(getServiceContextAttribute("show-completion", "true"));
58  }
59 
60  public static boolean getRequiresCompany() {
61  return "true".equals(getServiceContextAttribute("requires-company", "true"));
62  }
63 
64  public static String getNetworkingRoles() {
65  return getServiceContextAttribute("networking-roles", "@admin:is|@patron:on|@partner:on");
66  }
67 
68  public static String getPremiumNetworkingRoles() {
69  return getServiceContextAttribute("premium-networking-roles", "@partner:on");
70  }
71 
72  /* Contacts type */
73 
74  private static Boolean companies, centers;
75  private static Long countCompanies, countWorkers, countPossibleWorkers,
76  countCenters, countStudents, countPossibleStudents;
77 
78  public static boolean hasCompanies() {
79  if(companies == null) {
80  companies = !DaoUtil.isEmpty(new ContactsPU(), "Contact", "where type = ?", ContactType.CONTACT_COMPANY);
81  }
82  return companies;
83  }
84 
85  public static boolean hasCenters() {
86  if(centers == null) {
87  centers = !DaoUtil.isEmpty(new ContactsPU(), "Contact", "where type = ?", ContactType.CONTACT_LEARNINGCENTER);
88  }
89  return centers;
90  }
91 
92  public static long countCompanies() {
93  if(countCompanies == null) {
94  countCompanies = DaoUtil.count(new ContactsPU(), "Contact", "where type = ?", ContactType.CONTACT_COMPANY);
95  }
96  return countCompanies;
97  }
98 
99  public static long countWorkers() {
100  if(countWorkers == null) {
101  WhereClause wc = new WhereClause();
102  wc.addClause("select count(br) from BusinessRelation br");
103  wc.addInRange("where", "br.startDate", "br.endDate", "date", new Date());
104  FuzzyRelationTypes.isWorker(wc, "and", "br");
105  countWorkers = DaoUtil.count(new ContactsPU(), wc);
106  }
107  return countWorkers;
108  }
109 
110  public static long countPossibleWorkers() {
111  if(countPossibleWorkers == null) {
112  WhereClause wc = new WhereClause();
113  wc.addClause("select count(conn) from Connector conn");
114  wc.addClause("left outer join BusinessRelation br on conn.contact = br.contact");
115  wc.addClause("where conn.description = 'su_professional'");
116  wc.addClause("and br is null");
117  countPossibleWorkers = DaoUtil.count(new ContactsPU(), wc);
118  }
119  return countPossibleWorkers;
120  }
121 
122  public static long countCenters() {
123  if(countCenters == null) {
124  countCenters = DaoUtil.count(new ContactsPU(), "Contact", "where type = ?", ContactType.CONTACT_LEARNINGCENTER);
125  }
126  return countCenters;
127  }
128 
129  public static long countStudents() {
130  if(countStudents == null) {
131  WhereClause wc = new WhereClause();
132  wc.addClause("select count(br) from BusinessRelation br");
133  wc.addInRange("where", "br.startDate", "br.endDate", "date", new Date());
134  FuzzyRelationTypes.isStudent(wc, "and", "br");
135  countStudents = DaoUtil.count(new ContactsPU(), wc);
136  }
137  return countStudents;
138  }
139 
140  public static long countPossibleStudents() {
141  if(countPossibleStudents == null) {
142  WhereClause wc = new WhereClause();
143  wc.addClause("select count(conn) from Connector conn");
144  wc.addClause("left outer join BusinessRelation br on conn.contact = br.contact");
145  wc.addClause("where conn.description = 'su_student'");
146  wc.addClause("and br is null");
147  countPossibleStudents = DaoUtil.count(new ContactsPU(), wc);
148  }
149  return countPossibleStudents;
150  }
151 
152 }
static Boolean getAttribute(String attribute, Boolean defaultValue)
static String getAttribute(String attribute)
static String getAttribute(String attribute, String defaultValue)
static boolean isStudent(BusinessRelation relation)
static boolean isWorker(BusinessRelation relation)
static String getContextProperty(String context, String property)
void addInRange(String operator, String startField, String endField, String attribute, Date date)
static boolean isEmpty(Dao dao, String table)
Definition: DaoUtil.java:35
static Long count(Dao dao, String table)
Definition: DaoUtil.java:50