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.portal.repository.liferayrepository;
016    
017    import com.liferay.portal.kernel.exception.PortalException;
018    import com.liferay.portal.kernel.exception.SystemException;
019    import com.liferay.portal.kernel.util.ParamUtil;
020    import com.liferay.portal.kernel.util.SortedArrayList;
021    import com.liferay.portal.kernel.util.StringUtil;
022    import com.liferay.portal.repository.liferayrepository.util.LiferayBase;
023    import com.liferay.portal.service.RepositoryLocalService;
024    import com.liferay.portal.service.RepositoryService;
025    import com.liferay.portal.service.ResourceLocalService;
026    import com.liferay.portal.service.ServiceContext;
027    import com.liferay.portlet.documentlibrary.model.DLFileEntry;
028    import com.liferay.portlet.documentlibrary.model.DLFileEntryConstants;
029    import com.liferay.portlet.documentlibrary.model.DLFileEntryType;
030    import com.liferay.portlet.documentlibrary.model.DLFolderConstants;
031    import com.liferay.portlet.documentlibrary.service.DLAppHelperLocalService;
032    import com.liferay.portlet.documentlibrary.service.DLFileEntryLocalService;
033    import com.liferay.portlet.documentlibrary.service.DLFileEntryService;
034    import com.liferay.portlet.documentlibrary.service.DLFileEntryTypeLocalService;
035    import com.liferay.portlet.documentlibrary.service.DLFileEntryTypeLocalServiceUtil;
036    import com.liferay.portlet.documentlibrary.service.DLFileVersionLocalService;
037    import com.liferay.portlet.documentlibrary.service.DLFileVersionService;
038    import com.liferay.portlet.documentlibrary.service.DLFolderLocalService;
039    import com.liferay.portlet.documentlibrary.service.DLFolderService;
040    import com.liferay.portlet.dynamicdatamapping.model.DDMStructure;
041    import com.liferay.portlet.dynamicdatamapping.storage.Fields;
042    import com.liferay.portlet.dynamicdatamapping.util.DDMUtil;
043    
044    import java.util.ArrayList;
045    import java.util.HashMap;
046    import java.util.List;
047    
048    /**
049     * @author Alexander Chow
050     */
051    public abstract class LiferayRepositoryBase extends LiferayBase {
052    
053            public LiferayRepositoryBase(
054                    RepositoryLocalService repositoryLocalService,
055                    RepositoryService repositoryService,
056                    DLAppHelperLocalService dlAppHelperLocalService,
057                    DLFileEntryLocalService dlFileEntryLocalService,
058                    DLFileEntryService dlFileEntryService,
059                    DLFileEntryTypeLocalService dlFileEntryTypeLocalService,
060                    DLFileVersionLocalService dlFileVersionLocalService,
061                    DLFileVersionService dlFileVersionService,
062                    DLFolderLocalService dlFolderLocalService,
063                    DLFolderService dlFolderService,
064                    ResourceLocalService resourceLocalService, long repositoryId) {
065    
066                    this.repositoryLocalService = repositoryLocalService;
067                    this.repositoryService = repositoryService;
068                    this.dlAppHelperLocalService = dlAppHelperLocalService;
069                    this.dlFileEntryLocalService = dlFileEntryLocalService;
070                    this.dlFileEntryService = dlFileEntryService;
071                    this.dlFileEntryTypeLocalService = dlFileEntryTypeLocalService;
072                    this.dlFileVersionLocalService = dlFileVersionLocalService;
073                    this.dlFileVersionService = dlFileVersionService;
074                    this.dlFolderLocalService = dlFolderLocalService;
075                    this.dlFolderService = dlFolderService;
076                    this.resourceLocalService = resourceLocalService;
077    
078                    initByRepositoryId(repositoryId);
079            }
080    
081            public LiferayRepositoryBase(
082                    RepositoryLocalService repositoryLocalService,
083                    RepositoryService repositoryService,
084                    DLAppHelperLocalService dlAppHelperLocalService,
085                    DLFileEntryLocalService dlFileEntryLocalService,
086                    DLFileEntryService dlFileEntryService,
087                    DLFileEntryTypeLocalService dlFileEntryTypeLocalService,
088                    DLFileVersionLocalService dlFileVersionLocalService,
089                    DLFileVersionService dlFileVersionService,
090                    DLFolderLocalService dlFolderLocalService,
091                    DLFolderService dlFolderService,
092                    ResourceLocalService resourceLocalService, long folderId,
093                    long fileEntryId, long fileVersionId) {
094    
095                    this.repositoryLocalService = repositoryLocalService;
096                    this.repositoryService = repositoryService;
097                    this.dlAppHelperLocalService = dlAppHelperLocalService;
098                    this.dlFileEntryLocalService = dlFileEntryLocalService;
099                    this.dlFileEntryService = dlFileEntryService;
100                    this.dlFileEntryTypeLocalService = dlFileEntryTypeLocalService;
101                    this.dlFileVersionLocalService = dlFileVersionLocalService;
102                    this.dlFileVersionService = dlFileVersionService;
103                    this.dlFolderLocalService = dlFolderLocalService;
104                    this.dlFolderService = dlFolderService;
105                    this.resourceLocalService = resourceLocalService;
106    
107                    if (folderId != 0) {
108                            initByFolderId(folderId);
109                    }
110                    else if (fileEntryId != 0) {
111                            initByFileEntryId(fileEntryId);
112                    }
113                    else if (fileVersionId != 0) {
114                            initByFileVersionId(fileVersionId);
115                    }
116            }
117    
118            public long getRepositoryId() {
119                    return _repositoryId;
120            }
121    
122            protected void addFileEntryResources(
123                            DLFileEntry dlFileEntry, ServiceContext serviceContext)
124                    throws PortalException, SystemException {
125    
126                    if (serviceContext.isAddGroupPermissions() ||
127                            serviceContext.isAddGuestPermissions()) {
128    
129                            resourceLocalService.addResources(
130                                    dlFileEntry.getCompanyId(), dlFileEntry.getGroupId(),
131                                    dlFileEntry.getUserId(), DLFileEntry.class.getName(),
132                                    dlFileEntry.getFileEntryId(), false,
133                                    serviceContext.isAddGroupPermissions(),
134                                    serviceContext.isAddGuestPermissions());
135                    }
136                    else {
137                            if (serviceContext.isDeriveDefaultPermissions()) {
138                                    serviceContext.deriveDefaultPermissions(
139                                            dlFileEntry.getRepositoryId(),
140                                            DLFileEntryConstants.getClassName());
141                            }
142    
143                            resourceLocalService.addModelResources(
144                                    dlFileEntry.getCompanyId(), dlFileEntry.getGroupId(),
145                                    dlFileEntry.getUserId(), DLFileEntry.class.getName(),
146                                    dlFileEntry.getFileEntryId(),
147                                    serviceContext.getGroupPermissions(),
148                                    serviceContext.getGuestPermissions());
149                    }
150            }
151    
152            protected long getDefaultFileEntryTypeId(
153                            ServiceContext serviceContext, long folderId)
154                    throws PortalException, SystemException {
155    
156                    folderId = dlFolderLocalService.getFolderId(
157                            serviceContext.getCompanyId(), folderId);
158    
159                    return dlFileEntryTypeLocalService.getDefaultFileEntryTypeId(folderId);
160            }
161    
162            protected HashMap<String, Fields> getFieldsMap(
163                            ServiceContext serviceContext, long fileEntryTypeId)
164                    throws PortalException, SystemException {
165    
166                    HashMap<String, Fields> fieldsMap = new HashMap<String, Fields>();
167    
168                    if (fileEntryTypeId <= 0) {
169                            return fieldsMap;
170                    }
171    
172                    DLFileEntryType fileEntryType =
173                            DLFileEntryTypeLocalServiceUtil.getFileEntryType(fileEntryTypeId);
174    
175                    List<DDMStructure> ddmStructures = fileEntryType.getDDMStructures();
176    
177                    for (DDMStructure ddmStructure : ddmStructures) {
178                            String namespace = String.valueOf(ddmStructure.getStructureId());
179    
180                            Fields fields = (Fields)serviceContext.getAttribute(
181                                    Fields.class.getName() + ddmStructure.getStructureId());
182    
183                            if (fields == null) {
184                                    fields = DDMUtil.getFields(
185                                            ddmStructure.getStructureId(), namespace, serviceContext);
186                            }
187    
188                            fieldsMap.put(ddmStructure.getStructureKey(), fields);
189                    }
190    
191                    return fieldsMap;
192            }
193    
194            protected long getGroupId() {
195                    return _groupId;
196            }
197    
198            protected SortedArrayList<Long> getLongList(
199                    ServiceContext serviceContext, String name) {
200    
201                    String value = ParamUtil.getString(serviceContext, name);
202    
203                    if (value == null) {
204                            return new SortedArrayList<Long>();
205                    }
206    
207                    long[] longArray = StringUtil.split(value, 0L);
208    
209                    SortedArrayList<Long> longList = new SortedArrayList<Long>();
210    
211                    for (long longValue : longArray) {
212                            longList.add(longValue);
213                    }
214    
215                    return longList;
216            }
217    
218            protected abstract void initByFileEntryId(long fileEntryId);
219    
220            protected abstract void initByFileVersionId(long fileVersionId);
221    
222            protected abstract void initByFolderId(long folderId);
223    
224            protected abstract void initByRepositoryId(long repositoryId);
225    
226            protected boolean isDefaultRepository() {
227                    if (_groupId == _repositoryId) {
228                            return true;
229                    }
230                    else {
231                            return false;
232                    }
233            }
234    
235            protected void setDlFolderId(long dlFolderId) {
236                    _dlFolderId = dlFolderId;
237            }
238    
239            protected void setGroupId(long groupId) {
240                    _groupId = groupId;
241            }
242    
243            protected void setRepositoryId(long repositoryId) {
244                    _repositoryId = repositoryId;
245            }
246    
247            protected long toFolderId(long folderId) {
248                    if (folderId == DLFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
249                            return _dlFolderId;
250                    }
251                    else {
252                            return folderId;
253                    }
254            }
255    
256            protected List<Long> toFolderIds(List<Long> folderIds) {
257                    List<Long> toFolderIds = new ArrayList<Long>(folderIds.size());
258    
259                    for (long folderId : folderIds) {
260                            toFolderIds.add(toFolderId(folderId));
261                    }
262    
263                    return toFolderIds;
264            }
265    
266            protected DLAppHelperLocalService dlAppHelperLocalService;
267            protected DLFileEntryLocalService dlFileEntryLocalService;
268            protected DLFileEntryService dlFileEntryService;
269            protected DLFileEntryTypeLocalService dlFileEntryTypeLocalService;
270            protected DLFileVersionLocalService dlFileVersionLocalService;
271            protected DLFileVersionService dlFileVersionService;
272            protected DLFolderLocalService dlFolderLocalService;
273            protected DLFolderService dlFolderService;
274            protected RepositoryLocalService repositoryLocalService;
275            protected RepositoryService repositoryService;
276            protected ResourceLocalService resourceLocalService;
277    
278            private long _dlFolderId;
279            private long _groupId;
280            private long _repositoryId;
281    
282    }