1
22
23 package com.liferay.portlet.blogs.service.impl;
24
25 import com.liferay.counter.service.CounterLocalServiceUtil;
26 import com.liferay.portal.PortalException;
27 import com.liferay.portal.SystemException;
28 import com.liferay.portal.kernel.util.Validator;
29 import com.liferay.portal.model.User;
30 import com.liferay.portal.model.impl.ResourceImpl;
31 import com.liferay.portal.service.ResourceLocalServiceUtil;
32 import com.liferay.portal.service.persistence.UserUtil;
33 import com.liferay.portlet.blogs.CategoryNameException;
34 import com.liferay.portlet.blogs.model.BlogsCategory;
35 import com.liferay.portlet.blogs.model.BlogsEntry;
36 import com.liferay.portlet.blogs.model.impl.BlogsCategoryImpl;
37 import com.liferay.portlet.blogs.service.base.BlogsCategoryLocalServiceBaseImpl;
38 import com.liferay.portlet.blogs.service.persistence.BlogsCategoryUtil;
39 import com.liferay.portlet.blogs.service.persistence.BlogsEntryUtil;
40 import com.liferay.portlet.blogs.util.Indexer;
41
42 import java.io.IOException;
43
44 import java.util.Date;
45 import java.util.Iterator;
46 import java.util.List;
47
48 import org.apache.commons.logging.Log;
49 import org.apache.commons.logging.LogFactory;
50
51
58 public class BlogsCategoryLocalServiceImpl
59 extends BlogsCategoryLocalServiceBaseImpl {
60
61 public BlogsCategory addCategory(
62 long userId, long parentCategoryId, String name, String description,
63 boolean addCommunityPermissions, boolean addGuestPermissions)
64 throws PortalException, SystemException {
65
66 return addCategory(
67 userId, parentCategoryId, name, description,
68 Boolean.valueOf(addCommunityPermissions),
69 Boolean.valueOf(addGuestPermissions), null, null);
70 }
71
72 public BlogsCategory addCategory(
73 long userId, long parentCategoryId, String name, String description,
74 String[] communityPermissions, String[] guestPermissions)
75 throws PortalException, SystemException {
76
77 return addCategory(
78 userId, parentCategoryId, name, description, null, null,
79 communityPermissions, guestPermissions);
80 }
81
82 public BlogsCategory addCategory(
83 long userId, long parentCategoryId, String name, String description,
84 Boolean addCommunityPermissions, Boolean addGuestPermissions,
85 String[] communityPermissions, String[] guestPermissions)
86 throws PortalException, SystemException {
87
88
90 User user = UserUtil.findByPrimaryKey(userId);
91 Date now = new Date();
92
93 validate(name);
94
95 long categoryId = CounterLocalServiceUtil.increment();
96
97 BlogsCategory category = BlogsCategoryUtil.create(categoryId);
98
99 category.setCompanyId(user.getCompanyId());
100 category.setUserId(user.getUserId());
101 category.setUserName(user.getFullName());
102 category.setCreateDate(now);
103 category.setModifiedDate(now);
104 category.setParentCategoryId(parentCategoryId);
105 category.setName(name);
106 category.setDescription(description);
107
108 BlogsCategoryUtil.update(category);
109
110
112 if ((addCommunityPermissions != null) &&
113 (addGuestPermissions != null)) {
114
115 addCategoryResources(
116 category, addCommunityPermissions.booleanValue(),
117 addGuestPermissions.booleanValue());
118 }
119 else {
120 addCategoryResources(
121 category, communityPermissions, guestPermissions);
122 }
123
124 return category;
125 }
126
127 public void addCategoryResources(
128 long categoryId, boolean addCommunityPermissions,
129 boolean addGuestPermissions)
130 throws PortalException, SystemException {
131
132 BlogsCategory category = BlogsCategoryUtil.findByPrimaryKey(categoryId);
133
134 addCategoryResources(
135 category, addCommunityPermissions, addGuestPermissions);
136 }
137
138 public void addCategoryResources(
139 BlogsCategory category, boolean addCommunityPermissions,
140 boolean addGuestPermissions)
141 throws PortalException, SystemException {
142
143 ResourceLocalServiceUtil.addResources(
144 category.getCompanyId(), 0, category.getUserId(),
145 BlogsCategory.class.getName(), category.getCategoryId(), false,
146 addCommunityPermissions, addGuestPermissions);
147 }
148
149 public void addCategoryResources(
150 long categoryId, String[] communityPermissions,
151 String[] guestPermissions)
152 throws PortalException, SystemException {
153
154 BlogsCategory category = BlogsCategoryUtil.findByPrimaryKey(categoryId);
155
156 addCategoryResources(category, communityPermissions, guestPermissions);
157 }
158
159 public void addCategoryResources(
160 BlogsCategory category, String[] communityPermissions,
161 String[] guestPermissions)
162 throws PortalException, SystemException {
163
164 ResourceLocalServiceUtil.addModelResources(
165 category.getCompanyId(), 0, category.getUserId(),
166 BlogsCategory.class.getName(), category.getCategoryId(),
167 communityPermissions, guestPermissions);
168 }
169
170 public void deleteCategory(long categoryId)
171 throws PortalException, SystemException {
172
173 BlogsCategory category = BlogsCategoryUtil.findByPrimaryKey(categoryId);
174
175 deleteCategory(category);
176 }
177
178 public void deleteCategory(BlogsCategory category)
179 throws PortalException, SystemException {
180
181
183 Iterator itr = BlogsCategoryUtil.findByParentCategoryId(
184 category.getCategoryId()).iterator();
185
186 while (itr.hasNext()) {
187 BlogsCategory curCategory = (BlogsCategory)itr.next();
188
189 deleteCategory(curCategory);
190 }
191
192
194 itr = BlogsEntryUtil.findByCategoryId(
195 category.getCategoryId()).iterator();
196
197 while (itr.hasNext()) {
198
199
201 BlogsEntry entry = (BlogsEntry)itr.next();
202
203 entry.setCategoryId(BlogsCategoryImpl.DEFAULT_PARENT_CATEGORY_ID);
204
205 BlogsEntryUtil.update(entry);
206
207
209 try {
210 Indexer.updateEntry(
211 entry.getCompanyId(), entry.getGroupId(), entry.getUserId(),
212 category.getCategoryId(), entry.getEntryId(),
213 entry.getTitle(), entry.getContent());
214 }
215 catch (IOException ioe) {
216 _log.error("Indexing " + entry.getEntryId(), ioe);
217 }
218 }
219
220
222 ResourceLocalServiceUtil.deleteResource(
223 category.getCompanyId(), BlogsCategory.class.getName(),
224 ResourceImpl.SCOPE_INDIVIDUAL, category.getCategoryId());
225
226
228 BlogsCategoryUtil.remove(category.getCategoryId());
229 }
230
231 public List getCategories(long parentCategoryId, int begin, int end)
232 throws SystemException {
233
234 return BlogsCategoryUtil.findByParentCategoryId(
235 parentCategoryId, begin, end);
236 }
237
238 public int getCategoriesCount(long parentCategoryId)
239 throws SystemException {
240
241 return BlogsCategoryUtil.countByParentCategoryId(parentCategoryId);
242 }
243
244 public BlogsCategory getCategory(long categoryId)
245 throws PortalException, SystemException {
246
247 return BlogsCategoryUtil.findByPrimaryKey(categoryId);
248 }
249
250 public void getSubcategoryIds(List categoryIds, long categoryId)
251 throws SystemException {
252
253 Iterator itr = BlogsCategoryUtil.findByParentCategoryId(
254 categoryId).iterator();
255
256 while (itr.hasNext()) {
257 BlogsCategory category = (BlogsCategory)itr.next();
258
259 categoryIds.add(new Long(category.getCategoryId()));
260
261 getSubcategoryIds(categoryIds, category.getCategoryId());
262 }
263 }
264
265 public BlogsCategory updateCategory(
266 long categoryId, long parentCategoryId, String name,
267 String description)
268 throws PortalException, SystemException {
269
270 validate(name);
271
272 BlogsCategory category = BlogsCategoryUtil.findByPrimaryKey(categoryId);
273
274 category.setModifiedDate(new Date());
275 category.setParentCategoryId(parentCategoryId);
276 category.setName(name);
277 category.setDescription(description);
278
279 BlogsCategoryUtil.update(category);
280
281 return category;
282 }
283
284 protected void validate(String name) throws PortalException {
285 if (Validator.isNull(name)) {
286 throw new CategoryNameException();
287 }
288 }
289
290 private static Log _log =
291 LogFactory.getLog(BlogsCategoryLocalServiceImpl.class);
292
293 }