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