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