This repository has been archived by the owner on Nov 9, 2017. It is now read-only.
/
PersonDAO.java
190 lines (170 loc) · 6.29 KB
/
PersonDAO.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
/*
* Copyright 2010, Red Hat, Inc. and individual contributors as indicated by the
* @author tags. See the copyright.txt file in the distribution for a full
* listing of individual contributors.
*
* This is free software; you can redistribute it and/or modify it under the
* terms of the GNU Lesser General Public License as published by the Free
* Software Foundation; either version 2.1 of the License, or (at your option)
* any later version.
*
* This software is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
* details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this software; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA, or see the FSF
* site: http://www.fsf.org.
*/
package org.zanata.dao;
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.lang.StringUtils;
import org.hibernate.Query;
import org.hibernate.Session;
import org.jboss.seam.ScopeType;
import org.jboss.seam.annotations.AutoCreate;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Scope;
import org.zanata.model.HLocale;
import org.zanata.model.HLocaleMember;
import org.zanata.model.HPerson;
import org.zanata.model.HProject;
@Name("personDAO")
@AutoCreate
@Scope(ScopeType.STATELESS)
public class PersonDAO extends AbstractDAOImpl<HPerson, Long>
{
public PersonDAO()
{
super(HPerson.class);
}
public PersonDAO(Session session)
{
super(HPerson.class, session);
}
public HPerson findByEmail(String email)
{
return (HPerson)getSession().byNaturalId(HPerson.class).using("email", email).load();
}
@SuppressWarnings("unchecked")
public List<HLocale> getLanguageMembershipByUsername(String userName)
{
Query query = getSession().createQuery("select m.id.supportedLanguage from HLocaleMember as m where m.id.person.account.username = :username");
query.setParameter("username", userName);
query.setCacheable(false);
query.setComment("PersonDAO.getLanguageMembershipByUsername");
List<HLocale> re = new ArrayList<HLocale>();
List<HLocale> su = query.list();
for (HLocale lan : su)
{
if (lan.isActive())
{
re.add(lan);
}
}
return re;
}
@SuppressWarnings("unchecked")
public List<HProject> getMaintainerProjectByUsername(String userName)
{
Query query = getSession().createQuery("select p.maintainerProjects from HPerson as p where p.account.username = :username");
query.setParameter("username", userName);
query.setCacheable(false);
query.setComment("PersonDAO.getMaintainerProjectByUsername");
return query.list();
}
public HPerson findByUsername(String username)
{
Query query = getSession().createQuery("from HPerson as p where p.account.username = :username");
query.setParameter("username", username);
query.setCacheable(false);
query.setComment("PersonDAO.findByUsername");
return (HPerson) query.uniqueResult();
}
public String findEmail(String username)
{
Query query = getSession().createQuery("select p.email from HPerson as p where p.account.username = :username");
query.setParameter("username", username);
query.setCacheable(false);
query.setComment("PersonDAO.findEmail");
return (String) query.uniqueResult();
}
@SuppressWarnings("unchecked")
public List<HPerson> findAllContainingName(String name)
{
if(!StringUtils.isEmpty(name))
{
Query query = getSession().createQuery("from HPerson as p where p.account.username like :name or p.name like :name");
query.setParameter("name", "%" + name + "%");
query.setCacheable(false);
query.setComment("PersonDAO.findAllContainingName");
return query.list();
}
return new ArrayList<HPerson>();
}
public int getTotalTranslator()
{
Query q = getSession().createQuery("select count(*) from HPerson");
Long totalCount = (Long) q.uniqueResult();
q.setCacheable(true).setComment("PersonDAO.getTotalTranslator");
if (totalCount == null)
return 0;
return totalCount.intValue();
}
/**
* Indicates if a Person is a member of a language team with selected roles.
* @param person
* @param language
* @param isTranslator
* @param isReviewer
* @param isCoordinator
* @return True if person is a member of the language team with selected roles.
*/
public boolean isUserInLanguageTeamWithRoles(HPerson person, HLocale language, Boolean isTranslator, Boolean isReviewer, Boolean isCoordinator)
{
StringBuilder sb = new StringBuilder();
sb.append("select count(*) from HLocaleMember where ");
sb.append("id.person = :person ");
sb.append("and id.supportedLanguage = :language ");
if(isTranslator != null)
{
sb.append("and translator = :isTranslator ");
}
if(isReviewer != null)
{
sb.append("and reviewer = :isReviewer ");
}
if(isCoordinator != null)
{
sb.append("and coordinator = :isCoordinator ");
}
Query q = getSession().createQuery(sb.toString().trim())
.setParameter("person", person)
.setParameter("language", language);
if(isTranslator != null)
{
q.setParameter("isTranslator", isTranslator.booleanValue());
}
if(isReviewer != null)
{
q.setParameter("isReviewer", isReviewer.booleanValue());
}
if(isCoordinator != null)
{
q.setParameter("isCoordinator", isCoordinator.booleanValue());
}
q.setCacheable(false).setComment("PersonDAO.isUserInLanguageTeamWithRoles");
Long totalCount = (Long) q.uniqueResult();
return totalCount > 0L;
}
public List<HLocaleMember> getAllLanguageTeamMemberships( HPerson person )
{
Query q = getSession().createQuery("from HLocaleMember where id.person = :person")
.setParameter("person", person);
q.setCacheable(false).setComment("PersonDAO.getAllLanguageTeamMemberships");
return q.list();
}
}