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