001
014
015 package com.liferay.portlet.dynamicdatamapping.service.impl;
016
017 import com.liferay.portal.kernel.exception.PortalException;
018 import com.liferay.portal.kernel.exception.SystemException;
019 import com.liferay.portal.kernel.util.LocaleUtil;
020 import com.liferay.portal.kernel.util.OrderByComparator;
021 import com.liferay.portal.kernel.util.Validator;
022 import com.liferay.portal.model.ResourceConstants;
023 import com.liferay.portal.model.User;
024 import com.liferay.portal.service.ServiceContext;
025 import com.liferay.portlet.dynamicdatamapping.TemplateDuplicateTemplateKeyException;
026 import com.liferay.portlet.dynamicdatamapping.TemplateNameException;
027 import com.liferay.portlet.dynamicdatamapping.TemplateScriptException;
028 import com.liferay.portlet.dynamicdatamapping.model.DDMTemplate;
029 import com.liferay.portlet.dynamicdatamapping.service.base.DDMTemplateLocalServiceBaseImpl;
030
031 import java.util.ArrayList;
032 import java.util.Date;
033 import java.util.List;
034 import java.util.Locale;
035 import java.util.Map;
036
037
041 public class DDMTemplateLocalServiceImpl
042 extends DDMTemplateLocalServiceBaseImpl {
043
044 public DDMTemplate addTemplate(
045 long userId, long groupId, long classNameId, long classPK,
046 String templateKey, Map<Locale, String> nameMap,
047 Map<Locale, String> descriptionMap, String type, String mode,
048 String language, String script, boolean cacheable,
049 ServiceContext serviceContext)
050 throws PortalException, SystemException {
051
052
053
054 User user = userPersistence.findByPrimaryKey(userId);
055 Date now = new Date();
056
057 if (Validator.isNull(templateKey)) {
058 templateKey = String.valueOf(counterLocalService.increment());
059 }
060
061 validate(groupId, templateKey, nameMap, script);
062
063 long templateId = counterLocalService.increment();
064
065 DDMTemplate template = ddmTemplatePersistence.create(templateId);
066
067 template.setUuid(serviceContext.getUuid());
068 template.setGroupId(groupId);
069 template.setCompanyId(user.getCompanyId());
070 template.setUserId(user.getUserId());
071 template.setUserName(user.getFullName());
072 template.setCreateDate(serviceContext.getCreateDate(now));
073 template.setModifiedDate(serviceContext.getModifiedDate(now));
074 template.setClassNameId(classNameId);
075 template.setClassPK(classPK);
076 template.setTemplateKey(templateKey);
077 template.setNameMap(nameMap);
078 template.setDescriptionMap(descriptionMap);
079 template.setType(type);
080 template.setMode(mode);
081 template.setLanguage(language);
082 template.setScript(script);
083 template.setCacheable(cacheable);
084
085 ddmTemplatePersistence.update(template);
086
087
088
089 if (serviceContext.isAddGroupPermissions() ||
090 serviceContext.isAddGuestPermissions()) {
091
092 addTemplateResources(
093 template, serviceContext.isAddGroupPermissions(),
094 serviceContext.isAddGuestPermissions());
095 }
096 else {
097 addTemplateResources(
098 template, serviceContext.getGroupPermissions(),
099 serviceContext.getGuestPermissions());
100 }
101
102 return template;
103 }
104
105 public void addTemplateResources(
106 DDMTemplate template, boolean addGroupPermissions,
107 boolean addGuestPermissions)
108 throws PortalException, SystemException {
109
110 resourceLocalService.addResources(
111 template.getCompanyId(), template.getGroupId(),
112 template.getUserId(), DDMTemplate.class.getName(),
113 template.getTemplateId(), false, addGroupPermissions,
114 addGuestPermissions);
115 }
116
117 public void addTemplateResources(
118 DDMTemplate template, String[] groupPermissions,
119 String[] guestPermissions)
120 throws PortalException, SystemException {
121
122 resourceLocalService.addModelResources(
123 template.getCompanyId(), template.getGroupId(),
124 template.getUserId(), DDMTemplate.class.getName(),
125 template.getTemplateId(), groupPermissions, guestPermissions);
126 }
127
128 public List<DDMTemplate> copyTemplates(
129 long userId, long classNameId, long oldClassPK, long newClassPK,
130 String type, ServiceContext serviceContext)
131 throws PortalException, SystemException {
132
133 List<DDMTemplate> newTemplates = new ArrayList<DDMTemplate>();
134
135 List<DDMTemplate> oldTemplates = getTemplates(
136 classNameId, oldClassPK, type);
137
138 for (DDMTemplate oldTemplate : oldTemplates) {
139 DDMTemplate newTemplate = addTemplate(
140 userId, oldTemplate.getGroupId(), oldTemplate.getClassNameId(),
141 newClassPK, null, oldTemplate.getNameMap(),
142 oldTemplate.getDescriptionMap(), oldTemplate.getType(),
143 oldTemplate.getMode(), oldTemplate.getLanguage(),
144 oldTemplate.getScript(), oldTemplate.isCacheable(),
145 serviceContext);
146
147 newTemplates.add(newTemplate);
148 }
149
150 return newTemplates;
151 }
152
153 public void deleteTemplate(DDMTemplate template)
154 throws PortalException, SystemException {
155
156
157
158 ddmTemplatePersistence.remove(template);
159
160
161
162 resourceLocalService.deleteResource(
163 template.getCompanyId(), DDMTemplate.class.getName(),
164 ResourceConstants.SCOPE_INDIVIDUAL, template.getTemplateId());
165 }
166
167 public void deleteTemplate(long templateId)
168 throws PortalException, SystemException {
169
170 DDMTemplate template = ddmTemplatePersistence.findByPrimaryKey(
171 templateId);
172
173 deleteTemplate(template);
174 }
175
176 public void deleteTemplates(long groupId)
177 throws PortalException, SystemException {
178
179 List<DDMTemplate> templates = ddmTemplatePersistence.findByGroupId(
180 groupId);
181
182 for (DDMTemplate template : templates) {
183 deleteTemplate(template);
184 }
185 }
186
187 public DDMTemplate fetchTemplate(long groupId, String templateKey)
188 throws SystemException {
189
190 return ddmTemplatePersistence.fetchByG_T(groupId, templateKey);
191 }
192
193 public DDMTemplate getTemplate(long templateId)
194 throws PortalException, SystemException {
195
196 return ddmTemplatePersistence.findByPrimaryKey(templateId);
197 }
198
199 public DDMTemplate getTemplate(long groupId, String templateKey)
200 throws PortalException, SystemException {
201
202 return ddmTemplatePersistence.findByG_T(groupId, templateKey);
203 }
204
205 public List<DDMTemplate> getTemplates(long classPK) throws SystemException {
206 return ddmTemplatePersistence.findByClassPK(classPK);
207 }
208
209 public List<DDMTemplate> getTemplates(long groupId, long classNameId)
210 throws SystemException {
211
212 return ddmTemplatePersistence.findByG_C(groupId, classNameId);
213 }
214
215 public List<DDMTemplate> getTemplates(
216 long groupId, long classNameId, long classPK)
217 throws SystemException {
218
219 return ddmTemplatePersistence.findByG_C_C(
220 groupId, classNameId, classPK);
221 }
222
223 public List<DDMTemplate> getTemplates(
224 long classNameId, long classPK, String type)
225 throws SystemException {
226
227 return ddmTemplatePersistence.findByC_C_T(classNameId, classPK, type);
228 }
229
230 public List<DDMTemplate> getTemplates(
231 long classNameId, long classPK, String type, String mode)
232 throws SystemException {
233
234 return ddmTemplatePersistence.findByC_C_T_M(
235 classNameId, classPK, type, mode);
236 }
237
238 public List<DDMTemplate> search(
239 long companyId, long groupId, long classNameId, long classPK,
240 String keywords, String type, String mode, int start, int end,
241 OrderByComparator orderByComparator)
242 throws SystemException {
243
244 return ddmTemplateFinder.findByKeywords(
245 companyId, groupId, classNameId, classPK, keywords, type, mode,
246 start, end, orderByComparator);
247 }
248
249 public List<DDMTemplate> search(
250 long companyId, long groupId, long classNameId, long classPK,
251 String name, String description, String type, String mode,
252 String language, boolean andOperator, int start, int end,
253 OrderByComparator orderByComparator)
254 throws SystemException {
255
256 return ddmTemplateFinder.findByC_G_C_C_N_D_T_M_L(
257 companyId, groupId, classNameId, classPK, name, description, type,
258 mode, language, andOperator, start, end, orderByComparator);
259 }
260
261 public List<DDMTemplate> search(
262 long companyId, long[] groupIds, long[] classNameIds, long classPK,
263 String keywords, String type, String mode, int start, int end,
264 OrderByComparator orderByComparator)
265 throws SystemException {
266
267 return ddmTemplateFinder.findByKeywords(
268 companyId, groupIds, classNameIds, classPK, keywords, type, mode,
269 start, end, orderByComparator);
270 }
271
272 public List<DDMTemplate> search(
273 long companyId, long[] groupIds, long[] classNameIds, long classPK,
274 String name, String description, String type, String mode,
275 String language, boolean andOperator, int start, int end,
276 OrderByComparator orderByComparator)
277 throws SystemException {
278
279 return ddmTemplateFinder.findByC_G_C_C_N_D_T_M_L(
280 companyId, groupIds, classNameIds, classPK, name, description, type,
281 mode, language, andOperator, start, end, orderByComparator);
282 }
283
284 public int searchCount(
285 long companyId, long groupId, long classNameId, long classPK,
286 String keywords, String type, String mode)
287 throws SystemException {
288
289 return ddmTemplateFinder.countByKeywords(
290 companyId, groupId, classNameId, classPK, keywords, type, mode);
291 }
292
293 public int searchCount(
294 long companyId, long groupId, long classNameId, long classPK,
295 String name, String description, String type, String mode,
296 String language, boolean andOperator)
297 throws SystemException {
298
299 return ddmTemplateFinder.countByC_G_C_C_N_D_T_M_L(
300 companyId, groupId, classNameId, classPK, name, description, type,
301 mode, language, andOperator);
302 }
303
304 public int searchCount(
305 long companyId, long[] groupIds, long[] classNameIds, long classPK,
306 String keywords, String type, String mode)
307 throws SystemException {
308
309 return ddmTemplateFinder.countByKeywords(
310 companyId, groupIds, classNameIds, classPK, keywords, type, mode);
311 }
312
313 public int searchCount(
314 long companyId, long[] groupIds, long[] classNameIds, long classPK,
315 String name, String description, String type, String mode,
316 String language, boolean andOperator)
317 throws SystemException {
318
319 return ddmTemplateFinder.countByC_G_C_C_N_D_T_M_L(
320 companyId, groupIds, classNameIds, classPK, name, description, type,
321 mode, language, andOperator);
322 }
323
324 public DDMTemplate updateTemplate(
325 long templateId, Map<Locale, String> nameMap,
326 Map<Locale, String> descriptionMap, String type, String mode,
327 String language, String script, boolean cacheable,
328 ServiceContext serviceContext)
329 throws PortalException, SystemException {
330
331 validate(nameMap, script);
332
333 DDMTemplate template = ddmTemplateLocalService.getDDMTemplate(
334 templateId);
335
336 template.setModifiedDate(serviceContext.getModifiedDate(null));
337 template.setNameMap(nameMap);
338 template.setDescriptionMap(descriptionMap);
339 template.setType(type);
340 template.setMode(mode);
341 template.setLanguage(language);
342 template.setScript(script);
343 template.setCacheable(cacheable);
344
345 ddmTemplatePersistence.update(template);
346
347 return template;
348 }
349
350 protected void validate(
351 long groupId, String templateKey, Map<Locale, String> nameMap,
352 String script)
353 throws PortalException, SystemException {
354
355 DDMTemplate template = ddmTemplatePersistence.fetchByG_T(
356 groupId, templateKey);
357
358 if (template != null) {
359 throw new TemplateDuplicateTemplateKeyException();
360 }
361
362 validate(nameMap, script);
363 }
364
365 protected void validate(Map<Locale, String> nameMap, String script)
366 throws PortalException {
367
368 validateName(nameMap);
369
370 if (Validator.isNull(script)) {
371 throw new TemplateScriptException();
372 }
373 }
374
375 protected void validateName(Map<Locale, String> nameMap)
376 throws PortalException {
377
378 String name = nameMap.get(LocaleUtil.getDefault());
379
380 if (Validator.isNull(name)) {
381 throw new TemplateNameException();
382 }
383 }
384
385 }