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.AssetEntry;
023    import com.liferay.portlet.asset.model.AssetLink;
024    import com.liferay.portlet.asset.model.AssetLinkConstants;
025    import com.liferay.portlet.asset.service.base.AssetLinkLocalServiceBaseImpl;
026    
027    import java.util.ArrayList;
028    import java.util.Collections;
029    import java.util.Date;
030    import java.util.List;
031    
032    /**
033     * This class implements the methods needed to handle AssetLinks, the entity
034     * that relates different assets in the portal.
035     *
036     * The basic information stored for every link includes both assets entry IDs,
037     * the userId, the link type and the link's weight.
038     *
039     * @author Brian Wing Shun Chan
040     * @author Juan Fern??ndez
041     */
042    public class AssetLinkLocalServiceImpl extends AssetLinkLocalServiceBaseImpl {
043    
044            /**
045             * Adds a new asset link.
046             *
047             * @param  userId the primary key of the link's creator
048             * @param  entryId1 the primary key of the first asset entry
049             * @param  entryId2 the primary key of the second asset entry
050             * @param  type the link type. Acceptable values include {@link
051             *         com.liferay.portlet.asset.model.AssetLinkConstants#TYPE_RELATED}
052             *         which is a bidirectional relationship and {@link
053             *         com.liferay.portlet.asset.model.AssetLinkConstants#TYPE_CHILD}
054             *         which is a unidirectional relationship. For more information see
055             *         {@link com.liferay.portlet.asset.model.AssetLinkConstants}
056             * @param  weight the weight of the relationship, allowing precedence
057             *         ordering of links
058             * @return the asset link
059             * @throws PortalException if the user could not be found
060             * @throws SystemException if a system exception occurred
061             */
062            @Override
063            public AssetLink addLink(
064                            long userId, long entryId1, long entryId2, int type, int weight)
065                    throws PortalException, SystemException {
066    
067                    User user = userPersistence.findByPrimaryKey(userId);
068                    Date now = new Date();
069    
070                    long linkId = counterLocalService.increment();
071    
072                    AssetLink link = assetLinkPersistence.create(linkId);
073    
074                    link.setCompanyId(user.getCompanyId());
075                    link.setUserId(user.getUserId());
076                    link.setUserName(user.getFullName());
077                    link.setCreateDate(now);
078                    link.setEntryId1(entryId1);
079                    link.setEntryId2(entryId2);
080                    link.setType(type);
081                    link.setWeight(weight);
082    
083                    assetLinkPersistence.update(link);
084    
085                    if (AssetLinkConstants.isTypeBi(type)) {
086                            long linkId2 = counterLocalService.increment();
087    
088                            AssetLink link2 = assetLinkPersistence.create(linkId2);
089    
090                            link2.setCompanyId(user.getCompanyId());
091                            link2.setUserId(user.getUserId());
092                            link2.setUserName(user.getFullName());
093                            link2.setCreateDate(now);
094                            link2.setEntryId1(entryId2);
095                            link2.setEntryId2(entryId1);
096                            link2.setType(type);
097                            link2.setWeight(weight);
098    
099                            assetLinkPersistence.update(link2);
100                    }
101    
102                    return link;
103            }
104    
105            /**
106             * Deletes the asset link.
107             *
108             * @param  link the asset link
109             * @throws SystemException if a system exception occurred
110             */
111            @Override
112            public void deleteLink(AssetLink link) throws SystemException {
113                    if (AssetLinkConstants.isTypeBi(link.getType())) {
114                            try {
115                                    assetLinkPersistence.removeByE_E_T(
116                                            link.getEntryId2(), link.getEntryId1(), link.getType());
117                            }
118                            catch (NoSuchLinkException nsle) {
119                            }
120                    }
121    
122                    assetLinkPersistence.remove(link);
123            }
124    
125            /**
126             * Deletes the asset link.
127             *
128             * @param  linkId the primary key of the asset link
129             * @throws PortalException if the asset link could not be found
130             * @throws SystemException if a system exception occurred
131             */
132            @Override
133            public void deleteLink(long linkId)
134                    throws PortalException, SystemException {
135    
136                    AssetLink link = assetLinkPersistence.findByPrimaryKey(linkId);
137    
138                    deleteLink(link);
139            }
140    
141            /**
142             * Deletes all links associated with the asset entry.
143             *
144             * @param  entryId the primary key of the asset entry
145             * @throws SystemException if a system exception occurred
146             */
147            @Override
148            public void deleteLinks(long entryId) throws SystemException {
149                    for (AssetLink link : assetLinkPersistence.findByE1(entryId)) {
150                            deleteLink(link);
151                    }
152    
153                    for (AssetLink link : assetLinkPersistence.findByE2(entryId)) {
154                            deleteLink(link);
155                    }
156            }
157    
158            /**
159             * Delete all links that associate the two asset entries.
160             *
161             * @param  entryId1 the primary key of the first asset entry
162             * @param  entryId2 the primary key of the second asset entry
163             * @throws SystemException if a system exception occurred
164             */
165            @Override
166            public void deleteLinks(long entryId1, long entryId2)
167                    throws SystemException {
168    
169                    List<AssetLink> links = assetLinkPersistence.findByE_E(
170                            entryId1, entryId2);
171    
172                    for (AssetLink link : links) {
173                            deleteLink(link);
174                    }
175            }
176    
177            /**
178             * Returns all the asset links whose first entry ID is the given entry ID.
179             *
180             * @param  entryId the primary key of the asset entry
181             * @return the asset links whose first entry ID is the given entry ID
182             * @throws SystemException if a system exception occurred
183             */
184            @Override
185            public List<AssetLink> getDirectLinks(long entryId) throws SystemException {
186                    List<AssetLink> assetLinks = assetLinkPersistence.findByE1(entryId);
187    
188                    if (!assetLinks.isEmpty()) {
189                            List<AssetLink> filteredAssetLinks = new ArrayList<AssetLink>(
190                                    assetLinks.size());
191    
192                            for (AssetLink assetLink : assetLinks) {
193                                    AssetEntry assetEntry = assetEntryPersistence.fetchByPrimaryKey(
194                                            assetLink.getEntryId2());
195    
196                                    if ((assetEntry != null) && assetEntry.isVisible()) {
197                                            filteredAssetLinks.add(assetLink);
198                                    }
199                            }
200    
201                            assetLinks = Collections.unmodifiableList(filteredAssetLinks);
202                    }
203    
204                    return assetLinks;
205            }
206    
207            /**
208             * Returns all the asset links of the given link type whose first entry ID
209             * is the given entry ID.
210             *
211             * @param  entryId the primary key of the asset entry
212             * @param  typeId the link type. Acceptable values include {@link
213             *         com.liferay.portlet.asset.model.AssetLinkConstants#TYPE_RELATED}
214             *         which is a bidirectional relationship and {@link
215             *         com.liferay.portlet.asset.model.AssetLinkConstants#TYPE_CHILD}
216             *         which is a unidirectional relationship. For more information see
217             *         {@link com.liferay.portlet.asset.model.AssetLinkConstants}
218             * @return the asset links of the given link type whose first entry ID is
219             *         the given entry ID
220             * @throws SystemException if a system exception occurred
221             */
222            @Override
223            public List<AssetLink> getDirectLinks(long entryId, int typeId)
224                    throws SystemException {
225    
226                    List<AssetLink> assetLinks = assetLinkPersistence.findByE1_T(
227                            entryId, typeId);
228    
229                    if (!assetLinks.isEmpty()) {
230                            List<AssetLink> filteredAssetLinks = new ArrayList<AssetLink>(
231                                    assetLinks.size());
232    
233                            for (AssetLink assetLink : assetLinks) {
234                                    AssetEntry assetEntry = assetEntryPersistence.fetchByPrimaryKey(
235                                            assetLink.getEntryId2());
236    
237                                    if ((assetEntry != null) && assetEntry.isVisible()) {
238                                            filteredAssetLinks.add(assetLink);
239                                    }
240                            }
241    
242                            assetLinks = Collections.unmodifiableList(filteredAssetLinks);
243                    }
244    
245                    return assetLinks;
246            }
247    
248            /**
249             * Returns all the asset links whose first or second entry ID is the given
250             * entry ID.
251             *
252             * @param  entryId the primary key of the asset entry
253             * @return the asset links whose first or second entry ID is the given entry
254             *         ID
255             * @throws SystemException if a system exception occurred
256             */
257            @Override
258            public List<AssetLink> getLinks(long entryId) throws SystemException {
259                    List<AssetLink> e1Links = assetLinkPersistence.findByE1(entryId);
260                    List<AssetLink> e2Links = assetLinkPersistence.findByE2(entryId);
261    
262                    List<AssetLink> links = new ArrayList<AssetLink>(
263                            e1Links.size() + e2Links.size());
264    
265                    links.addAll(e1Links);
266                    links.addAll(e2Links);
267    
268                    return links;
269            }
270    
271            /**
272             * Returns all the asset links of the given link type whose first or second
273             * entry ID is the given entry ID.
274             *
275             * @param  entryId the primary key of the asset entry
276             * @param  typeId the link type. Acceptable values include {@link
277             *         com.liferay.portlet.asset.model.AssetLinkConstants#TYPE_RELATED}
278             *         which is a bidirectional relationship and {@link
279             *         com.liferay.portlet.asset.model.AssetLinkConstants#TYPE_CHILD}
280             *         which is a unidirectional relationship. For more information see
281             *         {@link com.liferay.portlet.asset.model.AssetLinkConstants}
282             * @return the asset links of the given link type whose first or second
283             *         entry ID is the given entry ID
284             * @throws SystemException if a system exception occurred
285             */
286            @Override
287            public List<AssetLink> getLinks(long entryId, int typeId)
288                    throws SystemException {
289    
290                    List<AssetLink> e1Links = assetLinkPersistence.findByE1_T(
291                            entryId, typeId);
292                    List<AssetLink> e2Links = assetLinkPersistence.findByE2_T(
293                            entryId, typeId);
294    
295                    List<AssetLink> links = new ArrayList<AssetLink>(
296                            e1Links.size() + e2Links.size());
297    
298                    links.addAll(e1Links);
299                    links.addAll(e2Links);
300    
301                    return links;
302            }
303    
304            /**
305             * Returns all the asset links of the given link type whose second entry ID
306             * is the given entry ID.
307             *
308             * @param  entryId the primary key of the asset entry
309             * @param  typeId the link type. Acceptable values include {@link
310             *         com.liferay.portlet.asset.model.AssetLinkConstants#TYPE_RELATED}
311             *         which is a bidirectional relationship and {@link
312             *         com.liferay.portlet.asset.model.AssetLinkConstants#TYPE_CHILD}
313             *         which is a unidirectional relationship. For more information see
314             *         {@link com.liferay.portlet.asset.model.AssetLinkConstants}
315             * @return the asset links of the given link type whose second entry ID is
316             *         the given entry ID
317             * @throws SystemException if a system exception occurred
318             */
319            @Override
320            public List<AssetLink> getReverseLinks(long entryId, int typeId)
321                    throws SystemException {
322    
323                    return assetLinkPersistence.findByE2_T(entryId, typeId);
324            }
325    
326            @Override
327            public AssetLink updateLink(
328                            long userId, long entryId1, long entryId2, int typeId, int weight)
329                    throws PortalException, SystemException {
330    
331                    AssetLink assetLink = assetLinkPersistence.fetchByE_E_T(
332                            entryId1, entryId2, typeId);
333    
334                    if (assetLink == null) {
335                            return addLink(userId, entryId1, entryId2, typeId, weight);
336                    }
337    
338                    assetLink.setWeight(weight);
339    
340                    assetLinkPersistence.update(assetLink);
341    
342                    return assetLink;
343            }
344    
345            /**
346             * Updates all links of the asset entry, replacing them with links
347             * associating the asset entry with the asset entries of the given link
348             * entry IDs.
349             *
350             * <p>
351             * If no link exists with a given link entry ID, a new link is created
352             * associating the current asset entry with the asset entry of that link
353             * entry ID. An existing link is deleted if either of its entry IDs is not
354             * contained in the given link entry IDs.
355             * </p>
356             *
357             * @param  userId the primary key of the user updating the links
358             * @param  entryId the primary key of the asset entry to be managed
359             * @param  linkEntryIds the primary keys of the asset entries to be linked
360             *         with the asset entry to be managed
361             * @param  typeId the type of the asset links to be created. Acceptable
362             *         values include {@link
363             *         com.liferay.portlet.asset.model.AssetLinkConstants#TYPE_RELATED}
364             *         which is a bidirectional relationship and {@link
365             *         com.liferay.portlet.asset.model.AssetLinkConstants#TYPE_CHILD}
366             *         which is a unidirectional relationship. For more information see
367             *         {@link com.liferay.portlet.asset.model.AssetLinkConstants}
368             * @throws PortalException if the user could not be found
369             * @throws SystemException if a system exception occurred
370             */
371            @Override
372            public void updateLinks(
373                            long userId, long entryId, long[] linkEntryIds, int typeId)
374                    throws PortalException, SystemException {
375    
376                    if (linkEntryIds == null) {
377                            return;
378                    }
379    
380                    List<AssetLink> links = getLinks(entryId, typeId);
381    
382                    for (AssetLink link : links) {
383                            if (((link.getEntryId1() == entryId) &&
384                                     !ArrayUtil.contains(linkEntryIds, link.getEntryId2())) ||
385                                    ((link.getEntryId2() == entryId) &&
386                                     !ArrayUtil.contains(linkEntryIds, link.getEntryId1()))) {
387    
388                                    deleteLink(link);
389                            }
390                    }
391    
392                    for (long assetLinkEntryId : linkEntryIds) {
393                            if (assetLinkEntryId != entryId) {
394                                    AssetLink link = assetLinkPersistence.fetchByE_E_T(
395                                            entryId, assetLinkEntryId, typeId);
396    
397                                    if (link == null) {
398                                            addLink(userId, entryId, assetLinkEntryId, typeId, 0);
399                                    }
400                            }
401                    }
402            }
403    
404    }