1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights 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.portal.PortalException;
26  import com.liferay.portal.SystemException;
27  import com.liferay.portal.kernel.util.MimeTypesUtil;
28  import com.liferay.portal.model.ResourceConstants;
29  import com.liferay.portal.model.User;
30  import com.liferay.portal.service.ServiceContext;
31  import com.liferay.portlet.documentlibrary.NoSuchFileEntryException;
32  import com.liferay.portlet.documentlibrary.model.DLFileEntry;
33  import com.liferay.portlet.documentlibrary.model.DLFileShortcut;
34  import com.liferay.portlet.documentlibrary.model.DLFolder;
35  import com.liferay.portlet.documentlibrary.model.DLFolderConstants;
36  import com.liferay.portlet.documentlibrary.service.base.DLFileShortcutLocalServiceBaseImpl;
37  
38  import java.util.Date;
39  import java.util.List;
40  
41  /**
42   * <a href="DLFileShortcutLocalServiceImpl.java.html"><b><i>View Source</i></b>
43   * </a>
44   *
45   * @author Brian Wing Shun Chan
46   */
47  public class DLFileShortcutLocalServiceImpl
48      extends DLFileShortcutLocalServiceBaseImpl {
49  
50      public DLFileShortcut addFileShortcut(
51              long userId, long folderId, long toFolderId, String toName,
52              boolean addCommunityPermissions, boolean addGuestPermissions)
53          throws PortalException, SystemException {
54  
55          return addFileShortcut(
56              null, userId, folderId, toFolderId, toName,
57              Boolean.valueOf(addCommunityPermissions),
58              Boolean.valueOf(addGuestPermissions), null, null);
59      }
60  
61      public DLFileShortcut addFileShortcut(
62              String uuid, long userId, long folderId, long toFolderId,
63              String toName, boolean addCommunityPermissions,
64              boolean addGuestPermissions)
65          throws PortalException, SystemException {
66  
67          return addFileShortcut(
68                  uuid, userId, folderId, toFolderId, toName,
69                  Boolean.valueOf(addCommunityPermissions),
70                  Boolean.valueOf(addGuestPermissions), null, null);
71      }
72  
73      public DLFileShortcut addFileShortcut(
74              long userId, long folderId, long toFolderId, String toName,
75              String[] communityPermissions, String[] guestPermissions)
76          throws PortalException, SystemException {
77  
78          return addFileShortcut(
79              null, userId, folderId, toFolderId, toName, null, null,
80              communityPermissions, guestPermissions);
81      }
82  
83      public DLFileShortcut addFileShortcut(
84              String uuid, long userId, long folderId, long toFolderId,
85              String toName, Boolean addCommunityPermissions,
86              Boolean addGuestPermissions, String[] communityPermissions,
87              String[] guestPermissions)
88          throws PortalException, SystemException {
89  
90          ServiceContext serviceContext = new ServiceContext();
91  
92          serviceContext.setAddCommunityPermissions(addCommunityPermissions);
93          serviceContext.setAddGuestPermissions(addGuestPermissions);
94          serviceContext.setCommunityPermissions(communityPermissions);
95          serviceContext.setGuestPermissions(guestPermissions);
96  
97          return addFileShortcut(
98              uuid, userId, folderId, toFolderId, toName, serviceContext);
99      }
100 
101     public DLFileShortcut addFileShortcut(
102             long userId, long folderId, long toFolderId, String toName,
103             ServiceContext serviceContext)
104         throws PortalException, SystemException {
105 
106         return addFileShortcut(
107             null, userId, folderId, toFolderId, toName, serviceContext);
108     }
109 
110     public DLFileShortcut addFileShortcut(
111             String uuid, long userId, long folderId, long toFolderId,
112             String toName, ServiceContext serviceContext)
113         throws PortalException, SystemException {
114 
115         // File shortcut
116 
117         User user = userPersistence.findByPrimaryKey(userId);
118         folderId = getFolderId(user.getCompanyId(), folderId);
119         DLFolder folder = dlFolderPersistence.findByPrimaryKey(folderId);
120         Date now = new Date();
121 
122         validate(user, toFolderId, toName);
123 
124         long fileShortcutId = counterLocalService.increment();
125 
126         DLFileShortcut fileShortcut = dlFileShortcutPersistence.create(
127             fileShortcutId);
128 
129         fileShortcut.setUuid(uuid);
130         fileShortcut.setGroupId(folder.getGroupId());
131         fileShortcut.setCompanyId(user.getCompanyId());
132         fileShortcut.setUserId(user.getUserId());
133         fileShortcut.setUserName(user.getFullName());
134         fileShortcut.setCreateDate(now);
135         fileShortcut.setModifiedDate(now);
136         fileShortcut.setFolderId(folderId);
137         fileShortcut.setToFolderId(toFolderId);
138         fileShortcut.setToName(toName);
139 
140         dlFileShortcutPersistence.update(fileShortcut, false);
141 
142         // Resources
143 
144         if (serviceContext.getAddCommunityPermissions() ||
145             serviceContext.getAddGuestPermissions()) {
146 
147             addFileShortcutResources(
148                 fileShortcut, serviceContext.getAddCommunityPermissions(),
149                 serviceContext.getAddGuestPermissions());
150         }
151         else {
152             addFileShortcutResources(
153                 fileShortcut, serviceContext.getCommunityPermissions(),
154                 serviceContext.getGuestPermissions());
155         }
156 
157         // Tags
158 
159         DLFileEntry fileEntry = dlFileEntryLocalService.getFileEntry(
160             toFolderId, toName);
161 
162         copyTagEntries(fileEntry, serviceContext);
163 
164         updateTagsAsset(
165             userId, fileShortcut, serviceContext.getTagsCategories(),
166             serviceContext.getTagsEntries());
167 
168         // Folder
169 
170         folder.setLastPostDate(fileShortcut.getModifiedDate());
171 
172         dlFolderPersistence.update(folder, false);
173 
174         return fileShortcut;
175     }
176 
177     public void addFileShortcutResources(
178             long fileShortcutId, boolean addCommunityPermissions,
179             boolean addGuestPermissions)
180         throws PortalException, SystemException {
181 
182         DLFileShortcut fileShortcut =
183             dlFileShortcutPersistence.findByPrimaryKey(fileShortcutId);
184 
185         addFileShortcutResources(
186             fileShortcut, addCommunityPermissions, addGuestPermissions);
187     }
188 
189     public void addFileShortcutResources(
190             DLFileShortcut fileShortcut, boolean addCommunityPermissions,
191             boolean addGuestPermissions)
192         throws PortalException, SystemException {
193 
194         resourceLocalService.addResources(
195             fileShortcut.getCompanyId(), fileShortcut.getGroupId(),
196             fileShortcut.getUserId(), DLFileShortcut.class.getName(),
197             fileShortcut.getFileShortcutId(), false, addCommunityPermissions,
198             addGuestPermissions);
199     }
200 
201     public void addFileShortcutResources(
202             long fileShortcutId, String[] communityPermissions,
203             String[] guestPermissions)
204         throws PortalException, SystemException {
205 
206         DLFileShortcut fileShortcut =
207             dlFileShortcutPersistence.findByPrimaryKey(fileShortcutId);
208 
209         addFileShortcutResources(
210             fileShortcut, communityPermissions, guestPermissions);
211     }
212 
213     public void addFileShortcutResources(
214             DLFileShortcut fileShortcut, String[] communityPermissions,
215             String[] guestPermissions)
216         throws PortalException, SystemException {
217 
218         resourceLocalService.addModelResources(
219             fileShortcut.getCompanyId(), fileShortcut.getGroupId(),
220             fileShortcut.getUserId(), DLFileShortcut.class.getName(),
221             fileShortcut.getFileShortcutId(), communityPermissions,
222             guestPermissions);
223     }
224 
225     public void deleteFileShortcut(long fileShortcutId)
226         throws PortalException, SystemException {
227 
228         DLFileShortcut fileShortcut =
229             dlFileShortcutLocalService.getDLFileShortcut(fileShortcutId);
230 
231         deleteFileShortcut(fileShortcut);
232     }
233 
234     public void deleteFileShortcut(DLFileShortcut fileShortcut)
235         throws PortalException, SystemException {
236 
237         // Tags
238 
239         tagsAssetLocalService.deleteAsset(
240             DLFileShortcut.class.getName(), fileShortcut.getFileShortcutId());
241 
242         // Resources
243 
244         resourceLocalService.deleteResource(
245             fileShortcut.getCompanyId(), DLFileShortcut.class.getName(),
246             ResourceConstants.SCOPE_INDIVIDUAL,
247             fileShortcut.getFileShortcutId());
248 
249         // File shortcut
250 
251         dlFileShortcutPersistence.remove(fileShortcut);
252     }
253 
254     public void deleteFileShortcuts(long toFolderId, String toName)
255         throws PortalException, SystemException {
256 
257         List<DLFileShortcut> fileShortcuts =
258             dlFileShortcutPersistence.findByTF_TN(toFolderId, toName);
259 
260         for (DLFileShortcut fileShortcut : fileShortcuts) {
261             deleteFileShortcut(fileShortcut);
262         }
263     }
264 
265     public DLFileShortcut getFileShortcut(long fileShortcutId)
266         throws PortalException, SystemException {
267 
268         return dlFileShortcutPersistence.findByPrimaryKey(fileShortcutId);
269     }
270 
271     public void updateTagsAsset(
272             long userId, DLFileShortcut fileShortcut, String[] tagsCategories,
273             String[] tagsEntries)
274         throws PortalException, SystemException {
275 
276         DLFileEntry fileEntry = dlFileEntryLocalService.getFileEntry(
277             fileShortcut.getToFolderId(), fileShortcut.getToName());
278 
279         String mimeType = MimeTypesUtil.getContentType(fileEntry.getName());
280 
281         tagsAssetLocalService.updateAsset(
282             userId, fileShortcut.getGroupId(), DLFileShortcut.class.getName(),
283             fileShortcut.getFileShortcutId(), tagsCategories, tagsEntries,
284             false, null, null, null, null, mimeType, fileEntry.getTitle(),
285             fileEntry.getDescription(), null, null, 0, 0, null, false);
286     }
287 
288     public DLFileShortcut updateFileShortcut(
289             long userId, long fileShortcutId, long folderId,
290             long toFolderId, String toName)
291         throws PortalException, SystemException {
292 
293         ServiceContext serviceContext = new ServiceContext();
294 
295         return updateFileShortcut(
296             userId, fileShortcutId, folderId, toFolderId, toName,
297             serviceContext);
298     }
299 
300     public DLFileShortcut updateFileShortcut(
301             long userId, long fileShortcutId, long folderId,
302             long toFolderId, String toName, ServiceContext serviceContext)
303         throws PortalException, SystemException {
304 
305         // File shortcut
306 
307         User user = userPersistence.findByPrimaryKey(userId);
308         DLFolder folder = dlFolderPersistence.findByPrimaryKey(folderId);
309 
310         validate(user, toFolderId, toName);
311 
312         DLFileShortcut fileShortcut =
313             dlFileShortcutPersistence.findByPrimaryKey(fileShortcutId);
314 
315         fileShortcut.setModifiedDate(new Date());
316         fileShortcut.setFolderId(folderId);
317         fileShortcut.setToFolderId(toFolderId);
318         fileShortcut.setToName(toName);
319 
320         dlFileShortcutPersistence.update(fileShortcut, false);
321 
322         // Tags
323 
324         DLFileEntry fileEntry = dlFileEntryLocalService.getFileEntry(
325             toFolderId, toName);
326 
327         copyTagEntries(fileEntry, serviceContext);
328 
329         updateTagsAsset(
330             userId, fileShortcut, serviceContext.getTagsCategories(),
331             serviceContext.getTagsEntries());
332 
333         // Folder
334 
335         folder.setLastPostDate(fileShortcut.getModifiedDate());
336 
337         dlFolderPersistence.update(folder, false);
338 
339         return fileShortcut;
340     }
341 
342     public void updateFileShortcuts(
343             long oldToFolderId, String oldToName, long newToFolderId,
344             String newToName)
345         throws SystemException {
346 
347         List<DLFileShortcut> fileShortcuts =
348             dlFileShortcutPersistence.findByTF_TN(oldToFolderId, oldToName);
349 
350         for (DLFileShortcut fileShortcut : fileShortcuts) {
351             fileShortcut.setToFolderId(newToFolderId);
352             fileShortcut.setToName(newToName);
353 
354             dlFileShortcutPersistence.update(fileShortcut, false);
355         }
356     }
357 
358     protected void copyTagEntries(
359             DLFileEntry fileEntry, ServiceContext serviceContext)
360         throws PortalException, SystemException {
361 
362         String[] tagsEntries = tagsEntryLocalService.getEntryNames(
363             DLFileEntry.class.getName(), fileEntry.getFileEntryId());
364 
365         tagsEntryLocalService.checkEntries(
366             serviceContext.getUserId(), serviceContext.getScopeGroupId(),
367             tagsEntries);
368 
369         serviceContext.setTagsEntries(tagsEntries);
370     }
371 
372     protected long getFolderId(long companyId, long folderId)
373         throws SystemException {
374 
375         if (folderId != DLFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
376 
377             // Ensure folder exists and belongs to the proper company
378 
379             DLFolder folder = dlFolderPersistence.fetchByPrimaryKey(folderId);
380 
381             if ((folder == null) || (companyId != folder.getCompanyId())) {
382                 folderId = DLFolderConstants.DEFAULT_PARENT_FOLDER_ID;
383             }
384         }
385 
386         return folderId;
387     }
388 
389     protected void validate(User user, long toFolderId, String toName)
390         throws PortalException, SystemException {
391 
392         DLFileEntry fileEntry = dlFileEntryLocalService.getFileEntry(
393             toFolderId, toName);
394 
395         if (user.getCompanyId() != fileEntry.getCompanyId()) {
396             throw new NoSuchFileEntryException();
397         }
398     }
399 
400 }