001    /**
002     * Copyright (c) 2000-present 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.impl;
016    
017    import com.liferay.portal.exception.NoSuchPasswordPolicyRelException;
018    import com.liferay.portal.kernel.exception.PortalException;
019    import com.liferay.portal.kernel.util.StringBundler;
020    import com.liferay.portal.kernel.util.StringPool;
021    import com.liferay.portal.model.PasswordPolicyRel;
022    import com.liferay.portal.service.base.PasswordPolicyRelLocalServiceBaseImpl;
023    
024    import java.util.List;
025    
026    /**
027     * @author Scott Lee
028     * @author Shuyang Zhou
029     */
030    public class PasswordPolicyRelLocalServiceImpl
031            extends PasswordPolicyRelLocalServiceBaseImpl {
032    
033            @Override
034            public PasswordPolicyRel addPasswordPolicyRel(
035                    long passwordPolicyId, String className, long classPK) {
036    
037                    long classNameId = classNameLocalService.getClassNameId(className);
038    
039                    PasswordPolicyRel passwordPolicyRel =
040                            passwordPolicyRelPersistence.fetchByC_C(classNameId, classPK);
041    
042                    if (passwordPolicyRel != null) {
043                            if (passwordPolicyRel.getPasswordPolicyId() == passwordPolicyId) {
044                                    return null;
045                            }
046    
047                            passwordPolicyRelPersistence.remove(passwordPolicyRel);
048                    }
049    
050                    long passwordPolicyRelId = counterLocalService.increment();
051    
052                    passwordPolicyRel = passwordPolicyRelPersistence.create(
053                            passwordPolicyRelId);
054    
055                    passwordPolicyRel.setPasswordPolicyId(passwordPolicyId);
056                    passwordPolicyRel.setClassNameId(classNameId);
057                    passwordPolicyRel.setClassPK(classPK);
058    
059                    passwordPolicyRelPersistence.update(passwordPolicyRel);
060    
061                    return passwordPolicyRel;
062            }
063    
064            @Override
065            public void addPasswordPolicyRels(
066                    long passwordPolicyId, String className, long[] classPKs) {
067    
068                    for (int i = 0; i < classPKs.length; i++) {
069                            addPasswordPolicyRel(passwordPolicyId, className, classPKs[i]);
070                    }
071            }
072    
073            @Override
074            public void deletePasswordPolicyRel(
075                    long passwordPolicyId, String className, long classPK) {
076    
077                    long classNameId = classNameLocalService.getClassNameId(className);
078    
079                    PasswordPolicyRel passwordPolicyRel =
080                            passwordPolicyRelPersistence.fetchByC_C(classNameId, classPK);
081    
082                    if ((passwordPolicyRel != null) &&
083                            (passwordPolicyRel.getPasswordPolicyId() == passwordPolicyId)) {
084    
085                            passwordPolicyRelPersistence.remove(passwordPolicyRel);
086                    }
087            }
088    
089            @Override
090            public void deletePasswordPolicyRel(String className, long classPK) {
091                    try {
092                            long classNameId = classNameLocalService.getClassNameId(className);
093    
094                            PasswordPolicyRel passwordPolicyRel =
095                                    passwordPolicyRelPersistence.findByC_C(classNameId, classPK);
096    
097                            deletePasswordPolicyRel(passwordPolicyRel);
098                    }
099                    catch (NoSuchPasswordPolicyRelException nsppre) {
100                    }
101            }
102    
103            @Override
104            public void deletePasswordPolicyRels(long passwordPolicyId) {
105                    List<PasswordPolicyRel> passwordPolicyRels =
106                            passwordPolicyRelPersistence.findByPasswordPolicyId(
107                                    passwordPolicyId);
108    
109                    for (PasswordPolicyRel passwordPolicyRel : passwordPolicyRels) {
110                            deletePasswordPolicyRel(passwordPolicyRel);
111                    }
112            }
113    
114            @Override
115            public void deletePasswordPolicyRels(
116                    long passwordPolicyId, String className, long[] classPKs) {
117    
118                    for (int i = 0; i < classPKs.length; i++) {
119                            deletePasswordPolicyRel(passwordPolicyId, className, classPKs[i]);
120                    }
121            }
122    
123            @Override
124            public PasswordPolicyRel fetchPasswordPolicyRel(
125                    String className, long classPK) {
126    
127                    long classNameId = classNameLocalService.getClassNameId(className);
128    
129                    return passwordPolicyRelPersistence.fetchByC_C(classNameId, classPK);
130            }
131    
132            @Override
133            public PasswordPolicyRel getPasswordPolicyRel(
134                            long passwordPolicyId, String className, long classPK)
135                    throws PortalException {
136    
137                    long classNameId = classNameLocalService.getClassNameId(className);
138    
139                    PasswordPolicyRel passwordPolicyRel =
140                            passwordPolicyRelPersistence.fetchByC_C(classNameId, classPK);
141    
142                    if ((passwordPolicyRel != null) &&
143                            (passwordPolicyRel.getPasswordPolicyId() == passwordPolicyId)) {
144    
145                            return passwordPolicyRel;
146                    }
147    
148                    StringBundler sb = new StringBundler(8);
149    
150                    sb.append("No PasswordPolicyRel exists with the key {");
151                    sb.append("passwordPolicyId=");
152                    sb.append(passwordPolicyId);
153                    sb.append(", classNameId=");
154                    sb.append(classNameId);
155                    sb.append(", classPK=");
156                    sb.append(classPK);
157                    sb.append(StringPool.CLOSE_CURLY_BRACE);
158    
159                    throw new NoSuchPasswordPolicyRelException(sb.toString());
160            }
161    
162            @Override
163            public PasswordPolicyRel getPasswordPolicyRel(
164                            String className, long classPK)
165                    throws PortalException {
166    
167                    long classNameId = classNameLocalService.getClassNameId(className);
168    
169                    return passwordPolicyRelPersistence.findByC_C(classNameId, classPK);
170            }
171    
172            @Override
173            public boolean hasPasswordPolicyRel(
174                    long passwordPolicyId, String className, long classPK) {
175    
176                    long classNameId = classNameLocalService.getClassNameId(className);
177    
178                    PasswordPolicyRel passwordPolicyRel =
179                            passwordPolicyRelPersistence.fetchByC_C(classNameId, classPK);
180    
181                    if ((passwordPolicyRel != null) &&
182                            (passwordPolicyRel.getPasswordPolicyId() == passwordPolicyId)) {
183    
184                            return true;
185                    }
186                    else {
187                            return false;
188                    }
189            }
190    
191    }