001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.service.persistence;
016    
017    import com.liferay.portal.kernel.dao.orm.QueryPos;
018    import com.liferay.portal.kernel.dao.orm.QueryUtil;
019    import com.liferay.portal.kernel.dao.orm.SQLQuery;
020    import com.liferay.portal.kernel.dao.orm.Session;
021    import com.liferay.portal.kernel.dao.orm.Type;
022    import com.liferay.portal.kernel.exception.SystemException;
023    import com.liferay.portal.kernel.util.OrderByComparator;
024    import com.liferay.portal.kernel.util.StringBundler;
025    import com.liferay.portal.kernel.util.StringPool;
026    import com.liferay.portal.kernel.util.StringUtil;
027    import com.liferay.portal.kernel.util.Validator;
028    import com.liferay.portal.model.Organization;
029    import com.liferay.portal.model.impl.OrganizationImpl;
030    import com.liferay.portal.service.persistence.impl.BasePersistenceImpl;
031    import com.liferay.util.dao.orm.CustomSQLUtil;
032    
033    import java.util.ArrayList;
034    import java.util.Iterator;
035    import java.util.LinkedHashMap;
036    import java.util.List;
037    import java.util.Map;
038    
039    /**
040     * @author Amos Fong
041     * @author Brian Wing Shun Chan
042     * @author Jorge Ferrer
043     * @author Connor McKay
044     * @author Shuyang Zhou
045     */
046    public class OrganizationFinderImpl
047            extends BasePersistenceImpl<Organization> implements OrganizationFinder {
048    
049            public static final String COUNT_BY_GROUP_ID =
050                    OrganizationFinder.class.getName() + ".countByGroupId";
051    
052            public static final String COUNT_BY_ORGANIZATION_ID =
053                    OrganizationFinder.class.getName() + ".countByOrganizationId";
054    
055            public static final String COUNT_BY_C_PO_N_S_C_Z_R_C =
056                    OrganizationFinder.class.getName() + ".countByC_PO_N_S_C_Z_R_C";
057    
058            public static final String COUNT_BY_C_PO_N_L_S_C_Z_R_C =
059                    OrganizationFinder.class.getName() + ".countByC_PO_N_L_S_C_Z_R_C";
060    
061            public static final String FIND_BY_NO_ASSETS =
062            OrganizationFinder.class.getName() + ".findByNoAssets";
063    
064            public static final String FIND_BY_GROUP_ID =
065                    OrganizationFinder.class.getName() + ".findByGroupId";
066    
067            public static final String FIND_BY_C_P =
068                    OrganizationFinder.class.getName() + ".findByC_P";
069    
070            public static final String FIND_BY_C_PO_N_S_C_Z_R_C =
071                    OrganizationFinder.class.getName() + ".findByC_PO_N_S_C_Z_R_C";
072    
073            public static final String FIND_BY_C_PO_N_L_S_C_Z_R_C =
074                    OrganizationFinder.class.getName() + ".findByC_PO_N_L_S_C_Z_R_C";
075    
076            public static final String JOIN_BY_ORGANIZATIONS_GROUPS =
077                    OrganizationFinder.class.getName() + ".joinByOrganizationsGroups";
078    
079            public static final String JOIN_BY_ORGANIZATIONS_PASSWORD_POLICIES =
080                    OrganizationFinder.class.getName() +
081                            ".joinByOrganizationsPasswordPolicies";
082    
083            public static final String JOIN_BY_ORGANIZATIONS_ROLES =
084                    OrganizationFinder.class.getName() + ".joinByOrganizationsRoles";
085    
086            public static final String JOIN_BY_ORGANIZATIONS_USERS =
087                    OrganizationFinder.class.getName() + ".joinByOrganizationsUsers";
088    
089            public static final String JOIN_BY_USERS_ORGS =
090                    OrganizationFinder.class.getName() + ".joinByUsersOrgs";
091    
092            @Override
093            public int countByKeywords(
094                            long companyId, long parentOrganizationId,
095                            String parentOrganizationIdComparator, String keywords, String type,
096                            Long regionId, Long countryId,
097                            LinkedHashMap<String, Object> params)
098                    throws SystemException {
099    
100                    String[] names = null;
101                    String[] streets = null;
102                    String[] cities = null;
103                    String[] zips = null;
104                    boolean andOperator = false;
105    
106                    if (Validator.isNotNull(keywords)) {
107                            names = CustomSQLUtil.keywords(keywords);
108                            streets = CustomSQLUtil.keywords(keywords);
109                            cities = CustomSQLUtil.keywords(keywords);
110                            zips = CustomSQLUtil.keywords(keywords);
111                    }
112                    else {
113                            andOperator = true;
114                    }
115    
116                    return countByC_PO_N_T_S_C_Z_R_C(
117                            companyId, parentOrganizationId, parentOrganizationIdComparator,
118                            names, type, streets, cities, zips, regionId, countryId, params,
119                            andOperator);
120            }
121    
122            @Override
123            public int countByO_U(long organizationId, long userId)
124                    throws SystemException {
125    
126                    LinkedHashMap<String, Object> params1 =
127                            new LinkedHashMap<String, Object>();
128    
129                    params1.put("usersOrgs", userId);
130    
131                    Session session = null;
132    
133                    try {
134                            session = openSession();
135    
136                            int count = countByOrganizationId(session, organizationId, params1);
137    
138                            return count;
139                    }
140                    catch (Exception e) {
141                            throw new SystemException(e);
142                    }
143                    finally {
144                            closeSession(session);
145                    }
146            }
147    
148            @Override
149            public int countByC_PO_N_T_S_C_Z_R_C(
150                            long companyId, long parentOrganizationId,
151                            String parentOrganizationIdComparator, String name, String type,
152                            String street, String city, String zip, Long regionId,
153                            Long countryId, LinkedHashMap<String, Object> params,
154                            boolean andOperator)
155                    throws SystemException {
156    
157                    String[] names = CustomSQLUtil.keywords(name);
158                    String[] streets = CustomSQLUtil.keywords(street);
159                    String[] cities = CustomSQLUtil.keywords(city);
160                    String[] zips = CustomSQLUtil.keywords(zip);
161    
162                    return countByC_PO_N_T_S_C_Z_R_C(
163                            companyId, parentOrganizationId, parentOrganizationIdComparator,
164                            names, type, streets, cities, zips, regionId, countryId, params,
165                            andOperator);
166            }
167    
168            @Override
169            public int countByC_PO_N_T_S_C_Z_R_C(
170                            long companyId, long parentOrganizationId,
171                            String parentOrganizationIdComparator, String[] names, String type,
172                            String[] streets, String[] cities, String[] zips, Long regionId,
173                            Long countryId, LinkedHashMap<String, Object> params,
174                            boolean andOperator)
175                    throws SystemException {
176    
177                    names = CustomSQLUtil.keywords(names);
178                    streets = CustomSQLUtil.keywords(streets);
179                    cities = CustomSQLUtil.keywords(cities);
180                    zips = CustomSQLUtil.keywords(zips);
181    
182                    Session session = null;
183    
184                    try {
185                            session = openSession();
186    
187                            StringBundler sb = new StringBundler();
188    
189                            boolean doUnion = false;
190    
191                            if (params != null) {
192                                    Long groupOrganization = (Long)params.get("groupOrganization");
193    
194                                    if (groupOrganization != null) {
195                                            doUnion = true;
196                                    }
197                            }
198    
199                            if (doUnion) {
200                                    sb.append(StringPool.OPEN_PARENTHESIS);
201                                    sb.append(CustomSQLUtil.get(COUNT_BY_GROUP_ID));
202                                    sb.append(") UNION ALL (");
203                            }
204    
205                            if (Validator.isNotNull(type)) {
206                                    sb.append(CustomSQLUtil.get(COUNT_BY_C_PO_N_L_S_C_Z_R_C));
207                            }
208                            else {
209                                    sb.append(CustomSQLUtil.get(COUNT_BY_C_PO_N_S_C_Z_R_C));
210                            }
211    
212                            if (doUnion) {
213                                    sb.append(StringPool.CLOSE_PARENTHESIS);
214                            }
215    
216                            String sql = sb.toString();
217    
218                            sql = CustomSQLUtil.replaceKeywords(
219                                    sql, "lower(Organization_.name)", StringPool.LIKE, false,
220                                    names);
221                            sql = CustomSQLUtil.replaceKeywords(
222                                    sql, "lower(Address.street1)", StringPool.LIKE, true, streets);
223                            sql = CustomSQLUtil.replaceKeywords(
224                                    sql, "lower(Address.street2)", StringPool.LIKE, true, streets);
225                            sql = CustomSQLUtil.replaceKeywords(
226                                    sql, "lower(Address.street3)", StringPool.LIKE, true, streets);
227                            sql = CustomSQLUtil.replaceKeywords(
228                                    sql, "lower(Address.city)", StringPool.LIKE, false, cities);
229                            sql = CustomSQLUtil.replaceKeywords(
230                                    sql, "lower(Address.zip)", StringPool.LIKE, true, zips);
231    
232                            if (regionId == null) {
233                                    sql = StringUtil.replace(sql, _REGION_ID_SQL, StringPool.BLANK);
234                            }
235    
236                            if (countryId == null) {
237                                    sql = StringUtil.replace(
238                                            sql, _COUNTRY_ID_SQL, StringPool.BLANK);
239                            }
240    
241                            sql = StringUtil.replace(sql, "[$JOIN$]", getJoin(params));
242                            sql = StringUtil.replace(sql, "[$WHERE$]", getWhere(params));
243                            sql = StringUtil.replace(
244                                    sql, "[$PARENT_ORGANIZATION_ID_COMPARATOR$]",
245                                    parentOrganizationIdComparator.equals(StringPool.EQUAL) ?
246                                    StringPool.EQUAL : StringPool.NOT_EQUAL);
247                            sql = CustomSQLUtil.replaceAndOperator(sql, andOperator);
248    
249                            SQLQuery q = session.createSQLQuery(sql);
250    
251                            q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
252    
253                            QueryPos qPos = QueryPos.getInstance(q);
254    
255                            setJoin(qPos, params);
256    
257                            qPos.add(companyId);
258                            qPos.add(parentOrganizationId);
259    
260                            if (Validator.isNotNull(type)) {
261                                    qPos.add(type);
262                            }
263    
264                            qPos.add(names, 2);
265                            qPos.add(streets, 6);
266    
267                            if (regionId != null) {
268                                    qPos.add(regionId);
269                                    qPos.add(regionId);
270                            }
271    
272                            if (countryId != null) {
273                                    qPos.add(countryId);
274                                    qPos.add(countryId);
275                            }
276    
277                            qPos.add(cities, 2);
278                            qPos.add(zips, 2);
279    
280                            int count = 0;
281    
282                            Iterator<Long> itr = q.iterate();
283    
284                            while (itr.hasNext()) {
285                                    Long l = itr.next();
286    
287                                    if (l != null) {
288                                            count += l.intValue();
289                                    }
290                            }
291    
292                            return count;
293                    }
294                    catch (Exception e) {
295                            throw new SystemException(e);
296                    }
297                    finally {
298                            closeSession(session);
299                    }
300            }
301    
302            @Override
303            public List<Organization> findByKeywords(
304                            long companyId, long parentOrganizationId,
305                            String parentOrganizationIdComparator, String keywords, String type,
306                            Long regionId, Long countryId, LinkedHashMap<String, Object> params,
307                            int start, int end, OrderByComparator obc)
308                    throws SystemException {
309    
310                    String[] names = null;
311                    String[] streets = null;
312                    String[] cities = null;
313                    String[] zips = null;
314                    boolean andOperator = false;
315    
316                    if (Validator.isNotNull(keywords)) {
317                            names = CustomSQLUtil.keywords(keywords);
318                            streets = CustomSQLUtil.keywords(keywords);
319                            cities = CustomSQLUtil.keywords(keywords);
320                            zips = CustomSQLUtil.keywords(keywords);
321                    }
322                    else {
323                            andOperator = true;
324                    }
325    
326                    return findByC_PO_N_T_S_C_Z_R_C(
327                            companyId, parentOrganizationId, parentOrganizationIdComparator,
328                            names, type, streets, cities, zips, regionId, countryId, params,
329                            andOperator, start, end, obc);
330            }
331    
332            @Override
333            public List<Organization> findByNoAssets() throws SystemException {
334                    Session session = null;
335    
336                    try {
337                            session = openSession();
338    
339                            String sql = CustomSQLUtil.get(FIND_BY_NO_ASSETS);
340    
341                            SQLQuery q = session.createSQLQuery(sql);
342    
343                            q.addEntity("Organization_", OrganizationImpl.class);
344    
345                            return q.list(true);
346                    }
347                    catch (Exception e) {
348                            throw new SystemException(e);
349                    }
350                    finally {
351                            closeSession(session);
352                    }
353            }
354    
355            @Override
356            public List<Long> findByC_P(
357                            long companyId, long parentOrganizationId,
358                            long previousOrganizationId, int size)
359                    throws SystemException {
360    
361                    Session session = null;
362    
363                    try {
364                            session = openSession();
365    
366                            String sql = CustomSQLUtil.get(FIND_BY_C_P);
367    
368                            if (previousOrganizationId == 0) {
369                                    sql = StringUtil.replace(
370                                            sql, "(organizationId > ?) AND", StringPool.BLANK);
371                            }
372    
373                            SQLQuery q = session.createSQLQuery(sql);
374    
375                            q.addScalar("organizationId", Type.LONG);
376    
377                            QueryPos qPos = QueryPos.getInstance(q);
378    
379                            if (previousOrganizationId > 0) {
380                                    qPos.add(previousOrganizationId);
381                            }
382    
383                            qPos.add(companyId);
384                            qPos.add(parentOrganizationId);
385    
386                            return (List<Long>)QueryUtil.list(q, getDialect(), 0, size);
387                    }
388                    catch (Exception e) {
389                            throw new SystemException(e);
390                    }
391                    finally {
392                            closeSession(session);
393                    }
394            }
395    
396            @Override
397            public List<Organization> findByC_PO_N_T_S_C_Z_R_C(
398                            long companyId, long parentOrganizationId,
399                            String parentOrganizationIdComparator, String name, String type,
400                            String street, String city, String zip, Long regionId,
401                            Long countryId, LinkedHashMap<String, Object> params,
402                            boolean andOperator, int start, int end, OrderByComparator obc)
403                    throws SystemException {
404    
405                    String[] names = CustomSQLUtil.keywords(name);
406                    String[] streets = CustomSQLUtil.keywords(street);
407                    String[] cities = CustomSQLUtil.keywords(city);
408                    String[] zips = CustomSQLUtil.keywords(zip);
409    
410                    return findByC_PO_N_T_S_C_Z_R_C(
411                            companyId, parentOrganizationId, parentOrganizationIdComparator,
412                            names, type, streets, cities, zips, regionId, countryId, params,
413                            andOperator, start, end, obc);
414            }
415    
416            @Override
417            public List<Organization> findByC_PO_N_T_S_C_Z_R_C(
418                            long companyId, long parentOrganizationId,
419                            String parentOrganizationIdComparator, String[] names, String type,
420                            String[] streets, String[] cities, String[] zips, Long regionId,
421                            Long countryId, LinkedHashMap<String, Object> params,
422                            boolean andOperator, int start, int end, OrderByComparator obc)
423                    throws SystemException {
424    
425                    names = CustomSQLUtil.keywords(names);
426                    streets = CustomSQLUtil.keywords(streets);
427                    cities = CustomSQLUtil.keywords(cities);
428                    zips = CustomSQLUtil.keywords(zips);
429    
430                    if (params == null) {
431                            params = new LinkedHashMap<String, Object>();
432                    }
433    
434                    StringBundler sb = new StringBundler();
435    
436                    sb.append(StringPool.OPEN_PARENTHESIS);
437    
438                    Long groupOrganization = (Long)params.get("groupOrganization");
439    
440                    boolean doUnion = Validator.isNotNull(groupOrganization);
441    
442                    if (doUnion) {
443                            sb.append(CustomSQLUtil.get(FIND_BY_GROUP_ID));
444                            sb.append(") UNION ALL (");
445                    }
446    
447                    if (Validator.isNotNull(type)) {
448                            sb.append(CustomSQLUtil.get(FIND_BY_C_PO_N_L_S_C_Z_R_C));
449                    }
450                    else {
451                            sb.append(CustomSQLUtil.get(FIND_BY_C_PO_N_S_C_Z_R_C));
452                    }
453    
454                    String sql = sb.toString();
455    
456                    sql = StringUtil.replace(sql, "[$JOIN$]", getJoin(params));
457                    sql = StringUtil.replace(sql, "[$WHERE$]", getWhere(params));
458                    sql = sql.concat(StringPool.CLOSE_PARENTHESIS);
459                    sql = CustomSQLUtil.replaceKeywords(
460                            sql, "lower(Organization_.name)", StringPool.LIKE, false, names);
461                    sql = CustomSQLUtil.replaceKeywords(
462                            sql, "lower(Address.street1)", StringPool.LIKE, true, streets);
463                    sql = CustomSQLUtil.replaceKeywords(
464                            sql, "lower(Address.street2)", StringPool.LIKE, true, streets);
465                    sql = CustomSQLUtil.replaceKeywords(
466                            sql, "lower(Address.street3)", StringPool.LIKE, true, streets);
467                    sql = CustomSQLUtil.replaceKeywords(
468                            sql, "lower(Address.city)", StringPool.LIKE, false, cities);
469                    sql = CustomSQLUtil.replaceKeywords(
470                            sql, "lower(Address.zip)", StringPool.LIKE, true, zips);
471                    sql = StringUtil.replace(
472                            sql, "[$PARENT_ORGANIZATION_ID_COMPARATOR$]",
473                            parentOrganizationIdComparator.equals(StringPool.EQUAL) ?
474                            StringPool.EQUAL : StringPool.NOT_EQUAL);
475    
476                    if (regionId == null) {
477                            sql = StringUtil.replace(sql, _REGION_ID_SQL, StringPool.BLANK);
478                    }
479    
480                    if (countryId == null) {
481                            sql = StringUtil.replace(sql, _COUNTRY_ID_SQL, StringPool.BLANK);
482                    }
483    
484                    sql = CustomSQLUtil.replaceAndOperator(sql, andOperator);
485                    sql = CustomSQLUtil.replaceOrderBy(sql, obc);
486    
487                    Session session = null;
488    
489                    try {
490                            session = openSession();
491    
492                            SQLQuery q = session.createSQLQuery(sql);
493    
494                            q.addScalar("orgId", Type.LONG);
495    
496                            QueryPos qPos = QueryPos.getInstance(q);
497    
498                            setJoin(qPos, params);
499    
500                            qPos.add(companyId);
501                            qPos.add(parentOrganizationId);
502    
503                            if (Validator.isNotNull(type)) {
504                                    qPos.add(type);
505                            }
506    
507                            qPos.add(names, 2);
508                            qPos.add(streets, 6);
509    
510                            if (regionId != null) {
511                                    qPos.add(regionId);
512                                    qPos.add(regionId);
513                            }
514    
515                            if (countryId != null) {
516                                    qPos.add(countryId);
517                                    qPos.add(countryId);
518                            }
519    
520                            qPos.add(cities, 2);
521                            qPos.add(zips, 2);
522    
523                            List<Organization> organizations = new ArrayList<Organization>();
524    
525                            Iterator<Long> itr = (Iterator<Long>)QueryUtil.iterate(
526                                    q, getDialect(), start, end);
527    
528                            while (itr.hasNext()) {
529                                    Long organizationId = itr.next();
530    
531                                    Organization organization = OrganizationUtil.findByPrimaryKey(
532                                            organizationId.longValue());
533    
534                                    organizations.add(organization);
535                            }
536    
537                            return organizations;
538                    }
539                    catch (Exception e) {
540                            throw new SystemException(e);
541                    }
542                    finally {
543                            closeSession(session);
544                    }
545            }
546    
547            protected int countByOrganizationId(
548                    Session session, long organizationId,
549                    LinkedHashMap<String, Object> params) {
550    
551                    String sql = CustomSQLUtil.get(COUNT_BY_ORGANIZATION_ID);
552    
553                    sql = StringUtil.replace(sql, "[$JOIN$]", getJoin(params));
554                    sql = StringUtil.replace(sql, "[$WHERE$]", getWhere(params));
555    
556                    SQLQuery q = session.createSQLQuery(sql);
557    
558                    q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
559    
560                    QueryPos qPos = QueryPos.getInstance(q);
561    
562                    setJoin(qPos, params);
563    
564                    qPos.add(organizationId);
565    
566                    Iterator<Long> itr = q.iterate();
567    
568                    if (itr.hasNext()) {
569                            Long count = itr.next();
570    
571                            if (count != null) {
572                                    return count.intValue();
573                            }
574                    }
575    
576                    return 0;
577            }
578    
579            protected String getJoin(LinkedHashMap<String, Object> params) {
580                    if ((params == null) || params.isEmpty()) {
581                            return StringPool.BLANK;
582                    }
583    
584                    StringBundler sb = new StringBundler(params.size());
585    
586                    for (Map.Entry<String, Object> entry : params.entrySet()) {
587                            String key = entry.getKey();
588    
589                            if (key.equals("expandoAttributes")) {
590                                    continue;
591                            }
592    
593                            Object value = entry.getValue();
594    
595                            if (Validator.isNotNull(value)) {
596                                    sb.append(getJoin(key));
597                            }
598                    }
599    
600                    return sb.toString();
601            }
602    
603            protected String getJoin(String key) {
604                    String join = StringPool.BLANK;
605    
606                    if (key.equals("organizationsGroups")) {
607                            join = CustomSQLUtil.get(JOIN_BY_ORGANIZATIONS_GROUPS);
608                    }
609                    else if (key.equals("organizationsPasswordPolicies")) {
610                            join = CustomSQLUtil.get(JOIN_BY_ORGANIZATIONS_PASSWORD_POLICIES);
611                    }
612                    else if (key.equals("organizationsRoles")) {
613                            join = CustomSQLUtil.get(JOIN_BY_ORGANIZATIONS_ROLES);
614                    }
615                    else if (key.equals("organizationsUsers")) {
616                            join = CustomSQLUtil.get(JOIN_BY_ORGANIZATIONS_USERS);
617                    }
618                    else if (key.equals("usersOrgs")) {
619                            join = CustomSQLUtil.get(JOIN_BY_USERS_ORGS);
620                    }
621    
622                    if (Validator.isNotNull(join)) {
623                            int pos = join.indexOf("WHERE");
624    
625                            if (pos != -1) {
626                                    join = join.substring(0, pos);
627                            }
628                    }
629    
630                    return join;
631            }
632    
633            protected String getWhere(LinkedHashMap<String, Object> params) {
634                    if ((params == null) || params.isEmpty()) {
635                            return StringPool.BLANK;
636                    }
637    
638                    StringBundler sb = new StringBundler(params.size());
639    
640                    for (Map.Entry<String, Object> entry : params.entrySet()) {
641                            String key = entry.getKey();
642    
643                            if (key.equals("expandoAttributes")) {
644                                    continue;
645                            }
646    
647                            Object value = entry.getValue();
648    
649                            if (Validator.isNotNull(value)) {
650                                    sb.append(getWhere(key, value));
651                            }
652                    }
653    
654                    return sb.toString();
655            }
656    
657            protected String getWhere(String key) {
658                    return getWhere(key, null);
659            }
660    
661            protected String getWhere(String key, Object value) {
662                    String join = StringPool.BLANK;
663    
664                    if (key.equals("organizations")) {
665                            Long[] organizationIds = (Long[])value;
666    
667                            if (organizationIds.length == 0) {
668                                    join = "WHERE ((Organization_.organizationId = -1) )";
669                            }
670                            else {
671                                    StringBundler sb = new StringBundler(
672                                            organizationIds.length * 2 + 1);
673    
674                                    sb.append("WHERE (");
675    
676                                    for (int i = 0; i < organizationIds.length; i++) {
677                                            sb.append("(Organization_.organizationId = ?) ");
678    
679                                            if ((i + 1) < organizationIds.length) {
680                                                    sb.append("OR ");
681                                            }
682                                    }
683    
684                                    sb.append(StringPool.CLOSE_PARENTHESIS);
685    
686                                    join = sb.toString();
687                            }
688                    }
689                    else if (key.equals("organizationsGroups")) {
690                            if (value instanceof Long) {
691                                    join = CustomSQLUtil.get(JOIN_BY_ORGANIZATIONS_GROUPS);
692                            }
693                            else if (value instanceof Long[]) {
694                                    Long[] organizationGroupIds = (Long[])value;
695    
696                                    if (organizationGroupIds.length == 0) {
697                                            join = "WHERE (Groups_Orgs.groupId = -1)";
698                                    }
699                                    else {
700                                            StringBundler sb = new StringBundler(
701                                                    organizationGroupIds.length * 2 + 1);
702    
703                                            sb.append("WHERE (");
704    
705                                            for (int i = 0; i < organizationGroupIds.length; i++) {
706                                                    sb.append("(Groups_Orgs.groupId = ?) ");
707    
708                                                    if ((i + 1) < organizationGroupIds.length) {
709                                                            sb.append("OR ");
710                                                    }
711                                            }
712    
713                                            sb.append(StringPool.CLOSE_PARENTHESIS);
714    
715                                            join = sb.toString();
716                                    }
717                            }
718                    }
719                    else if (key.equals("organizationsPasswordPolicies")) {
720                            join = CustomSQLUtil.get(JOIN_BY_ORGANIZATIONS_PASSWORD_POLICIES);
721                    }
722                    else if (key.equals("organizationsRoles")) {
723                            join = CustomSQLUtil.get(JOIN_BY_ORGANIZATIONS_ROLES);
724                    }
725                    else if (key.equals("organizationsTree")) {
726                            List<Organization> organizationsTree = (List<Organization>)value;
727    
728                            int size = organizationsTree.size();
729    
730                            if (!organizationsTree.isEmpty()) {
731                                    StringBundler sb = new StringBundler(size * 2 + 1);
732    
733                                    sb.append("WHERE (");
734    
735                                    for (int i = 0; i < size; i++) {
736                                            sb.append("(Organization_.treePath LIKE ?) ");
737    
738                                            if ((i + 1) < size) {
739                                                    sb.append("OR ");
740                                            }
741                                    }
742    
743                                    sb.append(StringPool.CLOSE_PARENTHESIS);
744    
745                                    join = sb.toString();
746                            }
747                    }
748                    else if (key.equals("organizationsUsers")) {
749                            join = CustomSQLUtil.get(JOIN_BY_ORGANIZATIONS_USERS);
750                    }
751                    else if (key.equals("usersOrgs")) {
752                            join = CustomSQLUtil.get(JOIN_BY_USERS_ORGS);
753                    }
754    
755                    if (Validator.isNotNull(join)) {
756                            int pos = join.indexOf("WHERE");
757    
758                            if (pos != -1) {
759                                    join = join.substring(pos + 5, join.length()).concat(" AND ");
760                            }
761                            else {
762                                    join = StringPool.BLANK;
763                            }
764                    }
765    
766                    return join;
767            }
768    
769            protected void setJoin(
770                    QueryPos qPos, LinkedHashMap<String, Object> params) {
771    
772                    if (params == null) {
773                            return;
774                    }
775    
776                    for (Map.Entry<String, Object> entry : params.entrySet()) {
777                            String key = entry.getKey();
778    
779                            if (key.equals("expandoAttributes")) {
780                                    continue;
781                            }
782    
783                            Object value = entry.getValue();
784    
785                            if (key.equals("organizationsTree")) {
786                                    List<Organization> organizationsTree =
787                                            (List<Organization>)value;
788    
789                                    if (!organizationsTree.isEmpty()) {
790                                            for (Organization organization : organizationsTree) {
791                                                    StringBundler sb = new StringBundler(5);
792    
793                                                    sb.append(StringPool.PERCENT);
794                                                    sb.append(StringPool.SLASH);
795                                                    sb.append(organization.getOrganizationId());
796                                                    sb.append(StringPool.SLASH);
797                                                    sb.append(StringPool.PERCENT);
798    
799                                                    qPos.add(sb.toString());
800                                            }
801                                    }
802                            }
803                            else if (value instanceof Long) {
804                                    Long valueLong = (Long)value;
805    
806                                    if (Validator.isNotNull(valueLong)) {
807                                            qPos.add(valueLong);
808                                    }
809                            }
810                            else if (value instanceof Long[]) {
811                                    Long[] valueArray = (Long[])value;
812    
813                                    for (Long element : valueArray) {
814                                            if (Validator.isNotNull(element)) {
815                                                    qPos.add(element);
816                                            }
817                                    }
818                            }
819                            else if (value instanceof Long[][]) {
820                                    Long[][] valueDoubleArray = (Long[][])value;
821    
822                                    for (Long[] valueArray : valueDoubleArray) {
823                                            for (Long valueLong : valueArray) {
824                                                    qPos.add(valueLong);
825                                            }
826                                    }
827                            }
828                            else if (value instanceof String) {
829                                    String valueString = (String)value;
830    
831                                    if (Validator.isNotNull(valueString)) {
832                                            qPos.add(valueString);
833                                    }
834                            }
835                    }
836            }
837    
838            private static final String _COUNTRY_ID_SQL =
839                    "((Organization_.countryId = ?) OR (Address.countryId = ?)) " +
840                            "[$AND_OR_CONNECTOR$]";
841    
842            private static final String _REGION_ID_SQL =
843                    "((Organization_.regionId = ?) OR (Address.regionId = ?)) " +
844                            "[$AND_OR_CONNECTOR$]";
845    
846    }