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.model.User;
026: import com.liferay.portal.model.impl.ResourceImpl;
027: import com.liferay.portlet.documentlibrary.NoSuchFileEntryException;
028: import com.liferay.portlet.documentlibrary.NoSuchFolderException;
029: import com.liferay.portlet.documentlibrary.model.DLFileEntry;
030: import com.liferay.portlet.documentlibrary.model.DLFileShortcut;
031: import com.liferay.portlet.documentlibrary.model.DLFolder;
032: import com.liferay.portlet.documentlibrary.model.impl.DLFolderImpl;
033: import com.liferay.portlet.documentlibrary.service.base.DLFileShortcutLocalServiceBaseImpl;
034:
035: import java.util.Date;
036: import java.util.Iterator;
037:
038: /**
039: * <a href="DLFileShortcutLocalServiceImpl.java.html"><b><i>View Source</i></b>
040: * </a>
041: *
042: * @author Brian Wing Shun Chan
043: *
044: */
045: public class DLFileShortcutLocalServiceImpl extends
046: DLFileShortcutLocalServiceBaseImpl {
047:
048: public DLFileShortcut addFileShortcut(long userId, long folderId,
049: long toFolderId, String toName,
050: boolean addCommunityPermissions, boolean addGuestPermissions)
051: throws PortalException, SystemException {
052:
053: return addFileShortcut(null, userId, folderId, toFolderId,
054: toName, Boolean.valueOf(addCommunityPermissions),
055: Boolean.valueOf(addGuestPermissions), null, null);
056: }
057:
058: public DLFileShortcut addFileShortcut(String uuid, long userId,
059: long folderId, long toFolderId, String toName,
060: boolean addCommunityPermissions, boolean addGuestPermissions)
061: throws PortalException, SystemException {
062:
063: return addFileShortcut(uuid, userId, folderId, toFolderId,
064: toName, Boolean.valueOf(addCommunityPermissions),
065: Boolean.valueOf(addGuestPermissions), null, null);
066: }
067:
068: public DLFileShortcut addFileShortcut(long userId, long folderId,
069: long toFolderId, String toName,
070: String[] communityPermissions, String[] guestPermissions)
071: throws PortalException, SystemException {
072:
073: return addFileShortcut(null, userId, folderId, toFolderId,
074: toName, null, null, communityPermissions,
075: guestPermissions);
076: }
077:
078: public DLFileShortcut addFileShortcut(String uuid, long userId,
079: long folderId, long toFolderId, String toName,
080: Boolean addCommunityPermissions,
081: Boolean addGuestPermissions, String[] communityPermissions,
082: String[] guestPermissions) throws PortalException,
083: SystemException {
084:
085: // File shortcut
086:
087: User user = userPersistence.findByPrimaryKey(userId);
088: folderId = getFolderId(user.getCompanyId(), folderId);
089: DLFolder folder = dlFolderPersistence
090: .findByPrimaryKey(folderId);
091: Date now = new Date();
092:
093: validate(user, toFolderId, toName);
094:
095: long fileShortcutId = counterLocalService.increment();
096:
097: DLFileShortcut fileShortcut = dlFileShortcutPersistence
098: .create(fileShortcutId);
099:
100: fileShortcut.setUuid(uuid);
101: fileShortcut.setCompanyId(user.getCompanyId());
102: fileShortcut.setUserId(user.getUserId());
103: fileShortcut.setUserName(user.getFullName());
104: fileShortcut.setCreateDate(now);
105: fileShortcut.setModifiedDate(now);
106: fileShortcut.setFolderId(folderId);
107: fileShortcut.setToFolderId(toFolderId);
108: fileShortcut.setToName(toName);
109:
110: dlFileShortcutPersistence.update(fileShortcut);
111:
112: // Resources
113:
114: if ((addCommunityPermissions != null)
115: && (addGuestPermissions != null)) {
116:
117: addFileShortcutResources(folder, fileShortcut,
118: addCommunityPermissions.booleanValue(),
119: addGuestPermissions.booleanValue());
120: } else {
121: addFileShortcutResources(folder, fileShortcut,
122: communityPermissions, guestPermissions);
123: }
124:
125: // Folder
126:
127: folder.setLastPostDate(fileShortcut.getModifiedDate());
128:
129: dlFolderPersistence.update(folder);
130:
131: return fileShortcut;
132: }
133:
134: public void addFileShortcutResources(long fileShortcutId,
135: boolean addCommunityPermissions, boolean addGuestPermissions)
136: throws PortalException, SystemException {
137:
138: DLFileShortcut fileShortcut = dlFileShortcutPersistence
139: .findByPrimaryKey(fileShortcutId);
140: DLFolder folder = fileShortcut.getFolder();
141:
142: addFileShortcutResources(folder, fileShortcut,
143: addCommunityPermissions, addGuestPermissions);
144: }
145:
146: public void addFileShortcutResources(DLFolder folder,
147: DLFileShortcut fileShortcut,
148: boolean addCommunityPermissions, boolean addGuestPermissions)
149: throws PortalException, SystemException {
150:
151: resourceLocalService.addResources(fileShortcut.getCompanyId(),
152: folder.getGroupId(), fileShortcut.getUserId(),
153: DLFileShortcut.class.getName(), fileShortcut
154: .getFileShortcutId(), false,
155: addCommunityPermissions, addGuestPermissions);
156: }
157:
158: public void addFileShortcutResources(long fileShortcutId,
159: String[] communityPermissions, String[] guestPermissions)
160: throws PortalException, SystemException {
161:
162: DLFileShortcut fileShortcut = dlFileShortcutPersistence
163: .findByPrimaryKey(fileShortcutId);
164: DLFolder folder = fileShortcut.getFolder();
165:
166: addFileShortcutResources(folder, fileShortcut,
167: communityPermissions, guestPermissions);
168: }
169:
170: public void addFileShortcutResources(DLFolder folder,
171: DLFileShortcut fileShortcut, String[] communityPermissions,
172: String[] guestPermissions) throws PortalException,
173: SystemException {
174:
175: resourceLocalService.addModelResources(fileShortcut
176: .getCompanyId(), folder.getGroupId(), fileShortcut
177: .getUserId(), DLFileShortcut.class.getName(),
178: fileShortcut.getFileShortcutId(), communityPermissions,
179: guestPermissions);
180: }
181:
182: public void deleteFileShortcut(long fileShortcutId)
183: throws PortalException, SystemException {
184:
185: dlFileShortcutPersistence.remove(fileShortcutId);
186: }
187:
188: public void deleteFileShortcut(DLFileShortcut fileShortcut)
189: throws PortalException, SystemException {
190:
191: // Resources
192:
193: resourceLocalService.deleteResource(
194: fileShortcut.getCompanyId(), DLFileShortcut.class
195: .getName(), ResourceImpl.SCOPE_INDIVIDUAL,
196: fileShortcut.getFileShortcutId());
197:
198: // File shortcut
199:
200: dlFileShortcutPersistence.remove(fileShortcut
201: .getFileShortcutId());
202: }
203:
204: public void deleteFileShortcuts(long toFolderId, String toName)
205: throws PortalException, SystemException {
206:
207: Iterator itr = dlFileShortcutPersistence.findByTF_TN(
208: toFolderId, toName).iterator();
209:
210: while (itr.hasNext()) {
211: DLFileShortcut fileShortcut = (DLFileShortcut) itr.next();
212:
213: deleteFileShortcut(fileShortcut);
214: }
215: }
216:
217: public DLFileShortcut getFileShortcut(long fileShortcutId)
218: throws PortalException, SystemException {
219:
220: return dlFileShortcutPersistence
221: .findByPrimaryKey(fileShortcutId);
222: }
223:
224: public DLFileShortcut updateFileShortcut(long userId,
225: long fileShortcutId, long folderId, long toFolderId,
226: String toName) throws PortalException, SystemException {
227:
228: // File shortcut
229:
230: User user = userPersistence.findByPrimaryKey(userId);
231: DLFolder folder = dlFolderPersistence
232: .findByPrimaryKey(folderId);
233:
234: validate(user, toFolderId, toName);
235:
236: DLFileShortcut fileShortcut = dlFileShortcutPersistence
237: .findByPrimaryKey(fileShortcutId);
238:
239: fileShortcut.setModifiedDate(new Date());
240: fileShortcut.setFolderId(folderId);
241: fileShortcut.setToFolderId(toFolderId);
242: fileShortcut.setToName(toName);
243:
244: dlFileShortcutPersistence.update(fileShortcut);
245:
246: // Folder
247:
248: folder.setLastPostDate(fileShortcut.getModifiedDate());
249:
250: dlFolderPersistence.update(folder);
251:
252: return fileShortcut;
253: }
254:
255: public void updateFileShortcuts(long oldToFolderId,
256: String oldToName, long newToFolderId, String newToName)
257: throws PortalException, SystemException {
258:
259: Iterator itr = dlFileShortcutPersistence.findByTF_TN(
260: oldToFolderId, oldToName).iterator();
261:
262: while (itr.hasNext()) {
263: DLFileShortcut fileShortcut = (DLFileShortcut) itr.next();
264:
265: fileShortcut.setToFolderId(newToFolderId);
266: fileShortcut.setToName(newToName);
267:
268: dlFileShortcutPersistence.update(fileShortcut);
269: }
270: }
271:
272: protected long getFolderId(long companyId, long folderId)
273: throws PortalException, SystemException {
274:
275: if (folderId != DLFolderImpl.DEFAULT_PARENT_FOLDER_ID) {
276:
277: // Ensure folder exists and belongs to the proper company
278:
279: try {
280: DLFolder folder = dlFolderPersistence
281: .findByPrimaryKey(folderId);
282:
283: if (companyId != folder.getCompanyId()) {
284: folderId = DLFolderImpl.DEFAULT_PARENT_FOLDER_ID;
285: }
286: } catch (NoSuchFolderException nsfe) {
287: folderId = DLFolderImpl.DEFAULT_PARENT_FOLDER_ID;
288: }
289: }
290:
291: return folderId;
292: }
293:
294: protected void validate(User user, long toFolderId, String toName)
295: throws PortalException, SystemException {
296:
297: DLFileEntry fileEntry = dlFileEntryLocalService.getFileEntry(
298: toFolderId, toName);
299:
300: if (user.getCompanyId() != fileEntry.getCompanyId()) {
301: throw new NoSuchFileEntryException();
302: }
303: }
304:
305: }
|