001: /**
002: * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
003: *
004: * Permission is hereby granted, free of charge, to any person obtaining a copy
005: * of this software and associated documentation files (the "Software"), to deal
006: * in the Software without restriction, including without limitation the rights
007: * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
008: * copies of the Software, and to permit persons to whom the Software is
009: * furnished to do so, subject to the following conditions:
010: *
011: * The above copyright notice and this permission notice shall be included in
012: * all copies or substantial portions of the Software.
013: *
014: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
015: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
016: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
017: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
018: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
019: * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
020: * SOFTWARE.
021: */package com.liferay.portlet.documentlibrary.service.impl;
022:
023: import com.liferay.portal.PortalException;
024: import com.liferay.portal.SystemException;
025: import com.liferay.portal.kernel.security.permission.ActionKeys;
026: import com.liferay.portal.kernel.util.StringPool;
027: import com.liferay.portal.security.auth.PrincipalException;
028: import com.liferay.portlet.documentlibrary.model.DLFileEntry;
029: import com.liferay.portlet.documentlibrary.model.DLFolder;
030: import com.liferay.portlet.documentlibrary.service.base.DLFolderServiceBaseImpl;
031: import com.liferay.portlet.documentlibrary.service.permission.DLFileEntryPermission;
032: import com.liferay.portlet.documentlibrary.service.permission.DLFolderPermission;
033: import com.liferay.util.FileUtil;
034: import com.liferay.util.PwdGenerator;
035: import com.liferay.util.SystemProperties;
036: import com.liferay.util.Time;
037:
038: import java.io.File;
039: import java.io.InputStream;
040:
041: import java.util.Iterator;
042:
043: import org.apache.commons.logging.Log;
044: import org.apache.commons.logging.LogFactory;
045:
046: /**
047: * <a href="DLFolderServiceImpl.java.html"><b><i>View Source</i></b></a>
048: *
049: * @author Brian Wing Shun Chan
050: *
051: */
052: public class DLFolderServiceImpl extends DLFolderServiceBaseImpl {
053:
054: public DLFolder addFolder(long plid, long parentFolderId,
055: String name, String description,
056: boolean addCommunityPermissions, boolean addGuestPermissions)
057: throws PortalException, SystemException {
058:
059: DLFolderPermission.check(getPermissionChecker(), plid,
060: parentFolderId, ActionKeys.ADD_FOLDER);
061:
062: return dlFolderLocalService.addFolder(getUserId(), plid,
063: parentFolderId, name, description,
064: addCommunityPermissions, addGuestPermissions);
065: }
066:
067: public DLFolder addFolder(long plid, long parentFolderId,
068: String name, String description,
069: String[] communityPermissions, String[] guestPermissions)
070: throws PortalException, SystemException {
071:
072: DLFolderPermission.check(getPermissionChecker(), plid,
073: parentFolderId, ActionKeys.ADD_FOLDER);
074:
075: return dlFolderLocalService.addFolder(getUserId(), plid,
076: parentFolderId, name, description,
077: communityPermissions, guestPermissions);
078: }
079:
080: public DLFolder copyFolder(long plid, long sourceFolderId,
081: long parentFolderId, String name, String description,
082: boolean addCommunityPermissions, boolean addGuestPermissions)
083: throws PortalException, SystemException {
084:
085: DLFolderPermission.check(getPermissionChecker(),
086: sourceFolderId, ActionKeys.VIEW);
087:
088: DLFolder srcFolder = getFolder(sourceFolderId);
089:
090: DLFolderPermission.check(getPermissionChecker(), plid,
091: parentFolderId, ActionKeys.ADD_FOLDER);
092:
093: DLFolder destFolder = addFolder(plid, parentFolderId, name,
094: description, addCommunityPermissions,
095: addGuestPermissions);
096:
097: copyFolder(srcFolder, destFolder, addCommunityPermissions,
098: addGuestPermissions);
099:
100: return destFolder;
101: }
102:
103: public void deleteFolder(long folderId) throws PortalException,
104: SystemException {
105:
106: DLFolderPermission.check(getPermissionChecker(), folderId,
107: ActionKeys.DELETE);
108:
109: dlFolderLocalService.deleteFolder(folderId);
110: }
111:
112: public void deleteFolder(long groupId, long parentFolderId,
113: String name) throws PortalException, SystemException {
114:
115: DLFolder folder = getFolder(groupId, parentFolderId, name);
116:
117: deleteFolder(folder.getFolderId());
118: }
119:
120: public DLFolder getFolder(long folderId) throws PortalException,
121: SystemException {
122:
123: DLFolderPermission.check(getPermissionChecker(), folderId,
124: ActionKeys.VIEW);
125:
126: return dlFolderLocalService.getFolder(folderId);
127: }
128:
129: public DLFolder getFolder(long groupId, long parentFolderId,
130: String name) throws PortalException, SystemException {
131:
132: DLFolder folder = dlFolderLocalService.getFolder(groupId,
133: parentFolderId, name);
134:
135: DLFolderPermission.check(getPermissionChecker(), folder,
136: ActionKeys.VIEW);
137:
138: return folder;
139: }
140:
141: public long getFolderId(long groupId, long parentFolderId,
142: String name) throws PortalException, SystemException {
143:
144: DLFolder folder = getFolder(groupId, parentFolderId, name);
145:
146: return folder.getFolderId();
147: }
148:
149: public void reIndexSearch(long companyId) throws PortalException,
150: SystemException {
151:
152: if (!getPermissionChecker().isOmniadmin()) {
153: throw new PrincipalException();
154: }
155:
156: String[] ids = new String[] { String.valueOf(companyId) };
157:
158: dlFolderLocalService.reIndex(ids);
159: }
160:
161: public DLFolder updateFolder(long folderId, long parentFolderId,
162: String name, String description) throws PortalException,
163: SystemException {
164:
165: DLFolderPermission.check(getPermissionChecker(), folderId,
166: ActionKeys.UPDATE);
167:
168: return dlFolderLocalService.updateFolder(folderId,
169: parentFolderId, name, description);
170: }
171:
172: protected void copyFolder(DLFolder srcFolder, DLFolder destFolder,
173: boolean addCommunityPermissions, boolean addGuestPermissions)
174: throws PortalException, SystemException {
175:
176: long companyId = srcFolder.getCompanyId();
177: long userId = getUserId();
178: long srcFolderId = srcFolder.getFolderId();
179: long destFolderId = destFolder.getFolderId();
180:
181: // Copy all viewable files
182:
183: Iterator itr = dlFileEntryLocalService.getFileEntries(
184: srcFolderId).iterator();
185:
186: while (itr.hasNext()) {
187: DLFileEntry fileEntry = (DLFileEntry) itr.next();
188:
189: if (DLFileEntryPermission.contains(getPermissionChecker(),
190: fileEntry, ActionKeys.VIEW)) {
191:
192: String name = fileEntry.getTitleWithExtension();
193: String title = fileEntry.getTitleWithExtension();
194: String description = fileEntry.getDescription();
195: String[] tagsEntries = null;
196: String extraSettings = fileEntry.getExtraSettings();
197:
198: File file = null;
199:
200: try {
201: InputStream is = dlFileEntryLocalService
202: .getFileAsStream(companyId, userId,
203: srcFolderId, fileEntry.getName());
204:
205: String fileName = SystemProperties
206: .get(SystemProperties.TMP_DIR)
207: + StringPool.SLASH
208: + Time.getTimestamp()
209: + PwdGenerator.getPassword(
210: PwdGenerator.KEY2, 8);
211:
212: file = new File(fileName);
213:
214: FileUtil.write(file, is);
215: } catch (Exception e) {
216: _log.error(e, e);
217:
218: continue;
219: }
220:
221: dlFileEntryLocalService.addFileEntry(userId,
222: destFolderId, name, title, description,
223: tagsEntries, extraSettings, file,
224: addCommunityPermissions, addGuestPermissions);
225: }
226: }
227:
228: String uuid = StringPool.BLANK;
229: long groupId = destFolder.getGroupId();
230: Boolean addCommunityPermissionsObj = Boolean
231: .valueOf(addCommunityPermissions);
232: Boolean addGuestPermissionsObj = Boolean
233: .valueOf(addGuestPermissions);
234: String[] communityPermissions = null;
235: String[] guestPermissions = null;
236:
237: // Copy all viewable folders
238:
239: itr = dlFolderLocalService.getFolders(srcFolder.getGroupId(),
240: srcFolderId).iterator();
241:
242: while (itr.hasNext()) {
243: DLFolder folder = (DLFolder) itr.next();
244:
245: if (DLFolderPermission.contains(getPermissionChecker(),
246: folder, ActionKeys.VIEW)) {
247:
248: String name = folder.getName();
249: String description = folder.getDescription();
250:
251: DLFolder subfolder = dlFolderLocalService
252: .addFolderToGroup(uuid, userId, groupId,
253: destFolderId, name, description,
254: addCommunityPermissionsObj,
255: addGuestPermissionsObj,
256: communityPermissions, guestPermissions);
257:
258: // Recursively copy all subfolders
259:
260: copyFolder(folder, subfolder, addCommunityPermissions,
261: addGuestPermissions);
262: }
263: }
264: }
265:
266: private static Log _log = LogFactory
267: .getLog(DLFolderServiceImpl.class);
268:
269: }
|