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