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_C_G_O_O_P_P_P =
049 PortletPreferencesFinder.class.getName() + ".countByC_G_O_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 countByC_G_O_O_P_P_P(
132 long companyId, long groupId, long ownerId, int ownerType,
133 long plid, String portletId, boolean privateLayout,
134 boolean excludeDefaultPreferences)
135 throws SystemException {
136
137 Session session = null;
138
139 try {
140 session = openSession();
141
142 String sql = CustomSQLUtil.get(COUNT_BY_C_G_O_O_P_P_P);
143
144 if (ownerId == -1) {
145 sql = StringUtil.replace(sql, _OWNER_ID_SQL, StringPool.BLANK);
146 }
147
148 if (plid == -1) {
149 sql = StringUtil.replace(sql, _PLID_SQL, StringPool.BLANK);
150 }
151 else {
152 sql = StringUtil.replace(
153 sql, _PORTLET_ID_INSTANCE_SQL, StringPool.BLANK);
154
155 sql = StringUtil.replace(
156 sql, _PRIVATE_LAYOUT_SQL, StringPool.BLANK);
157 }
158
159 if (!excludeDefaultPreferences) {
160 sql = StringUtil.replace(
161 sql, _PREFERENCES_SQL, StringPool.BLANK);
162 }
163
164 SQLQuery q = session.createSQLQuery(sql);
165
166 q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
167
168 QueryPos qPos = QueryPos.getInstance(q);
169
170 qPos.add(companyId);
171 qPos.add(groupId);
172
173 if (ownerId != -1) {
174 qPos.add(ownerId);
175 }
176
177 qPos.add(ownerType);
178 qPos.add(portletId);
179
180 if (plid != -1) {
181 qPos.add(plid);
182 }
183 else {
184 qPos.add(portletId.concat("%_INSTANCE_%"));
185 qPos.add(privateLayout);
186 }
187
188 int count = 0;
189
190 Iterator<Long> itr = q.iterate();
191
192 while (itr.hasNext()) {
193 Long l = itr.next();
194
195 if (l != null) {
196 count += l.intValue();
197 }
198 }
199
200 return count;
201 }
202 catch (Exception e) {
203 throw new SystemException(e);
204 }
205 finally {
206 closeSession(session);
207 }
208 }
209
210 @Override
211 public List<PortletPreferences> findByPortletId(String portletId)
212 throws SystemException {
213
214 Session session = null;
215
216 try {
217 session = openSession();
218
219 String sql = CustomSQLUtil.get(FIND_BY_PORTLET_ID);
220
221 SQLQuery q = session.createSQLQuery(sql);
222
223 q.addEntity("PortletPreferences", PortletPreferencesImpl.class);
224
225 QueryPos qPos = QueryPos.getInstance(q);
226
227 qPos.add(portletId);
228
229 return q.list(true);
230 }
231 catch (Exception e) {
232 throw new SystemException(e);
233 }
234 finally {
235 closeSession(session);
236 }
237 }
238
239 @Override
240 public List<PortletPreferences> findByC_G_O_O_P_P(
241 long companyId, long groupId, long ownerId, int ownerType,
242 String portletId, boolean privateLayout)
243 throws SystemException {
244
245 Object[] finderArgs = {
246 companyId, groupId, ownerId, ownerType, portletId, privateLayout
247 };
248
249 List<PortletPreferences> list =
250 (List<PortletPreferences>)FinderCacheUtil.getResult(
251 FINDER_PATH_FIND_BY_C_G_O_O_P_P, finderArgs, this);
252
253 if ((list != null) && !list.isEmpty()) {
254 for (PortletPreferences portletPreferences : list) {
255 if ((ownerId != portletPreferences.getOwnerId()) ||
256 (ownerType != portletPreferences.getOwnerType()) ||
257 !isInstanceOf(
258 portletPreferences.getPortletId(), portletId)) {
259
260 list = null;
261
262 break;
263 }
264 }
265 }
266
267 if (list == null) {
268 Session session = null;
269
270 try {
271 session = openSession();
272
273 String sql = CustomSQLUtil.get(FIND_BY_C_G_O_O_P_P);
274
275 SQLQuery q = session.createSQLQuery(sql);
276
277 q.addEntity("PortletPreferences", PortletPreferencesImpl.class);
278
279 QueryPos qPos = QueryPos.getInstance(q);
280
281 qPos.add(companyId);
282 qPos.add(groupId);
283 qPos.add(ownerId);
284 qPos.add(ownerType);
285 qPos.add(portletId);
286 qPos.add(portletId.concat("_INSTANCE_%"));
287 qPos.add(privateLayout);
288
289 list = q.list(true);
290
291 PortletPreferencesUtil.cacheResult(list);
292
293 FinderCacheUtil.putResult(
294 FINDER_PATH_FIND_BY_C_G_O_O_P_P, finderArgs, list);
295 }
296 catch (Exception e) {
297 FinderCacheUtil.removeResult(
298 FINDER_PATH_FIND_BY_C_G_O_O_P_P, finderArgs);
299
300 throw new SystemException(e);
301 }
302 finally {
303 closeSession(session);
304 }
305 }
306
307 return list;
308 }
309
310 protected boolean isInstanceOf(
311 String portletPreferencesPortletId, String portletId) {
312
313 portletPreferencesPortletId = GetterUtil.getString(
314 portletPreferencesPortletId);
315 portletId = GetterUtil.getString(portletId);
316
317 if (portletPreferencesPortletId.equals(portletId)) {
318 return true;
319 }
320
321 if (portletPreferencesPortletId.startsWith(
322 portletId.concat(PortletConstants.INSTANCE_SEPARATOR))) {
323
324 return true;
325 }
326
327 return false;
328 }
329
330 private static final String _OWNER_ID_SQL =
331 "(PortletPreferences.ownerId = ?) AND";
332
333 private static final String _PLID_SQL = "(Layout.plid = ?) AND";
334
335 private static final String _PORTLET_ID_INSTANCE_SQL =
336 "OR (PortletPreferences.portletId LIKE ?)";
337
338 private static final String _PREFERENCES_SQL =
339 "AND PortletPreferences.preferences != " +
340 "'[$PORTLET_PREFERENCES_PREFERENCES_DEFAULT$]'";
341
342 private static final String _PRIVATE_LAYOUT_SQL =
343 "(Layout.privateLayout = ?) AND";
344
345 }