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.documentlibrary.DuplicateFileException;
024: import com.liferay.portal.NoSuchLayoutException;
025: import com.liferay.portal.PortalException;
026: import com.liferay.portal.SystemException;
027: import com.liferay.portal.kernel.search.Hits;
028: import com.liferay.portal.kernel.util.GetterUtil;
029: import com.liferay.portal.kernel.util.LocaleUtil;
030: import com.liferay.portal.kernel.util.StringPool;
031: import com.liferay.portal.kernel.util.Validator;
032: import com.liferay.portal.model.Layout;
033: import com.liferay.portal.model.User;
034: import com.liferay.portal.model.impl.LayoutImpl;
035: import com.liferay.portal.model.impl.ResourceImpl;
036: import com.liferay.portal.util.PortalUtil;
037: import com.liferay.portal.util.PortletKeys;
038: import com.liferay.portal.util.PropsUtil;
039: import com.liferay.portal.util.PropsValues;
040: import com.liferay.portlet.documentlibrary.DuplicateFolderNameException;
041: import com.liferay.portlet.documentlibrary.FolderNameException;
042: import com.liferay.portlet.documentlibrary.NoSuchFileEntryException;
043: import com.liferay.portlet.documentlibrary.NoSuchFolderException;
044: import com.liferay.portlet.documentlibrary.model.DLFolder;
045: import com.liferay.portlet.documentlibrary.model.impl.DLFolderImpl;
046: import com.liferay.portlet.documentlibrary.service.base.DLFolderLocalServiceBaseImpl;
047:
048: import java.util.ArrayList;
049: import java.util.Date;
050: import java.util.Iterator;
051: import java.util.List;
052:
053: /**
054: * <a href="DLFolderLocalServiceImpl.java.html"><b><i>View Source</i></b></a>
055: *
056: * @author Brian Wing Shun Chan
057: *
058: */
059: public class DLFolderLocalServiceImpl extends
060: DLFolderLocalServiceBaseImpl {
061:
062: public DLFolder addFolder(long userId, long plid,
063: long parentFolderId, String name, String description,
064: boolean addCommunityPermissions, boolean addGuestPermissions)
065: throws PortalException, SystemException {
066:
067: return addFolder(null, userId, plid, parentFolderId, name,
068: description, Boolean.valueOf(addCommunityPermissions),
069: Boolean.valueOf(addGuestPermissions), null, null);
070: }
071:
072: public DLFolder addFolder(String uuid, long userId, long plid,
073: long parentFolderId, String name, String description,
074: boolean addCommunityPermissions, boolean addGuestPermissions)
075: throws PortalException, SystemException {
076:
077: return addFolder(uuid, userId, plid, parentFolderId, name,
078: description, Boolean.valueOf(addCommunityPermissions),
079: Boolean.valueOf(addGuestPermissions), null, null);
080: }
081:
082: public DLFolder addFolder(long userId, long plid,
083: long parentFolderId, String name, String description,
084: String[] communityPermissions, String[] guestPermissions)
085: throws PortalException, SystemException {
086:
087: return addFolder(null, userId, plid, parentFolderId, name,
088: description, null, null, communityPermissions,
089: guestPermissions);
090: }
091:
092: public DLFolder addFolder(String uuid, long userId, long plid,
093: long parentFolderId, String name, String description,
094: Boolean addCommunityPermissions,
095: Boolean addGuestPermissions, String[] communityPermissions,
096: String[] guestPermissions) throws PortalException,
097: SystemException {
098:
099: long groupId = PortalUtil.getPortletGroupId(plid);
100:
101: return addFolderToGroup(uuid, userId, groupId, parentFolderId,
102: name, description, addCommunityPermissions,
103: addGuestPermissions, communityPermissions,
104: guestPermissions);
105: }
106:
107: public DLFolder addFolderToGroup(String uuid, long userId,
108: long groupId, long parentFolderId, String name,
109: String description, Boolean addCommunityPermissions,
110: Boolean addGuestPermissions, String[] communityPermissions,
111: String[] guestPermissions) throws PortalException,
112: SystemException {
113:
114: // Folder
115:
116: User user = userPersistence.findByPrimaryKey(userId);
117: parentFolderId = getParentFolderId(groupId, parentFolderId);
118: Date now = new Date();
119:
120: validate(groupId, parentFolderId, name);
121:
122: long folderId = counterLocalService.increment();
123:
124: DLFolder folder = dlFolderPersistence.create(folderId);
125:
126: folder.setUuid(uuid);
127: folder.setGroupId(groupId);
128: folder.setCompanyId(user.getCompanyId());
129: folder.setUserId(user.getUserId());
130: folder.setCreateDate(now);
131: folder.setModifiedDate(now);
132: folder.setParentFolderId(parentFolderId);
133: folder.setName(name);
134: folder.setDescription(description);
135:
136: dlFolderPersistence.update(folder);
137:
138: // Resources
139:
140: if ((addCommunityPermissions != null)
141: && (addGuestPermissions != null)) {
142:
143: addFolderResources(folder, addCommunityPermissions
144: .booleanValue(), addGuestPermissions.booleanValue());
145: } else {
146: addFolderResources(folder, communityPermissions,
147: guestPermissions);
148: }
149:
150: // Layout
151:
152: String[] pathArray = folder.getPathArray();
153:
154: if (PropsValues.DL_LAYOUTS_SYNC_ENABLED
155: && (parentFolderId != DLFolderImpl.DEFAULT_PARENT_FOLDER_ID)) {
156:
157: String layoutsSyncPrivateFolder = GetterUtil
158: .getString(PropsUtil
159: .get(PropsUtil.DL_LAYOUTS_SYNC_PRIVATE_FOLDER));
160: String layoutsSyncPublicFolder = GetterUtil
161: .getString(PropsUtil
162: .get(PropsUtil.DL_LAYOUTS_SYNC_PUBLIC_FOLDER));
163:
164: if (pathArray[0].equals(layoutsSyncPrivateFolder)
165: || pathArray[0].equals(layoutsSyncPublicFolder)) {
166:
167: boolean privateLayout = true;
168:
169: if (pathArray[0].equals(layoutsSyncPublicFolder)) {
170: privateLayout = false;
171: }
172:
173: long parentLayoutId = LayoutImpl.DEFAULT_PARENT_LAYOUT_ID;
174: String title = StringPool.BLANK;
175: String layoutDescription = StringPool.BLANK;
176: String type = LayoutImpl.TYPE_PORTLET;
177: boolean hidden = false;
178: String friendlyURL = StringPool.BLANK;
179:
180: Layout dlFolderLayout = null;
181:
182: try {
183: dlFolderLayout = layoutLocalService
184: .getDLFolderLayout(folder
185: .getParentFolderId());
186:
187: parentLayoutId = dlFolderLayout.getLayoutId();
188: } catch (NoSuchLayoutException nsle) {
189: }
190:
191: layoutLocalService.addLayout(userId, groupId,
192: privateLayout, parentLayoutId, name, title,
193: layoutDescription, type, hidden, friendlyURL,
194: folder.getFolderId());
195: }
196: }
197:
198: return folder;
199: }
200:
201: public void addFolderResources(long folderId,
202: boolean addCommunityPermissions, boolean addGuestPermissions)
203: throws PortalException, SystemException {
204:
205: DLFolder folder = dlFolderPersistence
206: .findByPrimaryKey(folderId);
207:
208: addFolderResources(folder, addCommunityPermissions,
209: addGuestPermissions);
210: }
211:
212: public void addFolderResources(DLFolder folder,
213: boolean addCommunityPermissions, boolean addGuestPermissions)
214: throws PortalException, SystemException {
215:
216: resourceLocalService.addResources(folder.getCompanyId(), folder
217: .getGroupId(), folder.getUserId(), DLFolder.class
218: .getName(), folder.getFolderId(), false,
219: addCommunityPermissions, addGuestPermissions);
220: }
221:
222: public void addFolderResources(long folderId,
223: String[] communityPermissions, String[] guestPermissions)
224: throws PortalException, SystemException {
225:
226: DLFolder folder = dlFolderPersistence
227: .findByPrimaryKey(folderId);
228:
229: addFolderResources(folder, communityPermissions,
230: guestPermissions);
231: }
232:
233: public void addFolderResources(DLFolder folder,
234: String[] communityPermissions, String[] guestPermissions)
235: throws PortalException, SystemException {
236:
237: resourceLocalService.addModelResources(folder.getCompanyId(),
238: folder.getGroupId(), folder.getUserId(), DLFolder.class
239: .getName(), folder.getFolderId(),
240: communityPermissions, guestPermissions);
241: }
242:
243: public void deleteFolder(long folderId) throws PortalException,
244: SystemException {
245:
246: DLFolder folder = dlFolderPersistence
247: .findByPrimaryKey(folderId);
248:
249: deleteFolder(folder);
250: }
251:
252: public void deleteFolder(DLFolder folder) throws PortalException,
253: SystemException {
254:
255: // Folders
256:
257: Iterator itr = dlFolderPersistence.findByG_P(
258: folder.getGroupId(), folder.getFolderId()).iterator();
259:
260: while (itr.hasNext()) {
261: DLFolder curFolder = (DLFolder) itr.next();
262:
263: deleteFolder(curFolder);
264: }
265:
266: // File entries
267:
268: dlFileEntryLocalService.deleteFileEntries(folder.getFolderId());
269:
270: // Resources
271:
272: resourceLocalService.deleteResource(folder.getCompanyId(),
273: DLFolder.class.getName(),
274: ResourceImpl.SCOPE_INDIVIDUAL, folder.getFolderId());
275:
276: // WebDAVProps
277:
278: webDAVPropsLocalService.deleteWebDAVProps(DLFolder.class
279: .getName(), folder.getPrimaryKey());
280:
281: // Folder
282:
283: dlFolderPersistence.remove(folder.getFolderId());
284: }
285:
286: public void deleteFolders(long groupId) throws PortalException,
287: SystemException {
288:
289: Iterator itr = dlFolderPersistence.findByG_P(groupId,
290: DLFolderImpl.DEFAULT_PARENT_FOLDER_ID).iterator();
291:
292: while (itr.hasNext()) {
293: DLFolder folder = (DLFolder) itr.next();
294:
295: deleteFolder(folder);
296: }
297: }
298:
299: public DLFolder getFolder(long folderId) throws PortalException,
300: SystemException {
301:
302: return dlFolderPersistence.findByPrimaryKey(folderId);
303: }
304:
305: public DLFolder getFolder(long groupId, long parentFolderId,
306: String name) throws PortalException, SystemException {
307:
308: return dlFolderPersistence.findByG_P_N(groupId, parentFolderId,
309: name);
310: }
311:
312: public List getFolders(long companyId) throws SystemException {
313: return dlFolderPersistence.findByCompanyId(companyId);
314: }
315:
316: public List getFolders(long groupId, long parentFolderId)
317: throws SystemException {
318:
319: return dlFolderPersistence.findByG_P(groupId, parentFolderId);
320: }
321:
322: public List getFolders(long groupId, long parentFolderId,
323: int begin, int end) throws SystemException {
324:
325: return dlFolderPersistence.findByG_P(groupId, parentFolderId,
326: begin, end);
327: }
328:
329: public int getFoldersCount(long groupId, long parentFolderId)
330: throws SystemException {
331:
332: return dlFolderPersistence.countByG_P(groupId, parentFolderId);
333: }
334:
335: public void getSubfolderIds(List folderIds, long groupId,
336: long folderId) throws SystemException {
337:
338: Iterator itr = dlFolderPersistence.findByG_P(groupId, folderId)
339: .iterator();
340:
341: while (itr.hasNext()) {
342: DLFolder folder = (DLFolder) itr.next();
343:
344: folderIds.add(new Long(folder.getFolderId()));
345:
346: getSubfolderIds(folderIds, folder.getGroupId(), folder
347: .getFolderId());
348: }
349: }
350:
351: public void reIndex(String[] ids) throws SystemException {
352: long companyId = GetterUtil.getLong(ids[0]);
353:
354: try {
355: List folders = getFolders(companyId);
356:
357: for (int i = 0; i < folders.size(); i++) {
358: DLFolder folder = (DLFolder) folders.get(i);
359:
360: String portletId = PortletKeys.DOCUMENT_LIBRARY;
361: long groupId = folder.getGroupId();
362: long folderId = folder.getFolderId();
363:
364: String[] newIds = { String.valueOf(companyId),
365: portletId, String.valueOf(groupId),
366: String.valueOf(folderId) };
367:
368: dlService.reIndex(newIds);
369: }
370: } catch (SystemException se) {
371: throw se;
372: } catch (Exception e) {
373: throw new SystemException(e);
374: }
375: }
376:
377: public Hits search(long companyId, long groupId, long[] folderIds,
378: String keywords) throws PortalException, SystemException {
379:
380: return dlLocalService.search(companyId,
381: PortletKeys.DOCUMENT_LIBRARY, groupId, folderIds,
382: keywords);
383: }
384:
385: public DLFolder updateFolder(long folderId, long parentFolderId,
386: String name, String description) throws PortalException,
387: SystemException {
388:
389: DLFolder folder = dlFolderPersistence
390: .findByPrimaryKey(folderId);
391:
392: parentFolderId = getParentFolderId(folder, parentFolderId);
393:
394: validate(folder.getGroupId(), parentFolderId, name);
395:
396: folder.setModifiedDate(new Date());
397: folder.setParentFolderId(parentFolderId);
398: folder.setName(name);
399: folder.setDescription(description);
400:
401: dlFolderPersistence.update(folder);
402:
403: if (PropsValues.DL_LAYOUTS_SYNC_ENABLED) {
404: String privateFolder = GetterUtil.getString(PropsUtil
405: .get(PropsUtil.DL_LAYOUTS_SYNC_PRIVATE_FOLDER));
406:
407: boolean privateLayout = false;
408:
409: String[] path = folder.getPathArray();
410:
411: if (path[0].equals(privateFolder)) {
412: privateLayout = true;
413: }
414:
415: Layout layout = layoutLocalService.getDLFolderLayout(folder
416: .getFolderId());
417:
418: layout.setName(folder.getName());
419:
420: layoutLocalService.updateName(folder.getGroupId(),
421: privateLayout, layout.getLayoutId(), folder
422: .getName(), LocaleUtil
423: .toLanguageId(LocaleUtil.getDefault()));
424: }
425:
426: return folder;
427: }
428:
429: protected long getParentFolderId(long groupId, long parentFolderId)
430: throws SystemException {
431:
432: if (parentFolderId != DLFolderImpl.DEFAULT_PARENT_FOLDER_ID) {
433: DLFolder parentFolder = dlFolderPersistence
434: .fetchByPrimaryKey(parentFolderId);
435:
436: if ((parentFolder == null)
437: || (groupId != parentFolder.getGroupId())) {
438:
439: parentFolderId = DLFolderImpl.DEFAULT_PARENT_FOLDER_ID;
440: }
441: }
442:
443: return parentFolderId;
444: }
445:
446: protected long getParentFolderId(DLFolder folder,
447: long parentFolderId) throws SystemException {
448:
449: if (parentFolderId == DLFolderImpl.DEFAULT_PARENT_FOLDER_ID) {
450: return parentFolderId;
451: }
452:
453: if (folder.getFolderId() == parentFolderId) {
454: return folder.getParentFolderId();
455: } else {
456: DLFolder parentFolder = dlFolderPersistence
457: .fetchByPrimaryKey(parentFolderId);
458:
459: if ((parentFolder == null)
460: || (folder.getGroupId() != parentFolder
461: .getGroupId())) {
462:
463: return folder.getParentFolderId();
464: }
465:
466: List subfolderIds = new ArrayList();
467:
468: getSubfolderIds(subfolderIds, folder.getGroupId(), folder
469: .getFolderId());
470:
471: if (subfolderIds.contains(new Long(parentFolderId))) {
472: return folder.getParentFolderId();
473: }
474:
475: return parentFolderId;
476: }
477: }
478:
479: protected void validate(long groupId, long parentFolderId,
480: String name) throws PortalException, SystemException {
481:
482: if ((Validator.isNull(name)) || (name.indexOf("\\\\") != -1)
483: || (name.indexOf("//") != -1)) {
484:
485: throw new FolderNameException();
486: }
487:
488: try {
489: dlFileEntryLocalService.getFileEntryByTitle(parentFolderId,
490: name);
491:
492: throw new DuplicateFileException();
493: } catch (NoSuchFileEntryException nsfee) {
494: }
495:
496: try {
497: dlFolderPersistence.findByG_P_N(groupId, parentFolderId,
498: name);
499:
500: throw new DuplicateFolderNameException();
501: } catch (NoSuchFolderException nsfe) {
502: }
503: }
504:
505: }
|