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