001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portlet.asset.service.impl;
016    
017    import com.liferay.portal.kernel.exception.PortalException;
018    import com.liferay.portal.kernel.exception.SystemException;
019    import com.liferay.portal.kernel.util.ArrayUtil;
020    import com.liferay.portal.model.User;
021    import com.liferay.portlet.asset.NoSuchLinkException;
022    import com.liferay.portlet.asset.model.AssetLink;
023    import com.liferay.portlet.asset.model.AssetLinkConstants;
024    import com.liferay.portlet.asset.service.base.AssetLinkLocalServiceBaseImpl;
025    
026    import java.util.ArrayList;
027    import java.util.Date;
028    import java.util.List;
029    
030    /**
031     * This class implements the methods needed to handle AssetLinks, the entity
032     * that relates different assets in the portal.
033     *
034     * The basic information stored for every link includes both assets entry IDs,
035     * the userId, the link type and the link's weight.
036     *
037     * @author Brian Wing Shun Chan
038     * @author Juan Fern??ndez
039     */
040    public class AssetLinkLocalServiceImpl extends AssetLinkLocalServiceBaseImpl {
041    
042            /**
043             * Adds a new asset link.
044             *
045             * @param  userId the primary key of the link's creator
046             * @param  entryId1 the primary key of the first asset entry
047             * @param  entryId2 the primary key of the second asset entry
048             * @param  type the link type. Acceptable values include {@link
049             *         com.liferay.portlet.asset.model.AssetLinkConstants#TYPE_RELATED}
050             *         which is a bidirectional relationship and {@link
051             *         com.liferay.portlet.asset.model.AssetLinkConstants#TYPE_CHILD}
052             *         which is a unidirectional relationship. For more information see
053             *         {@link com.liferay.portlet.asset.model.AssetLinkConstants}
054             * @param  weight the weight of the relationship, allowing precedence
055             *         ordering of links
056             * @return the asset link
057             * @throws PortalException if the user could not be found
058             * @throws SystemException if a system exception occurred
059             */
060            @Override
061            public AssetLink addLink(
062                            long userId, long entryId1, long entryId2, int type, int weight)
063                    throws PortalException, SystemException {
064    
065                    User user = userPersistence.findByPrimaryKey(userId);
066                    Date now = new Date();
067    
068                    long linkId = counterLocalService.increment();
069    
070                    AssetLink link = assetLinkPersistence.create(linkId);
071    
072                    link.setCompanyId(user.getCompanyId());
073                    link.setUserId(user.getUserId());
074                    link.setUserName(user.getFullName());
075                    link.setCreateDate(now);
076                    link.setEntryId1(entryId1);
077                    link.setEntryId2(entryId2);
078                    link.setType(type);
079                    link.setWeight(weight);
080    
081                    assetLinkPersistence.update(link, false);
082    
083                    if (AssetLinkConstants.isTypeBi(type)) {
084                            long linkId2 = counterLocalService.increment();
085    
086                            AssetLink link2 = assetLinkPersistence.create(linkId2);
087    
088                            link2.setCompanyId(user.getCompanyId());
089                            link2.setUserId(user.getUserId());
090                            link2.setUserName(user.getFullName());
091                            link2.setCreateDate(now);
092                            link2.setEntryId1(entryId2);
093                            link2.setEntryId2(entryId1);
094                            link2.setType(type);
095                            link2.setWeight(weight);
096    
097                            assetLinkPersistence.update(link2, false);
098                    }
099    
100                    return link;
101            }
102    
103            /**
104             * Deletes the asset link.
105             *
106             * @param  link the asset link
107             * @throws SystemException if a system exception occurred
108             */
109            @Override
110            public void deleteLink(AssetLink link) throws SystemException {
111                    if (AssetLinkConstants.isTypeBi(link.getType())) {
112                            try {
113                                    assetLinkPersistence.removeByE_E_T(
114                                            link.getEntryId2(), link.getEntryId1(), link.getType());
115                            }
116                            catch (NoSuchLinkException nsle) {
117                            }
118                    }
119    
120                    assetLinkPersistence.remove(link);
121            }
122    
123            /**
124             * Deletes the asset link.
125             *
126             * @param  linkId the primary key of the asset link
127             * @throws PortalException if the asset link could not be found
128             * @throws SystemException if a system exception occurred
129             */
130            @Override
131            public void deleteLink(long linkId)
132                    throws PortalException, SystemException {
133    
134                    AssetLink link = assetLinkPersistence.findByPrimaryKey(linkId);
135    
136                    deleteLink(link);
137            }
138    
139            /**
140             * Deletes all links associated with the asset entry.
141             *
142             * @param  entryId the primary key of the asset entry
143             * @throws SystemException if a system exception occurred
144             */
145            @Override
146            public void deleteLinks(long entryId) throws SystemException {
147                    for (AssetLink link : assetLinkPersistence.findByE1(entryId)) {
148                            deleteLink(link);
149                    }
150    
151                    for (AssetLink link : assetLinkPersistence.findByE2(entryId)) {
152                            deleteLink(link);
153                    }
154            }
155    
156            /**
157             * Delete all links that associate the two asset entries.
158             *
159             * @param  entryId1 the primary key of the first asset entry
160             * @param  entryId2 the primary key of the second asset entry
161             * @throws SystemException if a system exception occurred
162             */
163            @Override
164            public void deleteLinks(long entryId1, long entryId2)
165                    throws SystemException {
166    
167                    List<AssetLink> links = assetLinkPersistence.findByE_E(
168                            entryId1, entryId2);
169    
170                    for (AssetLink link : links) {
171                            deleteLink(link);
172                    }
173            }
174    
175            /**
176             * Returns all the asset links whose first entry ID is the given entry ID.
177             *
178             * @param  entryId the primary key of the asset entry
179             * @return the asset links whose first entry ID is the given entry ID
180             * @throws SystemException if a system exception occurred
181             */
182            @Override
183            public List<AssetLink> getDirectLinks(long entryId) throws SystemException {
184                    return assetLinkPersistence.findByE1(entryId);
185            }
186    
187            /**
188             * Returns all the asset links of the given link type whose first entry ID
189             * is the given entry ID.
190             *
191             * @param  entryId the primary key of the asset entry
192             * @param  typeId the link type. Acceptable values include {@link
193             *         com.liferay.portlet.asset.model.AssetLinkConstants#TYPE_RELATED}
194             *         which is a bidirectional relationship and {@link
195             *         com.liferay.portlet.asset.model.AssetLinkConstants#TYPE_CHILD}
196             *         which is a unidirectional relationship. For more information see
197             *         {@link com.liferay.portlet.asset.model.AssetLinkConstants}
198             * @return the asset links of the given link type whose first entry ID is
199             *         the given entry ID
200             * @throws SystemException if a system exception occurred
201             */
202            @Override
203            public List<AssetLink> getDirectLinks(long entryId, int typeId)
204                    throws SystemException {
205    
206                    return assetLinkPersistence.findByE1_T(entryId, typeId);
207            }
208    
209            /**
210             * Returns all the asset links whose first or second entry ID is the given
211             * entry ID.
212             *
213             * @param  entryId the primary key of the asset entry
214             * @return the asset links whose first or second entry ID is the given entry
215             *         ID
216             * @throws SystemException if a system exception occurred
217             */
218            @Override
219            public List<AssetLink> getLinks(long entryId) throws SystemException {
220                    List<AssetLink> e1Links = assetLinkPersistence.findByE1(entryId);
221                    List<AssetLink> e2Links = assetLinkPersistence.findByE2(entryId);
222    
223                    List<AssetLink> links = new ArrayList<AssetLink>(
224                            e1Links.size() + e2Links.size());
225    
226                    links.addAll(e1Links);
227                    links.addAll(e2Links);
228    
229                    return links;
230            }
231    
232            /**
233             * Returns all the asset links of the given link type whose first or second
234             * entry ID is the given entry ID.
235             *
236             * @param  entryId the primary key of the asset entry
237             * @param  typeId the link type. Acceptable values include {@link
238             *         com.liferay.portlet.asset.model.AssetLinkConstants#TYPE_RELATED}
239             *         which is a bidirectional relationship and {@link
240             *         com.liferay.portlet.asset.model.AssetLinkConstants#TYPE_CHILD}
241             *         which is a unidirectional relationship. For more information see
242             *         {@link com.liferay.portlet.asset.model.AssetLinkConstants}
243             * @return the asset links of the given link type whose first or second
244             *         entry ID is the given entry ID
245             * @throws SystemException if a system exception occurred
246             */
247            @Override
248            public List<AssetLink> getLinks(long entryId, int typeId)
249                    throws SystemException {
250    
251                    List<AssetLink> e1Links = assetLinkPersistence.findByE1_T(
252                            entryId, typeId);
253                    List<AssetLink> e2Links = assetLinkPersistence.findByE2_T(
254                            entryId, typeId);
255    
256                    List<AssetLink> links = new ArrayList<AssetLink>(
257                            e1Links.size() + e2Links.size());
258    
259                    links.addAll(e1Links);
260                    links.addAll(e2Links);
261    
262                    return links;
263            }
264    
265            /**
266             * Returns all the asset links of the given link type whose second entry ID
267             * is the given entry ID.
268             *
269             * @param  entryId the primary key of the asset entry
270             * @param  typeId the link type. Acceptable values include {@link
271             *         com.liferay.portlet.asset.model.AssetLinkConstants#TYPE_RELATED}
272             *         which is a bidirectional relationship and {@link
273             *         com.liferay.portlet.asset.model.AssetLinkConstants#TYPE_CHILD}
274             *         which is a unidirectional relationship. For more information see
275             *         {@link com.liferay.portlet.asset.model.AssetLinkConstants}
276             * @return the asset links of the given link type whose second entry ID is
277             *         the given entry ID
278             * @throws SystemException if a system exception occurred
279             */
280            @Override
281            public List<AssetLink> getReverseLinks(long entryId, int typeId)
282                    throws SystemException {
283    
284                    return assetLinkPersistence.findByE2_T(entryId, typeId);
285            }
286    
287            /**
288             * Updates all links of the asset entry, replacing them with links
289             * associating the asset entry with the asset entries of the given link
290             * entry IDs.
291             *
292             * <p>
293             * If no link exists with a given link entry ID, a new link is created
294             * associating the current asset entry with the asset entry of that link
295             * entry ID. An existing link is deleted if either of its entry IDs is not
296             * contained in the given link entry IDs.
297             * </p>
298             *
299             * @param  userId the primary key of the user updating the links
300             * @param  entryId the primary key of the asset entry to be managed
301             * @param  linkEntryIds the primary keys of the asset entries to be linked
302             *         with the asset entry to be managed
303             * @param  typeId the type of the asset links to be created. Acceptable
304             *         values include {@link
305             *         com.liferay.portlet.asset.model.AssetLinkConstants#TYPE_RELATED}
306             *         which is a bidirectional relationship and {@link
307             *         com.liferay.portlet.asset.model.AssetLinkConstants#TYPE_CHILD}
308             *         which is a unidirectional relationship. For more information see
309             *         {@link com.liferay.portlet.asset.model.AssetLinkConstants}
310             * @throws PortalException if the user could not be found
311             * @throws SystemException if a system exception occurred
312             */
313            @Override
314            public void updateLinks(
315                            long userId, long entryId, long[] linkEntryIds, int typeId)
316                    throws PortalException, SystemException {
317    
318                    if (linkEntryIds == null) {
319                            return;
320                    }
321    
322                    List<AssetLink> links = getLinks(entryId, typeId);
323    
324                    for (AssetLink link : links) {
325                            if (((link.getEntryId1() == entryId) &&
326                                     !ArrayUtil.contains(linkEntryIds, link.getEntryId2())) ||
327                                    ((link.getEntryId2() == entryId) &&
328                                     !ArrayUtil.contains(linkEntryIds, link.getEntryId1()))) {
329    
330                                    deleteLink(link);
331                            }
332                    }
333    
334                    for (long assetLinkEntryId : linkEntryIds) {
335                            if (assetLinkEntryId != entryId) {
336                                    AssetLink link = assetLinkPersistence.fetchByE_E_T(
337                                            entryId, assetLinkEntryId, typeId);
338    
339                                    if (link == null) {
340                                            addLink(userId, entryId, assetLinkEntryId, typeId, 0);
341                                    }
342                            }
343                    }
344            }
345    
346    }