001
014
015 package com.liferay.portal.service.persistence;
016
017 import com.liferay.portal.kernel.dao.orm.FinderCacheUtil;
018 import com.liferay.portal.kernel.dao.orm.FinderPath;
019 import com.liferay.portal.kernel.dao.orm.QueryPos;
020 import com.liferay.portal.kernel.dao.orm.SQLQuery;
021 import com.liferay.portal.kernel.dao.orm.Session;
022 import com.liferay.portal.kernel.dao.orm.Type;
023 import com.liferay.portal.kernel.exception.SystemException;
024 import com.liferay.portal.kernel.util.GetterUtil;
025 import com.liferay.portal.kernel.util.StringPool;
026 import com.liferay.portal.kernel.util.StringUtil;
027 import com.liferay.portal.model.PortletConstants;
028 import com.liferay.portal.model.PortletPreferences;
029 import com.liferay.portal.model.impl.PortletPreferencesImpl;
030 import com.liferay.portal.model.impl.PortletPreferencesModelImpl;
031 import com.liferay.portal.service.persistence.impl.BasePersistenceImpl;
032 import com.liferay.util.dao.orm.CustomSQLUtil;
033
034 import java.util.Iterator;
035 import java.util.List;
036
037
041 public class PortletPreferencesFinderImpl
042 extends BasePersistenceImpl<PortletPreferences>
043 implements PortletPreferencesFinder {
044
045 public static final String COUNT_BY_O_O_P =
046 PortletPreferencesFinder.class.getName() + ".countByO_O_P";
047
048 public static final String COUNT_BY_O_O_P_P_P =
049 PortletPreferencesFinder.class.getName() + ".countByO_O_P_P_P";
050
051 public static final String FIND_BY_PORTLET_ID =
052 PortletPreferencesFinder.class.getName() + ".findByPortletId";
053
054 public static final String FIND_BY_C_G_O_O_P_P =
055 PortletPreferencesFinder.class.getName() + ".findByC_G_O_O_P_P";
056
057 public static final FinderPath FINDER_PATH_FIND_BY_C_G_O_O_P_P =
058 new FinderPath(
059 PortletPreferencesModelImpl.ENTITY_CACHE_ENABLED,
060 PortletPreferencesModelImpl.FINDER_CACHE_ENABLED,
061 PortletPreferencesImpl.class,
062 PortletPreferencesPersistenceImpl.
063 FINDER_CLASS_NAME_LIST_WITH_PAGINATION,
064 "findByC_G_O_O_P_P",
065 new String[] {
066 Long.class.getName(), Long.class.getName(),
067 Long.class.getName(), Integer.class.getName(),
068 String.class.getName(), Boolean.class.getName()
069 }
070 );
071
072 @Override
073 public long countByO_O_P(
074 long ownerId, int ownerType, String portletId,
075 boolean excludeDefaultPreferences)
076 throws SystemException {
077
078 Session session = null;
079
080 try {
081 session = openSession();
082
083 String sql = CustomSQLUtil.get(COUNT_BY_O_O_P);
084
085 if (ownerId == -1) {
086 sql = StringUtil.replace(sql, _OWNER_ID_SQL, StringPool.BLANK);
087 }
088
089 if (!excludeDefaultPreferences) {
090 sql = StringUtil.replace(
091 sql, _PREFERENCES_SQL, StringPool.BLANK);
092 }
093
094 SQLQuery q = session.createSQLQuery(sql);
095
096 q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
097
098 QueryPos qPos = QueryPos.getInstance(q);
099
100 if (ownerId != -1) {
101 qPos.add(ownerId);
102 }
103
104 qPos.add(ownerType);
105 qPos.add(portletId);
106 qPos.add(portletId.concat("%_INSTANCE_%"));
107
108 int count = 0;
109
110 Iterator<Long> itr = q.iterate();
111
112 while (itr.hasNext()) {
113 Long l = itr.next();
114
115 if (l != null) {
116 count += l.intValue();
117 }
118 }
119
120 return count;
121 }
122 catch (Exception e) {
123 throw new SystemException(e);
124 }
125 finally {
126 closeSession(session);
127 }
128 }
129
130 @Override
131 public long countByO_O_P_P_P(
132 long ownerId, int ownerType, long plid, String portletId,
133 boolean excludeDefaultPreferences)
134 throws SystemException {
135
136 Session session = null;
137
138 try {
139 session = openSession();
140
141 String sql = CustomSQLUtil.get(COUNT_BY_O_O_P_P_P);
142
143 if (ownerId == -1) {
144 sql = StringUtil.replace(sql, _OWNER_ID_SQL, StringPool.BLANK);
145 }
146
147 if (plid == -1) {
148 sql = StringUtil.replace(sql, _PLID_SQL, StringPool.BLANK);
149 }
150 else {
151 sql = StringUtil.replace(
152 sql, _PORTLET_ID_INSTANCE_SQL, StringPool.BLANK);
153 }
154
155 if (excludeDefaultPreferences) {
156 sql = StringUtil.replace(
157 sql, "[$PORTLET_PREFERENCES_PREFERENCES_DEFAULT$]",
158 PortletConstants.DEFAULT_PREFERENCES);
159 }
160 else {
161 sql = StringUtil.replace(
162 sql, _PREFERENCES_SQL, StringPool.BLANK);
163 }
164
165 SQLQuery q = session.createSQLQuery(sql);
166
167 q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
168
169 QueryPos qPos = QueryPos.getInstance(q);
170
171 if (ownerId != -1) {
172 qPos.add(ownerId);
173 }
174
175 qPos.add(ownerType);
176 qPos.add(portletId);
177
178 if (plid != -1) {
179 qPos.add(plid);
180 }
181 else {
182 qPos.add(portletId.concat("%_INSTANCE_%"));
183 }
184
185 int count = 0;
186
187 Iterator<Long> itr = q.iterate();
188
189 while (itr.hasNext()) {
190 Long l = itr.next();
191
192 if (l != null) {
193 count += l.intValue();
194 }
195 }
196
197 return count;
198 }
199 catch (Exception e) {
200 throw new SystemException(e);
201 }
202 finally {
203 closeSession(session);
204 }
205 }
206
207 @Override
208 public List<PortletPreferences> findByPortletId(String portletId)
209 throws SystemException {
210
211 Session session = null;
212
213 try {
214 session = openSession();
215
216 String sql = CustomSQLUtil.get(FIND_BY_PORTLET_ID);
217
218 SQLQuery q = session.createSQLQuery(sql);
219
220 q.addEntity("PortletPreferences", PortletPreferencesImpl.class);
221
222 QueryPos qPos = QueryPos.getInstance(q);
223
224 qPos.add(portletId);
225
226 return q.list(true);
227 }
228 catch (Exception e) {
229 throw new SystemException(e);
230 }
231 finally {
232 closeSession(session);
233 }
234 }
235
236 @Override
237 public List<PortletPreferences> findByC_G_O_O_P_P(
238 long companyId, long groupId, long ownerId, int ownerType,
239 String portletId, boolean privateLayout)
240 throws SystemException {
241
242 Object[] finderArgs = {
243 companyId, groupId, ownerId, ownerType, portletId, privateLayout
244 };
245
246 List<PortletPreferences> list =
247 (List<PortletPreferences>)FinderCacheUtil.getResult(
248 FINDER_PATH_FIND_BY_C_G_O_O_P_P, finderArgs, this);
249
250 if ((list != null) && !list.isEmpty()) {
251 for (PortletPreferences portletPreferences : list) {
252 if ((ownerId != portletPreferences.getOwnerId()) ||
253 (ownerType != portletPreferences.getOwnerType()) ||
254 !isInstanceOf(
255 portletPreferences.getPortletId(), portletId)) {
256
257 list = null;
258
259 break;
260 }
261 }
262 }
263
264 if (list == null) {
265 Session session = null;
266
267 try {
268 session = openSession();
269
270 String sql = CustomSQLUtil.get(FIND_BY_C_G_O_O_P_P);
271
272 SQLQuery q = session.createSQLQuery(sql);
273
274 q.addEntity("PortletPreferences", PortletPreferencesImpl.class);
275
276 QueryPos qPos = QueryPos.getInstance(q);
277
278 qPos.add(companyId);
279 qPos.add(groupId);
280 qPos.add(ownerId);
281 qPos.add(ownerType);
282 qPos.add(portletId);
283 qPos.add(portletId.concat("_INSTANCE_%"));
284 qPos.add(privateLayout);
285
286 list = q.list(true);
287
288 PortletPreferencesUtil.cacheResult(list);
289
290 FinderCacheUtil.putResult(
291 FINDER_PATH_FIND_BY_C_G_O_O_P_P, finderArgs, list);
292 }
293 catch (Exception e) {
294 FinderCacheUtil.removeResult(
295 FINDER_PATH_FIND_BY_C_G_O_O_P_P, finderArgs);
296
297 throw new SystemException(e);
298 }
299 finally {
300 closeSession(session);
301 }
302 }
303
304 return list;
305 }
306
307 protected boolean isInstanceOf(
308 String portletPreferencesPortletId, String portletId) {
309
310 portletPreferencesPortletId = GetterUtil.getString(
311 portletPreferencesPortletId);
312 portletId = GetterUtil.getString(portletId);
313
314 if (portletPreferencesPortletId.equals(portletId)) {
315 return true;
316 }
317
318 if (portletPreferencesPortletId.startsWith(
319 portletId.concat(PortletConstants.INSTANCE_SEPARATOR))) {
320
321 return true;
322 }
323
324 return false;
325 }
326
327 private static final String _OWNER_ID_SQL =
328 "(PortletPreferences.ownerId = ?) AND";
329
330 private static final String _PLID_SQL = "AND (PortletPreferences.plid = ?)";
331
332 private static final String _PORTLET_ID_INSTANCE_SQL =
333 "OR (PortletPreferences.portletId LIKE ?)";
334
335 private static final String _PREFERENCES_SQL =
336 "AND (PortletPreferences.preferences != " +
337 "'[$PORTLET_PREFERENCES_PREFERENCES_DEFAULT$]')";
338
339 }