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