001    /**
002     * Copyright (c) 2000-2012 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            public AssetLink addLink(
061                            long userId, long entryId1, long entryId2, int type, int weight)
062                    throws PortalException, SystemException {
063    
064                    User user = userPersistence.findByPrimaryKey(userId);
065                    Date now = new Date();
066    
067                    long linkId = counterLocalService.increment();
068    
069                    AssetLink link = assetLinkPersistence.create(linkId);
070    
071                    link.setCompanyId(user.getCompanyId());
072                    link.setUserId(user.getUserId());
073                    link.setUserName(user.getFullName());
074                    link.setCreateDate(now);
075                    link.setEntryId1(entryId1);
076                    link.setEntryId2(entryId2);
077                    link.setType(type);
078                    link.setWeight(weight);
079    
080                    assetLinkPersistence.update(link, false);
081    
082                    if (AssetLinkConstants.isTypeBi(type)) {
083                            long linkId2 = counterLocalService.increment();
084    
085                            AssetLink link2 = assetLinkPersistence.create(linkId2);
086    
087                            link2.setCompanyId(user.getCompanyId());
088                            link2.setUserId(user.getUserId());
089                            link2.setUserName(user.getFullName());
090                            link2.setCreateDate(now);
091                            link2.setEntryId1(entryId2);
092                            link2.setEntryId2(entryId1);
093                            link2.setType(type);
094                            link2.setWeight(weight);
095    
096                            assetLinkPersistence.update(link2, false);
097                    }
098    
099                    return link;
100            }
101    
102            /**
103             * Deletes the asset link.
104             *
105             * @param  link the asset link
106             * @throws SystemException if a system exception occurred
107             */
108            public void deleteLink(AssetLink link) throws SystemException {
109                    if (AssetLinkConstants.isTypeBi(link.getType())) {
110                            try {
111                                    assetLinkPersistence.removeByE_E_T(
112                                            link.getEntryId2(), link.getEntryId1(), link.getType());
113                            }
114                            catch (NoSuchLinkException nsle) {
115                            }
116                    }
117    
118                    assetLinkPersistence.remove(link);
119            }
120    
121            /**
122             * Deletes the asset link.
123             *
124             * @param  linkId the primary key of the asset link
125             * @throws PortalException if the asset link could not be found
126             * @throws SystemException if a system exception occurred
127             */
128            public void deleteLink(long linkId)
129                    throws PortalException, SystemException {
130    
131                    AssetLink link = assetLinkPersistence.findByPrimaryKey(linkId);
132    
133                    deleteLink(link);
134            }
135    
136            /**
137             * Deletes all links associated with the asset entry.
138             *
139             * @param  entryId the primary key of the asset entry
140             * @throws SystemException if a system exception occurred
141             */
142            public void deleteLinks(long entryId) throws SystemException {
143                    for (AssetLink link : assetLinkPersistence.findByE1(entryId)) {
144                            deleteLink(link);
145                    }
146    
147                    for (AssetLink link : assetLinkPersistence.findByE2(entryId)) {
148                            deleteLink(link);
149                    }
150            }
151    
152            /**
153             * Delete all links that associate the two asset entries.
154             *
155             * @param  entryId1 the primary key of the first asset entry
156             * @param  entryId2 the primary key of the second asset entry
157             * @throws SystemException if a system exception occurred
158             */
159            public void deleteLinks(long entryId1, long entryId2)
160                    throws SystemException {
161    
162                    List<AssetLink> links = assetLinkPersistence.findByE_E(
163                            entryId1, entryId2);
164    
165                    for (AssetLink link : links) {
166                            deleteLink(link);
167                    }
168            }
169    
170            /**
171             * Returns all the asset links whose first entry ID is the given entry ID.
172             *
173             * @param  entryId the primary key of the asset entry
174             * @return the asset links whose first entry ID is the given entry ID
175             * @throws SystemException if a system exception occurred
176             */
177            public List<AssetLink> getDirectLinks(long entryId) throws SystemException {
178                    return assetLinkPersistence.findByE1(entryId);
179            }
180    
181            /**
182             * Returns all the asset links of the given link type whose first entry ID
183             * is the given entry ID.
184             *
185             * @param  entryId the primary key of the asset entry
186             * @param  typeId the link type. Acceptable values include {@link
187             *         com.liferay.portlet.asset.model.AssetLinkConstants#TYPE_RELATED}
188             *         which is a bidirectional relationship and {@link
189             *         com.liferay.portlet.asset.model.AssetLinkConstants#TYPE_CHILD}
190             *         which is a unidirectional relationship. For more information see
191             *         {@link com.liferay.portlet.asset.model.AssetLinkConstants}
192             * @return the asset links of the given link type whose first entry ID is
193             *         the given entry ID
194             * @throws SystemException if a system exception occurred
195             */
196            public List<AssetLink> getDirectLinks(long entryId, int typeId)
197                    throws SystemException {
198    
199                    return assetLinkPersistence.findByE1_T(entryId, typeId);
200            }
201    
202            /**
203             * Returns all the asset links whose first or second entry ID is the given
204             * entry ID.
205             *
206             * @param  entryId the primary key of the asset entry
207             * @return the asset links whose first or second entry ID is the given entry
208             *         ID
209             * @throws SystemException if a system exception occurred
210             */
211            public List<AssetLink> getLinks(long entryId) throws SystemException {
212                    List<AssetLink> e1Links = assetLinkPersistence.findByE1(entryId);
213                    List<AssetLink> e2Links = assetLinkPersistence.findByE2(entryId);
214    
215                    List<AssetLink> links = new ArrayList<AssetLink>(
216                            e1Links.size() + e2Links.size());
217    
218                    links.addAll(e1Links);
219                    links.addAll(e2Links);
220    
221                    return links;
222            }
223    
224            /**
225             * Returns all the asset links of the given link type whose first or second
226             * entry ID is the given entry ID.
227             *
228             * @param  entryId the primary key of the asset entry
229             * @param  typeId the link type. Acceptable values include {@link
230             *         com.liferay.portlet.asset.model.AssetLinkConstants#TYPE_RELATED}
231             *         which is a bidirectional relationship and {@link
232             *         com.liferay.portlet.asset.model.AssetLinkConstants#TYPE_CHILD}
233             *         which is a unidirectional relationship. For more information see
234             *         {@link com.liferay.portlet.asset.model.AssetLinkConstants}
235             * @return the asset links of the given link type whose first or second
236             *         entry ID is the given entry ID
237             * @throws SystemException if a system exception occurred
238             */
239            public List<AssetLink> getLinks(long entryId, int typeId)
240                    throws SystemException {
241    
242                    List<AssetLink> e1Links = assetLinkPersistence.findByE1_T(
243                            entryId, typeId);
244                    List<AssetLink> e2Links = assetLinkPersistence.findByE2_T(
245                            entryId, typeId);
246    
247                    List<AssetLink> links = new ArrayList<AssetLink>(
248                            e1Links.size() + e2Links.size());
249    
250                    links.addAll(e1Links);
251                    links.addAll(e2Links);
252    
253                    return links;
254            }
255    
256            /**
257             * Returns all the asset links of the given link type whose second entry ID
258             * is the given entry ID.
259             *
260             * @param  entryId the primary key of the asset entry
261             * @param  typeId the link type. Acceptable values include {@link
262             *         com.liferay.portlet.asset.model.AssetLinkConstants#TYPE_RELATED}
263             *         which is a bidirectional relationship and {@link
264             *         com.liferay.portlet.asset.model.AssetLinkConstants#TYPE_CHILD}
265             *         which is a unidirectional relationship. For more information see
266             *         {@link com.liferay.portlet.asset.model.AssetLinkConstants}
267             * @return the asset links of the given link type whose second entry ID is
268             *         the given entry ID
269             * @throws SystemException if a system exception occurred
270             */
271            public List<AssetLink> getReverseLinks(long entryId, int typeId)
272                    throws SystemException {
273    
274                    return assetLinkPersistence.findByE2_T(entryId, typeId);
275            }
276    
277            /**
278             * Updates all links of the asset entry, replacing them with links
279             * associating the asset entry with the asset entries of the given link
280             * entry IDs.
281             *
282             * <p>
283             * If no link exists with a given link entry ID, a new link is created
284             * associating the current asset entry with the asset entry of that link
285             * entry ID. An existing link is deleted if either of its entry IDs is not
286             * contained in the given link entry IDs.
287             * </p>
288             *
289             * @param  userId the primary key of the user updating the links
290             * @param  entryId the primary key of the asset entry to be managed
291             * @param  linkEntryIds the primary keys of the asset entries to be linked
292             *         with the asset entry to be managed
293             * @param  typeId the type of the asset links to be created. Acceptable
294             *         values include {@link
295             *         com.liferay.portlet.asset.model.AssetLinkConstants#TYPE_RELATED}
296             *         which is a bidirectional relationship and {@link
297             *         com.liferay.portlet.asset.model.AssetLinkConstants#TYPE_CHILD}
298             *         which is a unidirectional relationship. For more information see
299             *         {@link com.liferay.portlet.asset.model.AssetLinkConstants}
300             * @throws PortalException if the user could not be found
301             * @throws SystemException if a system exception occurred
302             */
303            public void updateLinks(
304                            long userId, long entryId, long[] linkEntryIds, int typeId)
305                    throws PortalException, SystemException {
306    
307                    if (linkEntryIds == null) {
308                            return;
309                    }
310    
311                    List<AssetLink> links = getLinks(entryId, typeId);
312    
313                    for (AssetLink link : links) {
314                            if (((link.getEntryId1() == entryId) &&
315                                     !ArrayUtil.contains(linkEntryIds, link.getEntryId2())) ||
316                                    ((link.getEntryId2() == entryId) &&
317                                     !ArrayUtil.contains(linkEntryIds, link.getEntryId1()))) {
318    
319                                    deleteLink(link);
320                            }
321                    }
322    
323                    for (long assetLinkEntryId : linkEntryIds) {
324                            if (assetLinkEntryId != entryId) {
325                                    AssetLink link = assetLinkPersistence.fetchByE_E_T(
326                                            entryId, assetLinkEntryId, typeId);
327    
328                                    if (link == null) {
329                                            addLink(userId, entryId, assetLinkEntryId, typeId, 0);
330                                    }
331                            }
332                    }
333            }
334    
335    }