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.Date;
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                    Date now = new Date();
042    
043                    validate(key, value);
044    
045                    long categoryPropertyId = counterLocalService.increment();
046    
047                    AssetCategoryProperty categoryProperty =
048                            assetCategoryPropertyPersistence.create(categoryPropertyId);
049    
050                    categoryProperty.setCompanyId(user.getCompanyId());
051                    categoryProperty.setUserId(user.getUserId());
052                    categoryProperty.setUserName(user.getFullName());
053                    categoryProperty.setCreateDate(now);
054                    categoryProperty.setModifiedDate(now);
055                    categoryProperty.setCategoryId(categoryId);
056                    categoryProperty.setKey(key);
057                    categoryProperty.setValue(value);
058    
059                    assetCategoryPropertyPersistence.update(categoryProperty);
060    
061                    return categoryProperty;
062            }
063    
064            @Override
065            public void deleteCategoryProperties(long entryId) {
066                    List<AssetCategoryProperty> categoryProperties =
067                            assetCategoryPropertyPersistence.findByCategoryId(entryId);
068    
069                    for (AssetCategoryProperty categoryProperty : categoryProperties) {
070                            deleteCategoryProperty(categoryProperty);
071                    }
072            }
073    
074            @Override
075            public void deleteCategoryProperty(AssetCategoryProperty categoryProperty) {
076                    assetCategoryPropertyPersistence.remove(categoryProperty);
077            }
078    
079            @Override
080            public void deleteCategoryProperty(long categoryPropertyId)
081                    throws PortalException {
082    
083                    AssetCategoryProperty categoryProperty =
084                            assetCategoryPropertyPersistence.findByPrimaryKey(
085                                    categoryPropertyId);
086    
087                    deleteCategoryProperty(categoryProperty);
088            }
089    
090            @Override
091            public List<AssetCategoryProperty> getCategoryProperties() {
092                    return assetCategoryPropertyPersistence.findAll();
093            }
094    
095            @Override
096            public List<AssetCategoryProperty> getCategoryProperties(long entryId) {
097                    return assetCategoryPropertyPersistence.findByCategoryId(entryId);
098            }
099    
100            @Override
101            public AssetCategoryProperty getCategoryProperty(long categoryPropertyId)
102                    throws PortalException {
103    
104                    return assetCategoryPropertyPersistence.findByPrimaryKey(
105                            categoryPropertyId);
106            }
107    
108            @Override
109            public AssetCategoryProperty getCategoryProperty(
110                            long categoryId, String key)
111                    throws PortalException {
112    
113                    return assetCategoryPropertyPersistence.findByCA_K(categoryId, key);
114            }
115    
116            @Override
117            public List<AssetCategoryProperty> getCategoryPropertyValues(
118                    long groupId, String key) {
119    
120                    return assetCategoryPropertyFinder.findByG_K(groupId, key);
121            }
122    
123            @Override
124            public AssetCategoryProperty updateCategoryProperty(
125                            long userId, long categoryPropertyId, String key, String value)
126                    throws PortalException {
127    
128                    validate(key, value);
129    
130                    AssetCategoryProperty categoryProperty =
131                            assetCategoryPropertyPersistence.findByPrimaryKey(
132                                    categoryPropertyId);
133    
134                    if (userId != 0) {
135                            User user = userPersistence.findByPrimaryKey(userId);
136    
137                            categoryProperty.setUserId(userId);
138                            categoryProperty.setUserName(user.getFullName());
139                    }
140    
141                    categoryProperty.setModifiedDate(new Date());
142                    categoryProperty.setKey(key);
143                    categoryProperty.setValue(value);
144    
145                    assetCategoryPropertyPersistence.update(categoryProperty);
146    
147                    return categoryProperty;
148            }
149    
150            @Override
151            public AssetCategoryProperty updateCategoryProperty(
152                            long categoryPropertyId, String key, String value)
153                    throws PortalException {
154    
155                    return updateCategoryProperty(0, categoryPropertyId, key, value);
156            }
157    
158            protected void validate(String key, String value) throws PortalException {
159                    if (!AssetUtil.isValidWord(key)) {
160                            throw new CategoryPropertyKeyException();
161                    }
162    
163                    if (!AssetUtil.isValidWord(value)) {
164                            throw new CategoryPropertyValueException();
165                    }
166            }
167    
168    }