1
22
23 package com.liferay.portlet.blogs.service.persistence;
24
25 import com.liferay.portal.SystemException;
26 import com.liferay.portal.kernel.dao.orm.QueryPos;
27 import com.liferay.portal.kernel.dao.orm.QueryUtil;
28 import com.liferay.portal.kernel.dao.orm.SQLQuery;
29 import com.liferay.portal.kernel.dao.orm.Session;
30 import com.liferay.portal.kernel.dao.orm.Type;
31 import com.liferay.portal.kernel.util.StringUtil;
32 import com.liferay.portal.service.persistence.impl.BasePersistenceImpl;
33 import com.liferay.portlet.blogs.model.BlogsEntry;
34 import com.liferay.portlet.blogs.model.impl.BlogsEntryImpl;
35 import com.liferay.util.dao.orm.CustomSQLUtil;
36
37 import java.util.ArrayList;
38 import java.util.Iterator;
39 import java.util.List;
40
41
47 public class BlogsEntryFinderImpl
48 extends BasePersistenceImpl implements BlogsEntryFinder {
49
50 public static String COUNT_BY_ORGANIZATION_IDS =
51 BlogsEntryFinder.class.getName() + ".countByOrganizationIds";
52
53 public static String FIND_BY_ORGANIZATION_IDS =
54 BlogsEntryFinder.class.getName() + ".findByOrganizationIds";
55
56 public static String FIND_BY_NO_ASSETS =
57 BlogsEntryFinder.class.getName() + ".findByNoAssets";
58
59 public int countByOrganizationId(long organizationId, boolean draft)
60 throws SystemException {
61
62 List<Long> organizationIds = new ArrayList<Long>();
63
64 organizationIds.add(organizationId);
65
66 return countByOrganizationIds(organizationIds, draft);
67 }
68
69 public int countByOrganizationIds(
70 List<Long> organizationIds, boolean draft)
71 throws SystemException {
72
73 Session session = null;
74
75 try {
76 session = openSession();
77
78 String sql = CustomSQLUtil.get(COUNT_BY_ORGANIZATION_IDS);
79
80 sql = StringUtil.replace(
81 sql, "[$ORGANIZATION_ID$]",
82 getOrganizationIds(organizationIds));
83
84 SQLQuery q = session.createSQLQuery(sql);
85
86 q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
87
88 QueryPos qPos = QueryPos.getInstance(q);
89
90 for (int i = 0; i < organizationIds.size(); i++) {
91 Long organizationId = organizationIds.get(i);
92
93 qPos.add(organizationId);
94 }
95
96 qPos.add(draft);
97
98 Iterator<Long> itr = q.list().iterator();
99
100 if (itr.hasNext()) {
101 Long count = itr.next();
102
103 if (count != null) {
104 return count.intValue();
105 }
106 }
107
108 return 0;
109 }
110 catch (Exception e) {
111 throw new SystemException(e);
112 }
113 finally {
114 closeSession(session);
115 }
116 }
117
118 public List<BlogsEntry> findByOrganizationId(
119 long organizationId, boolean draft, int start, int end)
120 throws SystemException {
121
122 List<Long> organizationIds = new ArrayList<Long>();
123
124 organizationIds.add(organizationId);
125
126 return findByOrganizationIds(organizationIds, draft, start, end);
127 }
128
129 public List<BlogsEntry> findByOrganizationIds(
130 List<Long> organizationIds, boolean draft, int start, int end)
131 throws SystemException {
132
133 Session session = null;
134
135 try {
136 session = openSession();
137
138 String sql = CustomSQLUtil.get(FIND_BY_ORGANIZATION_IDS);
139
140 sql = StringUtil.replace(
141 sql, "[$ORGANIZATION_ID$]",
142 getOrganizationIds(organizationIds));
143
144 SQLQuery q = session.createSQLQuery(sql);
145
146 q.addEntity("BlogsEntry", BlogsEntryImpl.class);
147
148 QueryPos qPos = QueryPos.getInstance(q);
149
150 for (int i = 0; i < organizationIds.size(); i++) {
151 Long organizationId = organizationIds.get(i);
152
153 qPos.add(organizationId);
154 }
155
156 qPos.add(draft);
157
158 return (List<BlogsEntry>)QueryUtil.list(
159 q, getDialect(), start, end);
160 }
161 catch (Exception e) {
162 throw new SystemException(e);
163 }
164 finally {
165 closeSession(session);
166 }
167 }
168
169 public List<BlogsEntry> findByNoAssets() throws SystemException {
170 Session session = null;
171
172 try {
173 session = openSession();
174
175 String sql = CustomSQLUtil.get(FIND_BY_NO_ASSETS);
176
177 SQLQuery q = session.createSQLQuery(sql);
178
179 q.addEntity("BlogsEntry", BlogsEntryImpl.class);
180
181 return q.list();
182 }
183 catch (Exception e) {
184 throw new SystemException(e);
185 }
186 finally {
187 closeSession(session);
188 }
189 }
190
191 protected String getOrganizationIds(List<Long> organizationIds) {
192 StringBuilder sb = new StringBuilder();
193
194 for (int i = 0; i < organizationIds.size(); i++) {
195 sb.append("Users_Orgs.organizationId = ? ");
196
197 if ((i + 1) != organizationIds.size()) {
198 sb.append("OR ");
199 }
200 }
201
202 return sb.toString();
203 }
204
205 }