1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portal.service.persistence;
24  
25  import com.liferay.portal.SystemException;
26  import com.liferay.portal.kernel.dao.orm.QueryPos;
27  import com.liferay.portal.kernel.dao.orm.QueryUtil;
28  import com.liferay.portal.kernel.dao.orm.SQLQuery;
29  import com.liferay.portal.kernel.dao.orm.Session;
30  import com.liferay.portal.kernel.dao.orm.Type;
31  import com.liferay.portal.kernel.util.OrderByComparator;
32  import com.liferay.portal.kernel.util.StringPool;
33  import com.liferay.portal.kernel.util.StringUtil;
34  import com.liferay.portal.kernel.util.Validator;
35  import com.liferay.portal.model.Organization;
36  import com.liferay.portal.service.persistence.impl.BasePersistenceImpl;
37  import com.liferay.util.dao.orm.CustomSQLUtil;
38  
39  import java.util.ArrayList;
40  import java.util.Iterator;
41  import java.util.LinkedHashMap;
42  import java.util.List;
43  import java.util.Map;
44  
45  /**
46   * <a href="OrganizationFinderImpl.java.html"><b><i>View Source</i></b></a>
47   *
48   * @author Brian Wing Shun Chan
49   * @author Jorge Ferrer
50   *
51   */
52  public class OrganizationFinderImpl
53      extends BasePersistenceImpl implements OrganizationFinder {
54  
55      public static String COUNT_BY_C_PO_N_S_C_Z_R_C =
56          OrganizationFinder.class.getName() + ".countByC_PO_N_S_C_Z_R_C";
57  
58      public static String COUNT_BY_C_PO_N_L_S_C_Z_R_C =
59          OrganizationFinder.class.getName() + ".countByC_PO_N_L_S_C_Z_R_C";
60  
61      public static String FIND_BY_C_PO_N_S_C_Z_R_C =
62          OrganizationFinder.class.getName() + ".findByC_PO_N_S_C_Z_R_C";
63  
64      public static String FIND_BY_C_PO_N_L_S_C_Z_R_C =
65          OrganizationFinder.class.getName() + ".findByC_PO_N_L_S_C_Z_R_C";
66  
67      public static String JOIN_BY_GROUPS_PERMISSIONS =
68          OrganizationFinder.class.getName() + ".joinByGroupsPermissions";
69  
70      public static String JOIN_BY_ORGANIZATIONS_GROUPS =
71          OrganizationFinder.class.getName() + ".joinByOrganizationsGroups";
72  
73      public static String JOIN_BY_ORGANIZATIONS_PASSWORD_POLICIES =
74          OrganizationFinder.class.getName() +
75              ".joinByOrganizationsPasswordPolicies";
76  
77      public static String JOIN_BY_ORGANIZATIONS_ROLES =
78          OrganizationFinder.class.getName() + ".joinByOrganizationsRoles";
79  
80      public static String JOIN_BY_ORGANIZATIONS_USERS =
81          OrganizationFinder.class.getName() + ".joinByOrganizationsUsers";
82  
83      public static String JOIN_BY_ORG_GROUP_PERMISSION =
84          OrganizationFinder.class.getName() + ".joinByOrgGroupPermission";
85  
86      public int countByKeywords(
87              long companyId, long parentOrganizationId,
88              String parentOrganizationIdComparator, String keywords,
89              String type, Long regionId, Long countryId,
90              LinkedHashMap<String, Object> params)
91          throws SystemException {
92  
93          String[] names = null;
94          String[] streets = null;
95          String[] cities = null;
96          String[] zips = null;
97          boolean andOperator = false;
98  
99          if (Validator.isNotNull(keywords)) {
100             names = CustomSQLUtil.keywords(keywords);
101             streets = CustomSQLUtil.keywords(keywords);
102             cities = CustomSQLUtil.keywords(keywords);
103             zips = CustomSQLUtil.keywords(keywords);
104         }
105         else {
106             andOperator = true;
107         }
108 
109         return countByC_PO_N_T_S_C_Z_R_C(
110             companyId, parentOrganizationId, parentOrganizationIdComparator,
111             names, type, streets, cities, zips, regionId, countryId, params,
112             andOperator);
113     }
114 
115     public int countByC_PO_N_T_S_C_Z_R_C(
116             long companyId, long parentOrganizationId,
117             String parentOrganizationIdComparator, String name, String type,
118             String street, String city, String zip, Long regionId,
119             Long countryId, LinkedHashMap<String, Object> params,
120             boolean andOperator)
121         throws SystemException {
122 
123         return countByC_PO_N_T_S_C_Z_R_C(
124             companyId, parentOrganizationId, parentOrganizationIdComparator,
125             new String[] {name}, type, new String[] {street},
126             new String[] {city}, new String[] {zip}, regionId, countryId,
127             params, andOperator);
128     }
129 
130     public int countByC_PO_N_T_S_C_Z_R_C(
131             long companyId, long parentOrganizationId,
132             String parentOrganizationIdComparator, String[] names,
133             String type, String[] streets, String[] cities, String[] zips,
134             Long regionId, Long countryId, LinkedHashMap<String, Object> params,
135             boolean andOperator)
136         throws SystemException {
137 
138         names = CustomSQLUtil.keywords(names);
139         streets = CustomSQLUtil.keywords(streets);
140         cities = CustomSQLUtil.keywords(cities);
141         zips = CustomSQLUtil.keywords(zips);
142 
143         if (params != null) {
144             Long resourceId = (Long)params.get("permissionsResourceId");
145             Long groupId = (Long)params.get("permissionsGroupId");
146 
147             if (Validator.isNotNull(groupId) &&
148                     Validator.isNotNull(resourceId)) {
149 
150                 return countByPermissions(
151                     companyId, parentOrganizationId,
152                     parentOrganizationIdComparator, names, type, streets,
153                     cities, zips, regionId, countryId, resourceId.longValue(),
154                     groupId.longValue(), andOperator);
155             }
156         }
157 
158         Session session = null;
159 
160         try {
161             session = openSession();
162 
163             String sql = null;
164 
165             if (Validator.isNotNull(type)) {
166                 sql = CustomSQLUtil.get(COUNT_BY_C_PO_N_L_S_C_Z_R_C);
167             }
168             else {
169                 sql = CustomSQLUtil.get(COUNT_BY_C_PO_N_S_C_Z_R_C);
170             }
171 
172             sql = CustomSQLUtil.replaceKeywords(
173                 sql, "lower(Organization_.name)", StringPool.LIKE, false,
174                 names);
175             sql = CustomSQLUtil.replaceKeywords(
176                 sql, "lower(Address.street1)", StringPool.LIKE, true,
177                 streets);
178             sql = CustomSQLUtil.replaceKeywords(
179                 sql, "lower(Address.street2)", StringPool.LIKE, true,
180                 streets);
181             sql = CustomSQLUtil.replaceKeywords(
182                 sql, "lower(Address.street3)", StringPool.LIKE, true,
183                 streets);
184             sql = CustomSQLUtil.replaceKeywords(
185                 sql, "lower(Address.city)", StringPool.LIKE, false,
186                 cities);
187             sql = CustomSQLUtil.replaceKeywords(
188                 sql, "lower(Address.zip)", StringPool.LIKE, true,
189                 zips);
190 
191             if (regionId == null) {
192                 sql = StringUtil.replace(sql, REGION_ID_SQL, StringPool.BLANK);
193             }
194 
195             if (countryId == null) {
196                 sql = StringUtil.replace(sql, COUNTRY_ID_SQL, StringPool.BLANK);
197             }
198 
199             sql = StringUtil.replace(sql, "[$JOIN$]", getJoin(params));
200             sql = StringUtil.replace(sql, "[$WHERE$]", getWhere(params));
201             sql = StringUtil.replace(
202                 sql, "[$PARENT_ORGANIZATION_ID_COMPARATOR$]",
203                 parentOrganizationIdComparator);
204             sql = CustomSQLUtil.replaceAndOperator(sql, andOperator);
205 
206             SQLQuery q = session.createSQLQuery(sql);
207 
208             q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
209 
210             QueryPos qPos = QueryPos.getInstance(q);
211 
212             setJoin(qPos, params);
213             qPos.add(companyId);
214             qPos.add(parentOrganizationId);
215 
216             if (Validator.isNotNull(type)) {
217                 qPos.add(type);
218             }
219 
220             qPos.add(names, 2);
221             qPos.add(streets, 6);
222 
223             if (regionId != null) {
224                 qPos.add(regionId);
225                 qPos.add(regionId);
226             }
227 
228             if (countryId != null) {
229                 qPos.add(countryId);
230                 qPos.add(countryId);
231             }
232 
233             qPos.add(cities, 2);
234             qPos.add(zips, 2);
235 
236             Iterator<Long> itr = q.list().iterator();
237 
238             if (itr.hasNext()) {
239                 Long count = itr.next();
240 
241                 if (count != null) {
242                     return count.intValue();
243                 }
244             }
245 
246             return 0;
247         }
248         catch (Exception e) {
249             throw new SystemException(e);
250         }
251         finally {
252             closeSession(session);
253         }
254     }
255 
256     public List<Organization> findByKeywords(
257             long companyId, long parentOrganizationId,
258             String parentOrganizationIdComparator, String keywords,
259             String type, Long regionId, Long countryId,
260             LinkedHashMap<String, Object> params, int start, int end,
261             OrderByComparator obc)
262         throws SystemException {
263 
264         String[] names = null;
265         String[] streets = null;
266         String[] cities = null;
267         String[] zips = null;
268         boolean andOperator = false;
269 
270         if (Validator.isNotNull(keywords)) {
271             names = CustomSQLUtil.keywords(keywords);
272             streets = CustomSQLUtil.keywords(keywords);
273             cities = CustomSQLUtil.keywords(keywords);
274             zips = CustomSQLUtil.keywords(keywords);
275         }
276         else {
277             andOperator = true;
278         }
279 
280         return findByC_PO_N_T_S_C_Z_R_C(
281             companyId, parentOrganizationId, parentOrganizationIdComparator,
282             names, type, streets, cities, zips, regionId, countryId, params,
283             andOperator, start, end, obc);
284     }
285 
286     public List<Organization> findByC_PO_N_T_S_C_Z_R_C(
287             long companyId, long parentOrganizationId,
288             String parentOrganizationIdComparator, String name, String type,
289             String street, String city, String zip, Long regionId,
290             Long countryId, LinkedHashMap<String, Object> params,
291             boolean andOperator, int start, int end, OrderByComparator obc)
292         throws SystemException {
293 
294         return findByC_PO_N_T_S_C_Z_R_C(
295             companyId, parentOrganizationId, parentOrganizationIdComparator,
296             new String[] {name}, type, new String[] {street},
297             new String[] {city}, new String[] {zip}, regionId, countryId,
298             params, andOperator, start, end, obc);
299     }
300 
301     public List<Organization> findByC_PO_N_T_S_C_Z_R_C(
302             long companyId, long parentOrganizationId,
303             String parentOrganizationIdComparator, String[] names,
304             String type, String[] streets, String[] cities, String[] zips,
305             Long regionId, Long countryId, LinkedHashMap<String, Object> params,
306             boolean andOperator, int start, int end, OrderByComparator obc)
307         throws SystemException {
308 
309         names = CustomSQLUtil.keywords(names);
310         streets = CustomSQLUtil.keywords(streets);
311         cities = CustomSQLUtil.keywords(cities);
312         zips = CustomSQLUtil.keywords(zips);
313 
314         if (params != null) {
315             Long resourceId = (Long)params.get("permissionsResourceId");
316             Long groupId = (Long)params.get("permissionsGroupId");
317 
318             if (Validator.isNotNull(groupId) &&
319                     Validator.isNotNull(resourceId)) {
320 
321                 return findByPermissions(
322                     companyId, parentOrganizationId,
323                     parentOrganizationIdComparator, names, type, streets,
324                     cities, zips, regionId, countryId, resourceId.longValue(),
325                     groupId.longValue(), andOperator, start, end, obc);
326             }
327         }
328 
329         Session session = null;
330 
331         try {
332             session = openSession();
333 
334             String sql = null;
335 
336             if (Validator.isNotNull(type)) {
337                 sql = CustomSQLUtil.get(FIND_BY_C_PO_N_L_S_C_Z_R_C);
338             }
339             else {
340                 sql = CustomSQLUtil.get(FIND_BY_C_PO_N_S_C_Z_R_C);
341             }
342 
343             sql = CustomSQLUtil.replaceKeywords(
344                 sql, "lower(Organization_.name)", StringPool.LIKE, false,
345                 names);
346             sql = CustomSQLUtil.replaceKeywords(
347                 sql, "lower(Address.street1)", StringPool.LIKE, true,
348                 streets);
349             sql = CustomSQLUtil.replaceKeywords(
350                 sql, "lower(Address.street2)", StringPool.LIKE, true,
351                 streets);
352             sql = CustomSQLUtil.replaceKeywords(
353                 sql, "lower(Address.street3)", StringPool.LIKE, true,
354                 streets);
355             sql = CustomSQLUtil.replaceKeywords(
356                 sql, "lower(Address.city)", StringPool.LIKE, false,
357                 cities);
358             sql = CustomSQLUtil.replaceKeywords(
359                 sql, "lower(Address.zip)", StringPool.LIKE, true,
360                 zips);
361 
362             if (regionId == null) {
363                 sql = StringUtil.replace(sql, REGION_ID_SQL, StringPool.BLANK);
364             }
365 
366             if (countryId == null) {
367                 sql = StringUtil.replace(sql, COUNTRY_ID_SQL, StringPool.BLANK);
368             }
369 
370             sql = StringUtil.replace(sql, "[$JOIN$]", getJoin(params));
371             sql = StringUtil.replace(sql, "[$WHERE$]", getWhere(params));
372             sql = StringUtil.replace(
373                 sql, "[$PARENT_ORGANIZATION_ID_COMPARATOR$]",
374                 parentOrganizationIdComparator);
375             sql = CustomSQLUtil.replaceAndOperator(sql, andOperator);
376             sql = CustomSQLUtil.replaceOrderBy(sql, obc);
377 
378             SQLQuery q = session.createSQLQuery(sql);
379 
380             q.addScalar("orgId", Type.LONG);
381 
382             QueryPos qPos = QueryPos.getInstance(q);
383 
384             setJoin(qPos, params);
385             qPos.add(companyId);
386             qPos.add(parentOrganizationId);
387 
388             if (Validator.isNotNull(type)) {
389                 qPos.add(type);
390             }
391 
392             qPos.add(names, 2);
393             qPos.add(streets, 6);
394 
395             if (regionId != null) {
396                 qPos.add(regionId);
397                 qPos.add(regionId);
398             }
399 
400             if (countryId != null) {
401                 qPos.add(countryId);
402                 qPos.add(countryId);
403             }
404 
405             qPos.add(cities, 2);
406             qPos.add(zips, 2);
407 
408             List<Organization> organizations = new ArrayList<Organization>();
409 
410             Iterator<Long> itr = (Iterator<Long>)QueryUtil.iterate(
411                 q, getDialect(), start, end);
412 
413             while (itr.hasNext()) {
414                 Long organizationId = itr.next();
415 
416                 Organization organization = OrganizationUtil.findByPrimaryKey(
417                     organizationId.longValue());
418 
419                 organizations.add(organization);
420             }
421 
422             return organizations;
423         }
424         catch (Exception e) {
425             throw new SystemException(e);
426         }
427         finally {
428             closeSession(session);
429         }
430     }
431 
432     protected int countByPermissions(
433             long companyId, long parentOrganizationId,
434             String parentOrganizationIdComparator, String[] names,
435             String type, String[] streets, String[] cities, String[] zips,
436             Long regionId, Long countryId, long resourceId, long groupId,
437             boolean andOperator)
438         throws SystemException {
439 
440         Session session = null;
441 
442         try {
443             session = openSession();
444 
445             StringBuilder sb = new StringBuilder();
446 
447             sb.append("(");
448 
449             if (Validator.isNotNull(type)) {
450                 sb.append(CustomSQLUtil.get(COUNT_BY_C_PO_N_L_S_C_Z_R_C));
451             }
452             else {
453                 sb.append(CustomSQLUtil.get(COUNT_BY_C_PO_N_S_C_Z_R_C));
454             }
455 
456             String sql = sb.toString();
457 
458             if (regionId == null) {
459                 sql = StringUtil.replace(sql, REGION_ID_SQL, StringPool.BLANK);
460             }
461 
462             if (countryId == null) {
463                 sql = StringUtil.replace(sql, COUNTRY_ID_SQL, StringPool.BLANK);
464             }
465 
466             sql = StringUtil.replace(
467                 sql, "[$JOIN$]", getJoin("groupsPermissions"));
468             sql = StringUtil.replace(
469                 sql, "[$WHERE$]", getWhere("groupsPermissions"));
470 
471             sb = new StringBuilder();
472 
473             sb.append(sql);
474 
475             sb.append(") UNION (");
476 
477             if (Validator.isNotNull(type)) {
478                 sb.append(CustomSQLUtil.get(COUNT_BY_C_PO_N_L_S_C_Z_R_C));
479             }
480             else {
481                 sb.append(CustomSQLUtil.get(COUNT_BY_C_PO_N_S_C_Z_R_C));
482             }
483 
484             sql = sb.toString();
485 
486             if (regionId == null) {
487                 sql = StringUtil.replace(sql, REGION_ID_SQL, StringPool.BLANK);
488             }
489 
490             if (countryId == null) {
491                 sql = StringUtil.replace(sql, COUNTRY_ID_SQL, StringPool.BLANK);
492             }
493 
494             sql = StringUtil.replace(
495                 sql, "[$JOIN$]", getJoin("orgGroupPermission"));
496             sql = StringUtil.replace(
497                 sql, "[$WHERE$]", getWhere("orgGroupPermission"));
498             sql = StringUtil.replace(
499                 sql, "[$PARENT_ORGANIZATION_ID_COMPARATOR$]",
500                 parentOrganizationIdComparator);
501             sql = CustomSQLUtil.replaceAndOperator(sql, andOperator);
502 
503             sb = new StringBuilder();
504 
505             sb.append(sql);
506 
507             sb.append(")");
508 
509             sql = sb.toString();
510 
511             sql = CustomSQLUtil.replaceKeywords(
512                 sql, "lower(Organization_.name)", StringPool.LIKE, false,
513                 names);
514             sql = CustomSQLUtil.replaceKeywords(
515                 sql, "lower(Address.street1)", StringPool.LIKE, true,
516                 streets);
517             sql = CustomSQLUtil.replaceKeywords(
518                 sql, "lower(Address.street2)", StringPool.LIKE, true,
519                 streets);
520             sql = CustomSQLUtil.replaceKeywords(
521                 sql, "lower(Address.street3)", StringPool.LIKE, true,
522                 streets);
523             sql = CustomSQLUtil.replaceKeywords(
524                 sql, "lower(Address.city)", StringPool.LIKE, false,
525                 cities);
526             sql = CustomSQLUtil.replaceKeywords(
527                 sql, "lower(Address.zip)", StringPool.LIKE, true,
528                 zips);
529 
530             if (regionId == null) {
531                 sql = StringUtil.replace(sql, REGION_ID_SQL, StringPool.BLANK);
532             }
533 
534             if (countryId == null) {
535                 sql = StringUtil.replace(sql, COUNTRY_ID_SQL, StringPool.BLANK);
536             }
537 
538             SQLQuery q = session.createSQLQuery(sql);
539 
540             q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
541 
542             QueryPos qPos = QueryPos.getInstance(q);
543 
544             for (int i = 0; i < 2; i++) {
545                 qPos.add(resourceId);
546 
547                 if (i == 1) {
548                     qPos.add(groupId);
549                 }
550 
551                 qPos.add(companyId);
552                 qPos.add(parentOrganizationId);
553 
554                 if (Validator.isNotNull(type)) {
555                     qPos.add(type);
556                 }
557 
558                 qPos.add(names, 2);
559                 qPos.add(streets, 6);
560 
561                 if (regionId != null) {
562                     qPos.add(regionId);
563                     qPos.add(regionId);
564                 }
565 
566                 if (countryId != null) {
567                     qPos.add(countryId);
568                     qPos.add(countryId);
569                 }
570 
571                 qPos.add(cities, 2);
572                 qPos.add(zips, 2);
573             }
574 
575             int count = 0;
576 
577             Iterator<Long> itr = q.list().iterator();
578 
579             while (itr.hasNext()) {
580                 Long l = itr.next();
581 
582                 if (l != null) {
583                     count += l.intValue();
584                 }
585             }
586 
587             return count;
588         }
589         catch (Exception e) {
590             throw new SystemException(e);
591         }
592         finally {
593             closeSession(session);
594         }
595     }
596 
597     protected List<Organization> findByPermissions(
598             long companyId, long parentOrganizationId,
599             String parentOrganizationIdComparator, String[] names,
600             String type, String[] streets, String[] cities, String[] zips,
601             Long regionId, Long countryId, long resourceId, long groupId,
602             boolean andOperator, int start, int end, OrderByComparator obc)
603         throws SystemException {
604 
605         Session session = null;
606 
607         try {
608             session = openSession();
609 
610             StringBuilder sb = new StringBuilder();
611 
612             sb.append("(");
613 
614             if (Validator.isNotNull(type)) {
615                 sb.append(CustomSQLUtil.get(FIND_BY_C_PO_N_L_S_C_Z_R_C));
616             }
617             else {
618                 sb.append(CustomSQLUtil.get(FIND_BY_C_PO_N_S_C_Z_R_C));
619             }
620 
621             String sql = sb.toString();
622 
623             if (regionId == null) {
624                 sql = StringUtil.replace(sql, REGION_ID_SQL, StringPool.BLANK);
625             }
626 
627             if (countryId == null) {
628                 sql = StringUtil.replace(sql, COUNTRY_ID_SQL, StringPool.BLANK);
629             }
630 
631             sql = StringUtil.replace(
632                 sql, "[$JOIN$]", getJoin("groupsPermissions"));
633             sql = StringUtil.replace(
634                 sql, "[$WHERE$]", getWhere("groupsPermissions"));
635 
636             sb = new StringBuilder();
637 
638             sb.append(sql);
639 
640             sb.append(") UNION (");
641 
642             if (Validator.isNotNull(type)) {
643                 sb.append(CustomSQLUtil.get(FIND_BY_C_PO_N_L_S_C_Z_R_C));
644             }
645             else {
646                 sb.append(CustomSQLUtil.get(FIND_BY_C_PO_N_S_C_Z_R_C));
647             }
648 
649             sql = sb.toString();
650 
651             if (regionId == null) {
652                 sql = StringUtil.replace(sql, REGION_ID_SQL, StringPool.BLANK);
653             }
654 
655             if (countryId == null) {
656                 sql = StringUtil.replace(sql, COUNTRY_ID_SQL, StringPool.BLANK);
657             }
658 
659             sql = StringUtil.replace(
660                 sql, "[$JOIN$]", getJoin("orgGroupPermission"));
661             sql = StringUtil.replace(
662                 sql, "[$WHERE$]", getWhere("orgGroupPermission"));
663             sql = StringUtil.replace(
664                 sql, "[$PARENT_ORGANIZATION_ID_COMPARATOR$]",
665                 parentOrganizationIdComparator);
666             sql = CustomSQLUtil.replaceAndOperator(sql, andOperator);
667 
668             sb = new StringBuilder();
669 
670             sb.append(sql);
671 
672             sb.append(") ");
673 
674             sql = sb.toString();
675 
676             sql = CustomSQLUtil.replaceKeywords(
677                 sql, "lower(Organization_.name)", StringPool.LIKE, false,
678                 names);
679             sql = CustomSQLUtil.replaceKeywords(
680                 sql, "lower(Address.street1)", StringPool.LIKE, true,
681                 streets);
682             sql = CustomSQLUtil.replaceKeywords(
683                 sql, "lower(Address.street2)", StringPool.LIKE, true,
684                 streets);
685             sql = CustomSQLUtil.replaceKeywords(
686                 sql, "lower(Address.street3)", StringPool.LIKE, true,
687                 streets);
688             sql = CustomSQLUtil.replaceKeywords(
689                 sql, "lower(Address.city)", StringPool.LIKE, false,
690                 cities);
691             sql = CustomSQLUtil.replaceKeywords(
692                 sql, "lower(Address.zip)", StringPool.LIKE, true,
693                 zips);
694 
695             if (regionId == null) {
696                 sql = StringUtil.replace(sql, REGION_ID_SQL, StringPool.BLANK);
697             }
698 
699             if (countryId == null) {
700                 sql = StringUtil.replace(sql, COUNTRY_ID_SQL, StringPool.BLANK);
701             }
702 
703             sql = CustomSQLUtil.replaceOrderBy(sql, obc);
704 
705             SQLQuery q = session.createSQLQuery(sql);
706 
707             q.addScalar("orgId", Type.LONG);
708 
709             QueryPos qPos = QueryPos.getInstance(q);
710 
711             for (int i = 0; i < 2; i++) {
712                 qPos.add(resourceId);
713 
714                 if (i == 1) {
715                     qPos.add(groupId);
716                 }
717 
718                 qPos.add(companyId);
719                 qPos.add(parentOrganizationId);
720 
721                 if (Validator.isNotNull(type)) {
722                     qPos.add(type);
723                 }
724 
725                 qPos.add(names, 2);
726                 qPos.add(streets, 6);
727 
728                 if (regionId != null) {
729                     qPos.add(regionId);
730                     qPos.add(regionId);
731                 }
732 
733                 if (countryId != null) {
734                     qPos.add(countryId);
735                     qPos.add(countryId);
736                 }
737 
738                 qPos.add(cities, 2);
739                 qPos.add(zips, 2);
740             }
741 
742             List<Organization> organizations = new ArrayList<Organization>();
743 
744             Iterator<Long> itr = (Iterator<Long>)QueryUtil.iterate(
745                 q, getDialect(), start, end);
746 
747             while (itr.hasNext()) {
748                 Long organizationId = itr.next();
749 
750                 Organization organization = OrganizationUtil.findByPrimaryKey(
751                     organizationId.longValue());
752 
753                 organizations.add(organization);
754             }
755 
756             return organizations;
757         }
758         catch (Exception e) {
759             throw new SystemException(e);
760         }
761         finally {
762             closeSession(session);
763         }
764     }
765 
766     protected String getJoin(LinkedHashMap<String, Object> params) {
767         if (params == null) {
768             return StringPool.BLANK;
769         }
770 
771         StringBuilder sb = new StringBuilder();
772 
773         Iterator<Map.Entry<String, Object>> itr = params.entrySet().iterator();
774 
775         while (itr.hasNext()) {
776             Map.Entry<String, Object> entry = itr.next();
777 
778             String key = entry.getKey();
779             Object value = entry.getValue();
780 
781             if (Validator.isNotNull(value)) {
782                 sb.append(getJoin(key));
783             }
784         }
785 
786         return sb.toString();
787     }
788 
789     protected String getJoin(String key) {
790         String join = StringPool.BLANK;
791 
792         if (key.equals("groupsPermissions")) {
793             join = CustomSQLUtil.get(JOIN_BY_GROUPS_PERMISSIONS);
794         }
795         else if (key.equals("organizationsGroups")) {
796             join = CustomSQLUtil.get(JOIN_BY_ORGANIZATIONS_GROUPS);
797         }
798         else if (key.equals("organizationsPasswordPolicies")) {
799             join = CustomSQLUtil.get(JOIN_BY_ORGANIZATIONS_PASSWORD_POLICIES);
800         }
801         else if (key.equals("organizationsRoles")) {
802             join = CustomSQLUtil.get(JOIN_BY_ORGANIZATIONS_ROLES);
803         }
804         else if (key.equals("organizationsUsers")) {
805             join = CustomSQLUtil.get(JOIN_BY_ORGANIZATIONS_USERS);
806         }
807         else if (key.equals("orgGroupPermission")) {
808             join = CustomSQLUtil.get(JOIN_BY_ORG_GROUP_PERMISSION);
809         }
810 
811         if (Validator.isNotNull(join)) {
812             int pos = join.indexOf("WHERE");
813 
814             if (pos != -1) {
815                 join = join.substring(0, pos);
816             }
817         }
818 
819         return join;
820     }
821 
822     protected String getWhere(LinkedHashMap<String, Object> params) {
823         if (params == null) {
824             return StringPool.BLANK;
825         }
826 
827         StringBuilder sb = new StringBuilder();
828 
829         Iterator<Map.Entry<String, Object>> itr = params.entrySet().iterator();
830 
831         while (itr.hasNext()) {
832             Map.Entry<String, Object> entry = itr.next();
833 
834             String key = entry.getKey();
835             Object value = entry.getValue();
836 
837             if (Validator.isNotNull(value)) {
838                 sb.append(getWhere(key, value));
839             }
840         }
841 
842         return sb.toString();
843     }
844 
845     protected String getWhere(String key) {
846         return getWhere(key, null);
847     }
848 
849     protected String getWhere(String key, Object value) {
850         String join = StringPool.BLANK;
851 
852         if (key.equals("groupsPermissions")) {
853             join = CustomSQLUtil.get(JOIN_BY_GROUPS_PERMISSIONS);
854         }
855         else if (key.equals("organizations")) {
856             Long[] organizationIds = (Long[])value;
857 
858             StringBuilder sb = new StringBuilder();
859 
860             sb.append("WHERE (");
861 
862             for (int i = 0; i < organizationIds.length; i++) {
863                 sb.append("(Organization_.organizationId = ?) ");
864 
865                 if ((i + 1) < organizationIds.length) {
866                     sb.append("OR ");
867                 }
868             }
869 
870             if (organizationIds.length == 0) {
871                 sb.append("(Organization_.organizationId = -1) ");
872             }
873 
874             sb.append(")");
875 
876             join = sb.toString();
877         }
878         else if (key.equals("organizationsGroups")) {
879             join = CustomSQLUtil.get(JOIN_BY_ORGANIZATIONS_GROUPS);
880         }
881         else if (key.equals("organizationsPasswordPolicies")) {
882             join = CustomSQLUtil.get(JOIN_BY_ORGANIZATIONS_PASSWORD_POLICIES);
883         }
884         else if (key.equals("organizationsRoles")) {
885             join = CustomSQLUtil.get(JOIN_BY_ORGANIZATIONS_ROLES);
886         }
887         else if (key.equals("organizationsUsers")) {
888             join = CustomSQLUtil.get(JOIN_BY_ORGANIZATIONS_USERS);
889         }
890         else if (key.equals("orgGroupPermission")) {
891             join = CustomSQLUtil.get(JOIN_BY_ORG_GROUP_PERMISSION);
892         }
893 
894         if (Validator.isNotNull(join)) {
895             int pos = join.indexOf("WHERE");
896 
897             if (pos != -1) {
898                 StringBuilder sb = new StringBuilder();
899 
900                 sb.append(join.substring(pos + 5, join.length()));
901                 sb.append(" AND ");
902 
903                 join = sb.toString();
904             }
905             else {
906                 join = StringPool.BLANK;
907             }
908         }
909 
910         return join;
911     }
912 
913     protected void setJoin(
914         QueryPos qPos, LinkedHashMap<String, Object> params) {
915 
916         if (params != null) {
917             Iterator<Map.Entry<String, Object>> itr =
918                 params.entrySet().iterator();
919 
920             while (itr.hasNext()) {
921                 Map.Entry<String, Object> entry = itr.next();
922 
923                 Object value = entry.getValue();
924 
925                 if (value instanceof Long) {
926                     Long valueLong = (Long)value;
927 
928                     if (Validator.isNotNull(valueLong)) {
929                         qPos.add(valueLong);
930                     }
931                 }
932                 else if (value instanceof Long[]) {
933                     Long[] valueArray = (Long[])value;
934 
935                     for (int i = 0; i < valueArray.length; i++) {
936                         if (Validator.isNotNull(valueArray[i])) {
937                             qPos.add(valueArray[i]);
938                         }
939                     }
940                 }
941                 else if (value instanceof String) {
942                     String valueString = (String)value;
943 
944                     if (Validator.isNotNull(valueString)) {
945                         qPos.add(valueString);
946                     }
947                 }
948             }
949         }
950     }
951 
952     protected static String COUNTRY_ID_SQL =
953         "((Organization_.countryId = ?) OR (Address.countryId = ?)) " +
954             "[$AND_OR_CONNECTOR$]";
955 
956     protected static String REGION_ID_SQL =
957         "((Organization_.regionId = ?) OR (Address.regionId = ?)) " +
958             "[$AND_OR_CONNECTOR$]";
959 
960 }