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