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