001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portlet.dynamicdatalists.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.portal.util.PortalUtil;
026    import com.liferay.portlet.dynamicdatalists.RecordSetDDMStructureIdException;
027    import com.liferay.portlet.dynamicdatalists.RecordSetDuplicateRecordSetKeyException;
028    import com.liferay.portlet.dynamicdatalists.RecordSetNameException;
029    import com.liferay.portlet.dynamicdatalists.model.DDLRecordSet;
030    import com.liferay.portlet.dynamicdatalists.service.base.DDLRecordSetLocalServiceBaseImpl;
031    import com.liferay.portlet.dynamicdatamapping.model.DDMStructure;
032    
033    import java.util.Date;
034    import java.util.List;
035    import java.util.Locale;
036    import java.util.Map;
037    
038    /**
039     * @author Brian Wing Shun Chan
040     * @author Marcellus Tavares
041     */
042    public class DDLRecordSetLocalServiceImpl
043            extends DDLRecordSetLocalServiceBaseImpl {
044    
045            @Override
046            public DDLRecordSet addRecordSet(
047                            long userId, long groupId, long ddmStructureId, String recordSetKey,
048                            Map<Locale, String> nameMap, Map<Locale, String> descriptionMap,
049                            int minDisplayRows, int scope, ServiceContext serviceContext)
050                    throws PortalException, SystemException {
051    
052                    // Record set
053    
054                    User user = userPersistence.findByPrimaryKey(userId);
055    
056                    if (Validator.isNull(recordSetKey)) {
057                            recordSetKey = String.valueOf(counterLocalService.increment());
058                    }
059    
060                    Date now = new Date();
061    
062                    validate(groupId, ddmStructureId, recordSetKey, nameMap);
063    
064                    long recordSetId = counterLocalService.increment();
065    
066                    DDLRecordSet recordSet = ddlRecordSetPersistence.create(recordSetId);
067    
068                    recordSet.setUuid(serviceContext.getUuid());
069                    recordSet.setGroupId(groupId);
070                    recordSet.setCompanyId(user.getCompanyId());
071                    recordSet.setUserId(user.getUserId());
072                    recordSet.setUserName(user.getFullName());
073                    recordSet.setCreateDate(serviceContext.getCreateDate(now));
074                    recordSet.setModifiedDate(serviceContext.getModifiedDate(now));
075                    recordSet.setDDMStructureId(ddmStructureId);
076                    recordSet.setRecordSetKey(recordSetKey);
077                    recordSet.setNameMap(nameMap);
078                    recordSet.setDescriptionMap(descriptionMap);
079                    recordSet.setMinDisplayRows(minDisplayRows);
080                    recordSet.setScope(scope);
081    
082                    ddlRecordSetPersistence.update(recordSet, false);
083    
084                    // Resources
085    
086                    if (serviceContext.isAddGroupPermissions() ||
087                            serviceContext.isAddGuestPermissions()) {
088    
089                            addRecordSetResources(
090                                    recordSet, serviceContext.isAddGroupPermissions(),
091                                    serviceContext.isAddGuestPermissions());
092                    }
093                    else {
094                            addRecordSetResources(
095                                    recordSet, serviceContext.getGroupPermissions(),
096                                    serviceContext.getGuestPermissions());
097                    }
098    
099                    // Dynamic data mapping structure link
100    
101                    long classNameId = PortalUtil.getClassNameId(DDLRecordSet.class);
102    
103                    ddmStructureLinkLocalService.addStructureLink(
104                            classNameId, recordSetId, ddmStructureId, serviceContext);
105    
106                    return recordSet;
107            }
108    
109            @Override
110            public void addRecordSetResources(
111                            DDLRecordSet recordSet, boolean addGroupPermissions,
112                            boolean addGuestPermissions)
113                    throws PortalException, SystemException {
114    
115                    resourceLocalService.addResources(
116                            recordSet.getCompanyId(), recordSet.getGroupId(),
117                            recordSet.getUserId(), DDLRecordSet.class.getName(),
118                            recordSet.getRecordSetId(), false, addGroupPermissions,
119                            addGuestPermissions);
120            }
121    
122            @Override
123            public void addRecordSetResources(
124                            DDLRecordSet recordSet, String[] groupPermissions,
125                            String[] guestPermissions)
126                    throws PortalException, SystemException {
127    
128                    resourceLocalService.addModelResources(
129                            recordSet.getCompanyId(), recordSet.getGroupId(),
130                            recordSet.getUserId(), DDLRecordSet.class.getName(),
131                            recordSet.getRecordSetId(), groupPermissions, guestPermissions);
132            }
133    
134            @Override
135            public void deleteRecordSet(DDLRecordSet recordSet)
136                    throws PortalException, SystemException {
137    
138                    // Record set
139    
140                    ddlRecordSetPersistence.remove(recordSet);
141    
142                    // Resources
143    
144                    resourceLocalService.deleteResource(
145                            recordSet.getCompanyId(), DDLRecordSet.class.getName(),
146                            ResourceConstants.SCOPE_INDIVIDUAL, recordSet.getRecordSetId());
147    
148                    // Records
149    
150                    ddlRecordLocalService.deleteRecords(recordSet.getRecordSetId());
151    
152                    // Dynamic data mapping structure link
153    
154                    ddmStructureLinkLocalService.deleteClassStructureLink(
155                            recordSet.getRecordSetId());
156    
157                    // Workflow
158    
159                    workflowDefinitionLinkLocalService.deleteWorkflowDefinitionLink(
160                            recordSet.getCompanyId(), recordSet.getGroupId(),
161                            DDLRecordSet.class.getName(), recordSet.getRecordSetId(), 0);
162            }
163    
164            @Override
165            public void deleteRecordSet(long recordSetId)
166                    throws PortalException, SystemException {
167    
168                    DDLRecordSet recordSet = ddlRecordSetPersistence.findByPrimaryKey(
169                            recordSetId);
170    
171                    deleteRecordSet(recordSet);
172            }
173    
174            @Override
175            public void deleteRecordSet(long groupId, String recordSetKey)
176                    throws PortalException, SystemException {
177    
178                    DDLRecordSet recordSet = ddlRecordSetPersistence.findByG_R(
179                            groupId, recordSetKey);
180    
181                    deleteRecordSet(recordSet);
182            }
183    
184            @Override
185            public void deleteRecordSets(long groupId)
186                    throws PortalException, SystemException {
187    
188                    List<DDLRecordSet> recordSets = ddlRecordSetPersistence.findByGroupId(
189                            groupId);
190    
191                    for (DDLRecordSet recordSet : recordSets) {
192                            deleteRecordSet(recordSet);
193                    }
194            }
195    
196            @Override
197            public DDLRecordSet fetchRecordSet(long groupId, String recordSetKey)
198                    throws SystemException {
199    
200                    return ddlRecordSetPersistence.fetchByG_R(groupId, recordSetKey);
201            }
202    
203            @Override
204            public DDLRecordSet getRecordSet(long recordSetId)
205                    throws PortalException, SystemException {
206    
207                    return ddlRecordSetPersistence.findByPrimaryKey(recordSetId);
208            }
209    
210            @Override
211            public DDLRecordSet getRecordSet(long groupId, String recordSetKey)
212                    throws PortalException, SystemException {
213    
214                    return ddlRecordSetPersistence.findByG_R(groupId, recordSetKey);
215            }
216    
217            @Override
218            public List<DDLRecordSet> getRecordSets(long groupId)
219                    throws SystemException {
220    
221                    return ddlRecordSetPersistence.findByGroupId(groupId);
222            }
223    
224            @Override
225            public int getRecordSetsCount(long groupId) throws SystemException {
226                    return ddlRecordSetPersistence.countByGroupId(groupId);
227            }
228    
229            @Override
230            public List<DDLRecordSet> search(
231                            long companyId, long groupId, String keywords, int scope, int start,
232                            int end, OrderByComparator orderByComparator)
233                    throws SystemException {
234    
235                    return ddlRecordSetFinder.findByKeywords(
236                            companyId, groupId, keywords, scope, start, end, orderByComparator);
237            }
238    
239            @Override
240            public List<DDLRecordSet> search(
241                            long companyId, long groupId, String name, String description,
242                            int scope, boolean andOperator, int start, int end,
243                            OrderByComparator orderByComparator)
244                    throws SystemException {
245    
246                    return ddlRecordSetFinder.findByC_G_N_D_S(
247                            companyId, groupId, name, description, scope, andOperator, start,
248                            end, orderByComparator);
249            }
250    
251            @Override
252            public int searchCount(
253                            long companyId, long groupId, String keywords, int scope)
254                    throws SystemException {
255    
256                    return ddlRecordSetFinder.countByKeywords(
257                            companyId, groupId, keywords, scope);
258            }
259    
260            @Override
261            public int searchCount(
262                            long companyId, long groupId, String name, String description,
263                            int scope, boolean andOperator)
264                    throws SystemException {
265    
266                    return ddlRecordSetFinder.countByC_G_N_D_S(
267                            companyId, groupId, name, description, scope, andOperator);
268            }
269    
270            @Override
271            public DDLRecordSet updateMinDisplayRows(
272                            long recordSetId, int minDisplayRows, ServiceContext serviceContext)
273                    throws PortalException, SystemException {
274    
275                    DDLRecordSet recordSet = ddlRecordSetPersistence.findByPrimaryKey(
276                            recordSetId);
277    
278                    recordSet.setModifiedDate(serviceContext.getModifiedDate(null));
279                    recordSet.setMinDisplayRows(minDisplayRows);
280    
281                    ddlRecordSetPersistence.update(recordSet, false);
282    
283                    return recordSet;
284            }
285    
286            @Override
287            public DDLRecordSet updateRecordSet(
288                            long recordSetId, long ddmStructureId, Map<Locale, String> nameMap,
289                            Map<Locale, String> descriptionMap, int minDisplayRows,
290                            ServiceContext serviceContext)
291                    throws PortalException, SystemException {
292    
293                    DDLRecordSet recordSet = ddlRecordSetPersistence.findByPrimaryKey(
294                            recordSetId);
295    
296                    return doUpdateRecordSet(
297                            ddmStructureId, nameMap, descriptionMap, minDisplayRows,
298                            serviceContext, recordSet);
299            }
300    
301            @Override
302            public DDLRecordSet updateRecordSet(
303                            long groupId, long ddmStructureId, String recordSetKey,
304                            Map<Locale, String> nameMap, Map<Locale, String> descriptionMap,
305                            int minDisplayRows, ServiceContext serviceContext)
306                    throws PortalException, SystemException {
307    
308                    DDLRecordSet recordSet = ddlRecordSetPersistence.findByG_R(
309                            groupId, recordSetKey);
310    
311                    return doUpdateRecordSet(
312                            ddmStructureId, nameMap, descriptionMap, minDisplayRows,
313                            serviceContext, recordSet);
314            }
315    
316            protected DDLRecordSet doUpdateRecordSet(
317                            long ddmStructureId, Map<Locale, String> nameMap,
318                            Map<Locale, String> descriptionMap, int minDisplayRows,
319                            ServiceContext serviceContext, DDLRecordSet recordSet)
320                    throws PortalException, SystemException {
321    
322                    validateDDMStructureId(ddmStructureId);
323                    validateName(nameMap);
324    
325                    recordSet.setModifiedDate(serviceContext.getModifiedDate(null));
326                    recordSet.setDDMStructureId(ddmStructureId);
327                    recordSet.setNameMap(nameMap);
328                    recordSet.setDescriptionMap(descriptionMap);
329                    recordSet.setMinDisplayRows(minDisplayRows);
330    
331                    ddlRecordSetPersistence.update(recordSet, false);
332    
333                    return recordSet;
334            }
335    
336            protected void validate(
337                            long groupId, long ddmStructureId, String recordSetKey,
338                            Map<Locale, String> nameMap)
339                    throws PortalException, SystemException {
340    
341                    validateDDMStructureId(ddmStructureId);
342    
343                    if (Validator.isNotNull(recordSetKey)) {
344                            DDLRecordSet recordSet = ddlRecordSetPersistence.fetchByG_R(
345                                    groupId, recordSetKey);
346    
347                            if (recordSet != null) {
348                                    RecordSetDuplicateRecordSetKeyException rsdrske =
349                                            new RecordSetDuplicateRecordSetKeyException();
350    
351                                    rsdrske.setRecordSetKey(recordSet.getRecordSetKey());
352    
353                                    throw rsdrske;
354                            }
355                    }
356    
357                    validateName(nameMap);
358            }
359    
360            protected void validateDDMStructureId(long ddmStructureId)
361                    throws PortalException, SystemException {
362    
363                    DDMStructure ddmStructure = ddmStructurePersistence.fetchByPrimaryKey(
364                            ddmStructureId);
365    
366                    if (ddmStructure == null) {
367                            throw new RecordSetDDMStructureIdException();
368                    }
369            }
370    
371            protected void validateName(Map<Locale, String> nameMap)
372                    throws PortalException {
373    
374                    String name = nameMap.get(LocaleUtil.getDefault());
375    
376                    if (Validator.isNull(name)) {
377                            throw new RecordSetNameException();
378                    }
379            }
380    
381    }