1   /**
2    * Copyright (c) 2000-2007 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portlet.documentlibrary.service.impl;
24  
25  import com.liferay.counter.service.CounterLocalServiceUtil;
26  import com.liferay.portal.PortalException;
27  import com.liferay.portal.SystemException;
28  import com.liferay.portal.model.User;
29  import com.liferay.portal.model.impl.ResourceImpl;
30  import com.liferay.portal.service.ResourceLocalServiceUtil;
31  import com.liferay.portal.service.persistence.UserUtil;
32  import com.liferay.portlet.documentlibrary.NoSuchFileEntryException;
33  import com.liferay.portlet.documentlibrary.NoSuchFolderException;
34  import com.liferay.portlet.documentlibrary.model.DLFileEntry;
35  import com.liferay.portlet.documentlibrary.model.DLFileShortcut;
36  import com.liferay.portlet.documentlibrary.model.DLFolder;
37  import com.liferay.portlet.documentlibrary.model.impl.DLFolderImpl;
38  import com.liferay.portlet.documentlibrary.service.DLFileEntryLocalServiceUtil;
39  import com.liferay.portlet.documentlibrary.service.base.DLFileShortcutLocalServiceBaseImpl;
40  import com.liferay.portlet.documentlibrary.service.persistence.DLFileShortcutUtil;
41  import com.liferay.portlet.documentlibrary.service.persistence.DLFolderUtil;
42  
43  import java.util.Date;
44  import java.util.Iterator;
45  
46  /**
47   * <a href="DLFileShortcutLocalServiceImpl.java.html"><b><i>View Source</i></b>
48   * </a>
49   *
50   * @author Brian Wing Shun Chan
51   *
52   */
53  public class DLFileShortcutLocalServiceImpl
54      extends DLFileShortcutLocalServiceBaseImpl {
55  
56      public DLFileShortcut addFileShortcut(
57              long userId, long folderId, long toFolderId, String toName,
58              boolean addCommunityPermissions, boolean addGuestPermissions)
59          throws PortalException, SystemException {
60  
61          return addFileShortcut(
62              userId, folderId, toFolderId, toName,
63              Boolean.valueOf(addCommunityPermissions),
64              Boolean.valueOf(addGuestPermissions), null, null);
65      }
66  
67      public DLFileShortcut addFileShortcut(
68              long userId, long folderId, long toFolderId, String toName,
69              String[] communityPermissions, String[] guestPermissions)
70          throws PortalException, SystemException {
71  
72          return addFileShortcut(
73              userId, folderId, toFolderId, toName, null, null,
74              communityPermissions, guestPermissions);
75      }
76  
77      public DLFileShortcut addFileShortcut(
78              long userId, long folderId, long toFolderId, String toName,
79              Boolean addCommunityPermissions, Boolean addGuestPermissions,
80              String[] communityPermissions, String[] guestPermissions)
81          throws PortalException, SystemException {
82  
83          // File shortcut
84  
85          User user = UserUtil.findByPrimaryKey(userId);
86          folderId = getFolderId(user.getCompanyId(), folderId);
87          DLFolder folder = DLFolderUtil.findByPrimaryKey(folderId);
88          Date now = new Date();
89  
90          validate(user, toFolderId, toName);
91  
92          long fileShortcutId = CounterLocalServiceUtil.increment();
93  
94          DLFileShortcut fileShortcut = DLFileShortcutUtil.create(fileShortcutId);
95  
96          fileShortcut.setCompanyId(user.getCompanyId());
97          fileShortcut.setUserId(user.getUserId());
98          fileShortcut.setUserName(user.getFullName());
99          fileShortcut.setCreateDate(now);
100         fileShortcut.setModifiedDate(now);
101         fileShortcut.setFolderId(folderId);
102         fileShortcut.setToFolderId(toFolderId);
103         fileShortcut.setToName(toName);
104 
105         DLFileShortcutUtil.update(fileShortcut);
106 
107         // Resources
108 
109         if ((addCommunityPermissions != null) &&
110             (addGuestPermissions != null)) {
111 
112             addFileShortcutResources(
113                 folder, fileShortcut, addCommunityPermissions.booleanValue(),
114                 addGuestPermissions.booleanValue());
115         }
116         else {
117             addFileShortcutResources(
118                 folder, fileShortcut, communityPermissions, guestPermissions);
119         }
120 
121         // Folder
122 
123         folder.setLastPostDate(fileShortcut.getModifiedDate());
124 
125         DLFolderUtil.update(folder);
126 
127         return fileShortcut;
128     }
129 
130     public void addFileShortcutResources(
131             long fileShortcutId, boolean addCommunityPermissions,
132             boolean addGuestPermissions)
133         throws PortalException, SystemException {
134 
135         DLFileShortcut fileShortcut = DLFileShortcutUtil.findByPrimaryKey(
136             fileShortcutId);
137         DLFolder folder = fileShortcut.getFolder();
138 
139         addFileShortcutResources(
140             folder, fileShortcut, addCommunityPermissions, addGuestPermissions);
141     }
142 
143     public void addFileShortcutResources(
144             DLFolder folder, DLFileShortcut fileShortcut,
145             boolean addCommunityPermissions, boolean addGuestPermissions)
146         throws PortalException, SystemException {
147 
148         ResourceLocalServiceUtil.addResources(
149             fileShortcut.getCompanyId(), folder.getGroupId(),
150             fileShortcut.getUserId(), DLFileShortcut.class.getName(),
151             fileShortcut.getFileShortcutId(), false, addCommunityPermissions,
152             addGuestPermissions);
153     }
154 
155     public void addFileShortcutResources(
156             long fileShortcutId, String[] communityPermissions,
157             String[] guestPermissions)
158         throws PortalException, SystemException {
159 
160         DLFileShortcut fileShortcut = DLFileShortcutUtil.findByPrimaryKey(
161             fileShortcutId);
162         DLFolder folder = fileShortcut.getFolder();
163 
164         addFileShortcutResources(
165             folder, fileShortcut, communityPermissions, guestPermissions);
166     }
167 
168     public void addFileShortcutResources(
169             DLFolder folder, DLFileShortcut fileShortcut,
170             String[] communityPermissions, String[] guestPermissions)
171         throws PortalException, SystemException {
172 
173         ResourceLocalServiceUtil.addModelResources(
174             fileShortcut.getCompanyId(), folder.getGroupId(),
175             fileShortcut.getUserId(), DLFileShortcut.class.getName(),
176             fileShortcut.getFileShortcutId(), communityPermissions,
177             guestPermissions);
178     }
179 
180     public void deleteFileShortcut(long fileShortcutId)
181         throws PortalException, SystemException {
182 
183         DLFileShortcutUtil.remove(fileShortcutId);
184     }
185 
186     public void deleteFileShortcut(DLFileShortcut fileShortcut)
187         throws PortalException, SystemException {
188 
189         // Resources
190 
191         ResourceLocalServiceUtil.deleteResource(
192             fileShortcut.getCompanyId(), DLFileShortcut.class.getName(),
193             ResourceImpl.SCOPE_INDIVIDUAL, fileShortcut.getFileShortcutId());
194 
195         // File shortcut
196 
197         DLFileShortcutUtil.remove(fileShortcut.getFileShortcutId());
198     }
199 
200     public void deleteFileShortcuts(long toFolderId, String toName)
201         throws PortalException, SystemException {
202 
203         Iterator itr = DLFileShortcutUtil.findByTF_TN(
204             toFolderId, toName).iterator();
205 
206         while (itr.hasNext()) {
207             DLFileShortcut fileShortcut = (DLFileShortcut)itr.next();
208 
209             deleteFileShortcut(fileShortcut);
210         }
211     }
212 
213     public DLFileShortcut getFileShortcut(long fileShortcutId)
214         throws PortalException, SystemException {
215 
216         return DLFileShortcutUtil.findByPrimaryKey(fileShortcutId);
217     }
218 
219     public DLFileShortcut updateFileShortcut(
220             long userId, long fileShortcutId, long folderId,
221             long toFolderId, String toName)
222         throws PortalException, SystemException {
223 
224         // File shortcut
225 
226         User user = UserUtil.findByPrimaryKey(userId);
227         DLFolder folder = DLFolderUtil.findByPrimaryKey(folderId);
228 
229         validate(user, toFolderId, toName);
230 
231         DLFileShortcut fileShortcut = DLFileShortcutUtil.findByPrimaryKey(
232             fileShortcutId);
233 
234         fileShortcut.setModifiedDate(new Date());
235         fileShortcut.setFolderId(folderId);
236         fileShortcut.setToFolderId(toFolderId);
237         fileShortcut.setToName(toName);
238 
239         DLFileShortcutUtil.update(fileShortcut);
240 
241         // Folder
242 
243         folder.setLastPostDate(fileShortcut.getModifiedDate());
244 
245         DLFolderUtil.update(folder);
246 
247         return fileShortcut;
248     }
249 
250     public void updateFileShortcuts(
251             long oldToFolderId, String oldToName, long newToFolderId,
252             String newToName)
253         throws PortalException, SystemException {
254 
255         Iterator itr = DLFileShortcutUtil.findByTF_TN(
256             oldToFolderId, oldToName).iterator();
257 
258         while (itr.hasNext()) {
259             DLFileShortcut fileShortcut = (DLFileShortcut)itr.next();
260 
261             fileShortcut.setToFolderId(newToFolderId);
262             fileShortcut.setToName(newToName);
263 
264             DLFileShortcutUtil.update(fileShortcut);
265         }
266     }
267 
268     protected long getFolderId(long companyId, long folderId)
269         throws PortalException, SystemException {
270 
271         if (folderId != DLFolderImpl.DEFAULT_PARENT_FOLDER_ID) {
272 
273             // Ensure folder exists and belongs to the proper company
274 
275             try {
276                 DLFolder folder = DLFolderUtil.findByPrimaryKey(folderId);
277 
278                 if (companyId != folder.getCompanyId()) {
279                     folderId = DLFolderImpl.DEFAULT_PARENT_FOLDER_ID;
280                 }
281             }
282             catch (NoSuchFolderException nsfe) {
283                 folderId = DLFolderImpl.DEFAULT_PARENT_FOLDER_ID;
284             }
285         }
286 
287         return folderId;
288     }
289 
290     protected void validate(User user, long toFolderId, String toName)
291         throws PortalException, SystemException {
292 
293         DLFileEntry fileEntry = DLFileEntryLocalServiceUtil.getFileEntry(
294             toFolderId, toName);
295 
296         if (user.getCompanyId() != fileEntry.getCompanyId()) {
297             throw new NoSuchFileEntryException();
298         }
299     }
300 
301 }