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.model.User;
019    import com.liferay.portlet.asset.exception.CategoryPropertyKeyException;
020    import com.liferay.portlet.asset.exception.CategoryPropertyValueException;
021    import com.liferay.portlet.asset.exception.DuplicateCategoryPropertyException;
022    import com.liferay.portlet.asset.model.AssetCategoryProperty;
023    import com.liferay.portlet.asset.service.base.AssetCategoryPropertyLocalServiceBaseImpl;
024    import com.liferay.portlet.asset.util.AssetUtil;
025    
026    import java.util.List;
027    
028    /**
029     * @author Brian Wing Shun Chan
030     * @author Jorge Ferrer
031     */
032    public class AssetCategoryPropertyLocalServiceImpl
033            extends AssetCategoryPropertyLocalServiceBaseImpl {
034    
035            @Override
036            public AssetCategoryProperty addCategoryProperty(
037                            long userId, long categoryId, String key, String value)
038                    throws PortalException {
039    
040                    User user = userPersistence.findByPrimaryKey(userId);
041    
042                    validate(key, value);
043    
044                    if (hasCategoryProperty(categoryId, key)) {
045                            throw new DuplicateCategoryPropertyException(
046                                    "A category property already exists with the key " + key);
047                    }
048    
049                    long categoryPropertyId = counterLocalService.increment();
050    
051                    AssetCategoryProperty categoryProperty =
052                            assetCategoryPropertyPersistence.create(categoryPropertyId);
053    
054                    categoryProperty.setCompanyId(user.getCompanyId());
055                    categoryProperty.setUserId(user.getUserId());
056                    categoryProperty.setUserName(user.getFullName());
057                    categoryProperty.setCategoryId(categoryId);
058                    categoryProperty.setKey(key);
059                    categoryProperty.setValue(value);
060    
061                    assetCategoryPropertyPersistence.update(categoryProperty);
062    
063                    return categoryProperty;
064            }
065    
066            @Override
067            public void deleteCategoryProperties(long entryId) {
068                    List<AssetCategoryProperty> categoryProperties =
069                            assetCategoryPropertyPersistence.findByCategoryId(entryId);
070    
071                    for (AssetCategoryProperty categoryProperty : categoryProperties) {
072                            deleteCategoryProperty(categoryProperty);
073                    }
074            }
075    
076            @Override
077            public void deleteCategoryProperty(AssetCategoryProperty categoryProperty) {
078                    assetCategoryPropertyPersistence.remove(categoryProperty);
079            }
080    
081            @Override
082            public void deleteCategoryProperty(long categoryPropertyId)
083                    throws PortalException {
084    
085                    AssetCategoryProperty categoryProperty =
086                            assetCategoryPropertyPersistence.findByPrimaryKey(
087                                    categoryPropertyId);
088    
089                    deleteCategoryProperty(categoryProperty);
090            }
091    
092            @Override
093            public List<AssetCategoryProperty> getCategoryProperties() {
094                    return assetCategoryPropertyPersistence.findAll();
095            }
096    
097            @Override
098            public List<AssetCategoryProperty> getCategoryProperties(long entryId) {
099                    return assetCategoryPropertyPersistence.findByCategoryId(entryId);
100            }
101    
102            @Override
103            public AssetCategoryProperty getCategoryProperty(long categoryPropertyId)
104                    throws PortalException {
105    
106                    return assetCategoryPropertyPersistence.findByPrimaryKey(
107                            categoryPropertyId);
108            }
109    
110            @Override
111            public AssetCategoryProperty getCategoryProperty(
112                            long categoryId, String key)
113                    throws PortalException {
114    
115                    return assetCategoryPropertyPersistence.findByCA_K(categoryId, key);
116            }
117    
118            @Override
119            public List<AssetCategoryProperty> getCategoryPropertyValues(
120                    long groupId, String key) {
121    
122                    return assetCategoryPropertyFinder.findByG_K(groupId, key);
123            }
124    
125            @Override
126            public AssetCategoryProperty updateCategoryProperty(
127                            long userId, long categoryPropertyId, String key, String value)
128                    throws PortalException {
129    
130                    AssetCategoryProperty categoryProperty =
131                            assetCategoryPropertyPersistence.findByPrimaryKey(
132                                    categoryPropertyId);
133    
134                    if (!categoryProperty.getKey().equals(key) &&
135                            hasCategoryProperty(categoryProperty.getCategoryId(), key)) {
136    
137                            throw new DuplicateCategoryPropertyException(
138                                    "A category property already exists with the key " + key);
139                    }
140    
141                    validate(key, value);
142    
143                    if (userId != 0) {
144                            User user = userPersistence.findByPrimaryKey(userId);
145    
146                            categoryProperty.setUserId(userId);
147                            categoryProperty.setUserName(user.getFullName());
148                    }
149    
150                    categoryProperty.setKey(key);
151                    categoryProperty.setValue(value);
152    
153                    assetCategoryPropertyPersistence.update(categoryProperty);
154    
155                    return categoryProperty;
156            }
157    
158            @Override
159            public AssetCategoryProperty updateCategoryProperty(
160                            long categoryPropertyId, String key, String value)
161                    throws PortalException {
162    
163                    return updateCategoryProperty(0, categoryPropertyId, key, value);
164            }
165    
166            protected boolean hasCategoryProperty(long categoryId, String key) {
167                    AssetCategoryProperty categoryProperty =
168                            assetCategoryPropertyPersistence.fetchByCA_K(categoryId, key);
169    
170                    if (categoryProperty != null) {
171                            return true;
172                    }
173    
174                    return false;
175            }
176    
177            protected void validate(String key, String value) throws PortalException {
178                    if (!AssetUtil.isValidWord(key)) {
179                            throw new CategoryPropertyKeyException("Invalid key " + key);
180                    }
181    
182                    if (!AssetUtil.isValidWord(value)) {
183                            throw new CategoryPropertyValueException("Invalid value " + value);
184                    }
185            }
186    
187    }