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.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.OrderByComparator;
020    import com.liferay.portal.kernel.util.ParamUtil;
021    import com.liferay.portal.security.permission.ActionKeys;
022    import com.liferay.portal.service.ServiceContext;
023    import com.liferay.portlet.dynamicdatamapping.model.DDMStructure;
024    import com.liferay.portlet.dynamicdatamapping.service.base.DDMStructureServiceBaseImpl;
025    import com.liferay.portlet.dynamicdatamapping.service.permission.DDMPermission;
026    import com.liferay.portlet.dynamicdatamapping.service.permission.DDMStructurePermission;
027    
028    import java.util.List;
029    import java.util.Locale;
030    import java.util.Map;
031    
032    /**
033     * Provides the remote service for accessing, adding, deleting, and updating
034     * dynamic data mapping (DDM) structures. Its methods include permission checks.
035     *
036     * @author Brian Wing Shun Chan
037     * @author Bruno Basto
038     * @author Marcellus Tavares
039     * @see    com.liferay.portlet.dynamicdatamapping.service.impl.DDMStructureLocalServiceImpl
040     */
041    public class DDMStructureServiceImpl extends DDMStructureServiceBaseImpl {
042    
043            /**
044             * Adds a structure referencing a default parent structure, using the portal
045             * property <code>dynamic.data.lists.storage.type</code> storage type and
046             * default structure type.
047             *
048             * @param  userId the primary key of the structure's creator/owner
049             * @param  groupId the primary key of the group
050             * @param  classNameId the primary key of the class name for the structure's
051             *         related model
052             * @param  nameMap the structure's locales and localized names
053             * @param  descriptionMap the structure's locales and localized descriptions
054             * @param  xsd the structure's XML schema definition
055             * @param  serviceContext the service context to be applied. Can set the
056             *         UUID, creation date, modification date, guest permissions, and
057             *         group permissions for the structure.
058             * @return the structure
059             * @throws PortalException if a user with the primary key could not be
060             *         found, if the user did not have permission to add the structure,
061             *         if the XSD was not well-formed, or if a portal exception occurred
062             * @throws SystemException if a system exception occurred
063             */
064            public DDMStructure addStructure(
065                            long userId, long groupId, long classNameId,
066                            Map<Locale, String> nameMap, Map<Locale, String> descriptionMap,
067                            String xsd, ServiceContext serviceContext)
068                    throws PortalException, SystemException {
069    
070                    String ddmResource = ParamUtil.getString(serviceContext, "ddmResource");
071    
072                    DDMPermission.check(
073                            getPermissionChecker(), serviceContext.getScopeGroupId(),
074                            ddmResource, ActionKeys.ADD_STRUCTURE);
075    
076                    return ddmStructureLocalService.addStructure(
077                            getUserId(), groupId, classNameId, nameMap, descriptionMap, xsd,
078                            serviceContext);
079            }
080    
081            /**
082             * Adds a structure referencing its parent structure.
083             *
084             * @param  groupId the primary key of the group
085             * @param  parentStructureId the primary key of the parent structure
086             *         (optionally {@link
087             *         com.liferay.portlet.dynamicdatamapping.model.DDMStructureConstants#DEFAULT_PARENT_STRUCTURE_ID})
088             * @param  classNameId the primary key of the class name for the structure's
089             *         related model
090             * @param  structureKey the unique string identifying the structure
091             *         (optionally <code>null</code>)
092             * @param  nameMap the structure's locales and localized names
093             * @param  descriptionMap the structure's locales and localized descriptions
094             * @param  xsd the structure's XML schema definition
095             * @param  storageType the structure's storage type. It can be "xml" or
096             *         "expando". For more information, see {@link
097             *         com.liferay.portlet.dynamicdatamapping.storage.StorageType}.
098             * @param  type the structure's type. For more information, see {@link
099             *         com.liferay.portlet.dynamicdatamapping.model.DDMStructureConstants}.
100             * @param  serviceContext the service context to be applied. Can set the
101             *         UUID, creation date, modification date, guest permissions, and
102             *         group permissions for the structure.
103             * @return the structure
104             * @throws PortalException if the user did not have permission to add the
105             *         structure, if the XSD is not well formed, or if a portal
106             *         exception occurred
107             * @throws SystemException if a system exception occurred
108             */
109            public DDMStructure addStructure(
110                            long groupId, long parentStructureId, long classNameId,
111                            String structureKey, Map<Locale, String> nameMap,
112                            Map<Locale, String> descriptionMap, String xsd, String storageType,
113                            int type, ServiceContext serviceContext)
114                    throws PortalException, SystemException {
115    
116                    String ddmResource = ParamUtil.getString(serviceContext, "ddmResource");
117    
118                    DDMPermission.check(
119                            getPermissionChecker(), serviceContext.getScopeGroupId(),
120                            ddmResource, ActionKeys.ADD_STRUCTURE);
121    
122                    return ddmStructureLocalService.addStructure(
123                            getUserId(), groupId, parentStructureId, classNameId, structureKey,
124                            nameMap, descriptionMap, xsd, storageType, type, serviceContext);
125            }
126    
127            /**
128             * Adds a structure referencing the parent structure by its structure key.
129             * In case the parent structure is not found, it uses the default parent
130             * structure ID.
131             *
132             * @param  userId the primary key of the structure's creator/owner
133             * @param  groupId the primary key of the group
134             * @param  parentStructureKey the unique string identifying the structure
135             * @param  classNameId the primary key of the class name for the structure's
136             *         related model
137             * @param  structureKey unique string identifying the structure (optionally
138             *         <code>null</code>)
139             * @param  nameMap the structure's locales and localized names
140             * @param  descriptionMap the structure's locales and localized descriptions
141             * @param  xsd the XML schema definition of the structure
142             * @param  storageType the storage type of the structure. It can be XML or
143             *         expando. For more information, see {@link
144             *         com.liferay.portlet.dynamicdatamapping.storage.StorageType}.
145             * @param  type the structure's type. For more information, see {@link
146             *         com.liferay.portlet.dynamicdatamapping.model.DDMStructureConstants}.
147             * @param  serviceContext the service context to be applied. Must have the
148             *         <code>ddmResource</code> attribute to check permissions. Can set
149             *         the UUID, creation date, modification date, guest permissions,
150             *         and group permissions for the structure.
151             * @return the structure
152             * @throws PortalException if a user with the primary key could not be
153             *         found, if the user did not have permission to add the structure,
154             *         if the XSD was not well-formed, or if a portal exception occurred
155             * @throws SystemException if a system exception occurred
156             */
157            public DDMStructure addStructure(
158                            long userId, long groupId, String parentStructureKey,
159                            long classNameId, String structureKey, Map<Locale, String> nameMap,
160                            Map<Locale, String> descriptionMap, String xsd, String storageType,
161                            int type, ServiceContext serviceContext)
162                    throws PortalException, SystemException {
163    
164                    String ddmResource = ParamUtil.getString(serviceContext, "ddmResource");
165    
166                    DDMPermission.check(
167                            getPermissionChecker(), serviceContext.getScopeGroupId(),
168                            ddmResource, ActionKeys.ADD_STRUCTURE);
169    
170                    return ddmStructureLocalService.addStructure(
171                            userId, groupId, parentStructureKey, classNameId, structureKey,
172                            nameMap, descriptionMap, xsd, storageType, type, serviceContext);
173            }
174    
175            /**
176             * Copies a structure, creating a new structure with all the values
177             * extracted from the original one. The new structure supports a new name
178             * and description.
179             *
180             * @param  structureId the primary key of the structure to be copied
181             * @param  nameMap the new structure's locales and localized names
182             * @param  descriptionMap the new structure's locales and localized
183             *         descriptions
184             * @param  serviceContext the service context to be applied. Can set the
185             *         UUID, creation date, modification date, guest permissions, and
186             *         group permissions for the structure.
187             * @return the new structure
188             * @throws PortalException if the user did not have permission to add the
189             *         structure or if a portal exception occurred
190             * @throws SystemException if a system exception occurred
191             */
192            public DDMStructure copyStructure(
193                            long structureId, Map<Locale, String> nameMap,
194                            Map<Locale, String> descriptionMap, ServiceContext serviceContext)
195                    throws PortalException, SystemException {
196    
197                    String ddmResource = ParamUtil.getString(serviceContext, "ddmResource");
198    
199                    DDMPermission.check(
200                            getPermissionChecker(), serviceContext.getScopeGroupId(),
201                            ddmResource, ActionKeys.ADD_STRUCTURE);
202    
203                    return ddmStructureLocalService.copyStructure(
204                            getUserId(), structureId, nameMap, descriptionMap, serviceContext);
205            }
206    
207            public DDMStructure copyStructure(
208                            long structureId, ServiceContext serviceContext)
209                    throws PortalException, SystemException {
210    
211                    String ddmResource = ParamUtil.getString(serviceContext, "ddmResource");
212    
213                    DDMPermission.check(
214                            getPermissionChecker(), serviceContext.getScopeGroupId(),
215                            ddmResource, ActionKeys.ADD_STRUCTURE);
216    
217                    return ddmStructureLocalService.copyStructure(
218                            getUserId(), structureId, serviceContext);
219            }
220    
221            /**
222             * Deletes the structure and its resources.
223             *
224             * <p>
225             * Before deleting the structure, the system verifies whether the structure
226             * is required by another entity. If it is needed, an exception is thrown.
227             * </p>
228             *
229             * @param  structureId the primary key of the structure to be deleted
230             * @throws PortalException if the user did not have permission to delete the
231             *         structure or if a portal exception occurred
232             * @throws SystemException if a system exception occurred
233             */
234            public void deleteStructure(long structureId)
235                    throws PortalException, SystemException {
236    
237                    DDMStructurePermission.check(
238                            getPermissionChecker(), structureId, ActionKeys.DELETE);
239    
240                    ddmStructureLocalService.deleteStructure(structureId);
241            }
242    
243            /**
244             * Returns the structure matching the class name ID, structure key, and
245             * group.
246             *
247             * @param  groupId the primary key of the group
248             * @param  classNameId the primary key of the class name for the structure's
249             *         related model
250             * @param  structureKey the unique string identifying the structure
251             * @return the matching structure, or <code>null</code> if a matching
252             *         structure could not be found
253             * @throws PortalException if the user did not have permission to view the
254             *         structure or if a portal exception occurred
255             * @throws SystemException if a system exception occurred
256             */
257            public DDMStructure fetchStructure(
258                            long groupId, long classNameId, String structureKey)
259                    throws PortalException, SystemException {
260    
261                    DDMStructure ddmStructure = ddmStructurePersistence.fetchByG_C_S(
262                            groupId, classNameId, structureKey);
263    
264                    if (ddmStructure != null) {
265                            DDMStructurePermission.check(
266                                    getPermissionChecker(), ddmStructure, ActionKeys.VIEW);
267                    }
268    
269                    return ddmStructure;
270            }
271    
272            /**
273             * Returns the structure with the ID.
274             *
275             * @param  structureId the primary key of the structure
276             * @return the structure with the ID
277             * @throws PortalException if the user did not have permission to view the
278             *         structure or if a structure with the ID could not be found
279             * @throws SystemException if a system exception occurred
280             */
281            public DDMStructure getStructure(long structureId)
282                    throws PortalException, SystemException {
283    
284                    DDMStructurePermission.check(
285                            getPermissionChecker(), structureId, ActionKeys.VIEW);
286    
287                    return ddmStructurePersistence.findByPrimaryKey(structureId);
288            }
289    
290            /**
291             * Returns the structure matching the class name ID, structure key, and
292             * group.
293             *
294             * @param  groupId the primary key of the structure's group
295             * @param  classNameId the primary key of the class name for the structure's
296             *         related model
297             * @param  structureKey the unique string identifying the structure
298             * @return the matching structure
299             * @throws PortalException if the user did not have permission to view the
300             *         structure or if a matching structure could not be found
301             * @throws SystemException if a system exception occurred
302             */
303            public DDMStructure getStructure(
304                            long groupId, long classNameId, String structureKey)
305                    throws PortalException, SystemException {
306    
307                    DDMStructurePermission.check(
308                            getPermissionChecker(), groupId, classNameId, structureKey,
309                            ActionKeys.VIEW);
310    
311                    return ddmStructureLocalService.getStructure(
312                            groupId, classNameId, structureKey);
313            }
314    
315            /**
316             * Returns the structure matching the class name ID, structure key, and
317             * group, optionally in the global scope.
318             *
319             * <p>
320             * This method first searches in the group. If the structure is still not
321             * found and <code>includeGlobalStructures</code> is set to
322             * <code>true</code>, this method searches the global group.
323             * </p>
324             *
325             * @param  groupId the primary key of the structure's group
326             * @param  classNameId the primary key of the class name for the structure's
327             *         related model
328             * @param  structureKey the unique string identifying the structure
329             * @param  includeGlobalStructures whether to include the global scope in
330             *         the search
331             * @return the matching structure
332             * @throws PortalException if the user did not have permission to view the
333             *         structure or if a matching structure could not be found
334             * @throws SystemException if a system exception occurred
335             */
336            public DDMStructure getStructure(
337                            long groupId, long classNameId, String structureKey,
338                            boolean includeGlobalStructures)
339                    throws PortalException, SystemException {
340    
341                    DDMStructurePermission.check(
342                            getPermissionChecker(), groupId, classNameId, structureKey,
343                            ActionKeys.VIEW);
344    
345                    return ddmStructureLocalService.getStructure(
346                            groupId, classNameId, structureKey, includeGlobalStructures);
347            }
348    
349            /**
350             * Returns all the structures in the group that the user has permission to
351             * view.
352             *
353             * @param  groupId the primary key of the group
354             * @return the structures in the group that the user has permission to view
355             * @throws SystemException if a system exception occurred
356             */
357            public List<DDMStructure> getStructures(long groupId)
358                    throws SystemException {
359    
360                    return ddmStructurePersistence.filterFindByGroupId(groupId);
361            }
362    
363            /**
364             * Returns all the structures in the groups that the user has permission to
365             * view.
366             *
367             * @param  groupIds the primary key of the groups
368             * @return the structures in the groups that the user has permission to view
369             * @throws SystemException if a system exception occurred
370             */
371            public List<DDMStructure> getStructures(long[] groupIds)
372                    throws SystemException {
373    
374                    return ddmStructurePersistence.filterFindByGroupId(groupIds);
375            }
376    
377            /**
378             * Returns an ordered range of all the structures matching the groups and
379             * class name IDs, and matching the keywords in the structure names and
380             * descriptions.
381             *
382             * <p>
383             * Useful when paginating results. Returns a maximum of <code>end -
384             * start</code> instances. <code>start</code> and <code>end</code> are not
385             * primary keys, they are indexes in the result set. Thus, <code>0</code>
386             * refers to the first result in the set. Setting both <code>start</code>
387             * and <code>end</code> to {@link
388             * com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full
389             * result set.
390             * </p>
391             *
392             * @param  companyId the primary key of the structure's company
393             * @param  groupIds the primary keys of the groups
394             * @param  classNameIds the primary keys of the class names of the models
395             *         the structures are related to
396             * @param  keywords the keywords (space separated), which may occur in the
397             *         structure's name or description (optionally <code>null</code>)
398             * @param  start the lower bound of the range of structures to return
399             * @param  end the upper bound of the range of structures to return (not
400             *         inclusive)
401             * @param  orderByComparator the comparator to order the structures
402             *         (optionally <code>null</code>)
403             * @return the range of matching structures ordered by the comparator
404             * @throws SystemException if a system exception occurred
405             */
406            public List<DDMStructure> search(
407                            long companyId, long[] groupIds, long[] classNameIds,
408                            String keywords, int start, int end,
409                            OrderByComparator orderByComparator)
410                    throws SystemException {
411    
412                    return ddmStructureFinder.filterFindByKeywords(
413                            companyId, groupIds, classNameIds, keywords, start, end,
414                            orderByComparator);
415            }
416    
417            /**
418             * Returns an ordered range of all the structures matching the groups, class
419             * name IDs, name keyword, description keyword, storage type, and type.
420             *
421             * <p>
422             * Useful when paginating results. Returns a maximum of <code>end -
423             * start</code> instances. <code>start</code> and <code>end</code> are not
424             * primary keys, they are indexes in the result set. Thus, <code>0</code>
425             * refers to the first result in the set. Setting both <code>start</code>
426             * and <code>end</code> to {@link
427             * com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full
428             * result set.
429             * </p>
430             *
431             * @param  companyId the primary key of the structure's company
432             * @param  groupIds the primary keys of the groups
433             * @param  classNameIds the primary keys of the class names of the models
434             *         the structures are related to
435             * @param  name the name keywords
436             * @param  description the description keywords
437             * @param  storageType the structure's storage type. It can be "xml" or
438             *         "expando". For more information, see {@link
439             *         com.liferay.portlet.dynamicdatamapping.storage.StorageType}.
440             * @param  type the structure's type. For more information, see {@link
441             *         com.liferay.portlet.dynamicdatamapping.model.DDMStructureConstants}.
442             * @param  andOperator whether every field must match its keywords, or just
443             *         one field
444             * @param  start the lower bound of the range of structures to return
445             * @param  end the upper bound of the range of structures to return (not
446             *         inclusive)
447             * @param  orderByComparator the comparator to order the structures
448             *         (optionally <code>null</code>)
449             * @return the range of matching structures ordered by the comparator
450             * @throws SystemException if a system exception occurred
451             */
452            public List<DDMStructure> search(
453                            long companyId, long[] groupIds, long[] classNameIds, String name,
454                            String description, String storageType, int type,
455                            boolean andOperator, int start, int end,
456                            OrderByComparator orderByComparator)
457                    throws SystemException {
458    
459                    return ddmStructureFinder.filterFindByC_G_C_N_D_S_T(
460                            companyId, groupIds, classNameIds, name, description, storageType,
461                            type, andOperator, start, end, orderByComparator);
462            }
463    
464            /**
465             * Returns the number of structures matching the groups and class name IDs,
466             * and matching the keywords in the structure names and descriptions.
467             *
468             * @param  companyId the primary key of the structure's company
469             * @param  groupIds the primary keys of the groups
470             * @param  classNameIds the primary keys of the class names of the models
471             *         the structures are related to
472             * @param  keywords the keywords (space separated), which may occur in the
473             *         structure's name or description (optionally <code>null</code>)
474             * @return the number of matching structures
475             * @throws SystemException if a system exception occurred
476             */
477            public int searchCount(
478                            long companyId, long[] groupIds, long[] classNameIds,
479                            String keywords)
480                    throws SystemException {
481    
482                    return ddmStructureFinder.filterCountByKeywords(
483                            companyId, groupIds, classNameIds, keywords);
484            }
485    
486            /**
487             * Returns the number of structures matching the groups, class name IDs,
488             * name keyword, description keyword, storage type, and type
489             *
490             * @param  companyId the primary key of the structure's company
491             * @param  groupIds the primary keys of the groups
492             * @param  classNameIds the primary keys of the class names of the models
493             *         the structure's are related to
494             * @param  name the name keywords
495             * @param  description the description keywords
496             * @param  storageType the structure's storage type. It can be "xml" or
497             *         "expando". For more information, see {@link
498             *         com.liferay.portlet.dynamicdatamapping.storage.StorageType}.
499             * @param  type the structure's type. For more information, see {@link
500             *         com.liferay.portlet.dynamicdatamapping.model.DDMStructureConstants}.
501             * @param  andOperator whether every field must match its keywords, or just
502             *         one field
503             * @return the number of matching structures
504             * @throws SystemException if a system exception occurred
505             */
506            public int searchCount(
507                            long companyId, long[] groupIds, long[] classNameIds, String name,
508                            String description, String storageType, int type,
509                            boolean andOperator)
510                    throws SystemException {
511    
512                    return ddmStructureFinder.filterCountByC_G_C_N_D_S_T(
513                            companyId, groupIds, classNameIds, name, description, storageType,
514                            type, andOperator);
515            }
516    
517            /**
518             * Updates the structure matching the class name ID, structure key, and
519             * group, replacing its old parent structure, name map, description map, and
520             * XSD with new ones.
521             *
522             * @param  groupId the primary key of the group
523             * @param  parentStructureId the primary key of the new parent structure
524             * @param  classNameId the primary key of the class name for the structure's
525             *         related model
526             * @param  structureKey the unique string identifying the structure
527             * @param  nameMap the structure's new locales and localized names
528             * @param  descriptionMap the structure's new locales and localized
529             *         description
530             * @param  xsd the structure's new XML schema definition
531             * @param  serviceContext the service context to be applied. Can set the
532             *         modification date.
533             * @return the updated structure
534             * @throws PortalException if the user did not have permission to update the
535             *         structure or if a portal exception occurred
536             * @throws SystemException if a system exception occurred
537             */
538            public DDMStructure updateStructure(
539                            long groupId, long parentStructureId, long classNameId,
540                            String structureKey, Map<Locale, String> nameMap,
541                            Map<Locale, String> descriptionMap, String xsd,
542                            ServiceContext serviceContext)
543                    throws PortalException, SystemException {
544    
545                    DDMStructurePermission.check(
546                            getPermissionChecker(), groupId, classNameId, structureKey,
547                            ActionKeys.UPDATE);
548    
549                    return ddmStructureLocalService.updateStructure(
550                            groupId, parentStructureId, classNameId, structureKey, nameMap,
551                            descriptionMap, xsd, serviceContext);
552            }
553    
554            /**
555             * Updates the structure matching the structure ID, replacing the old parent
556             * structure ID, name map, description map, and XSD with the new values.
557             *
558             * @param  structureId the primary key of the structure
559             * @param  parentStructureId the new parent structure primary key
560             * @param  nameMap the structure's new locales and localized names
561             * @param  descriptionMap the structure's new locales and localized
562             *         description
563             * @param  xsd the new XML schema definition of the structure
564             * @param  serviceContext the service context to be applied. Can set the
565             *         modification date.
566             * @return the updated structure
567             * @throws PortalException if the user did not have permission to update the
568             *         structure or if a portal exception occurred
569             * @throws SystemException if a system exception occurred
570             */
571            public DDMStructure updateStructure(
572                            long structureId, long parentStructureId,
573                            Map<Locale, String> nameMap, Map<Locale, String> descriptionMap,
574                            String xsd, ServiceContext serviceContext)
575                    throws PortalException, SystemException {
576    
577                    DDMStructurePermission.check(
578                            getPermissionChecker(), structureId, ActionKeys.UPDATE);
579    
580                    return ddmStructureLocalService.updateStructure(
581                            structureId, parentStructureId, nameMap, descriptionMap, xsd,
582                            serviceContext);
583            }
584    
585    }