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.ratings.kernel.transformer;
016    
017    import com.liferay.portal.kernel.dao.orm.ActionableDynamicQuery;
018    import com.liferay.portal.kernel.dao.orm.DynamicQuery;
019    import com.liferay.portal.kernel.dao.orm.Property;
020    import com.liferay.portal.kernel.dao.orm.PropertyFactoryUtil;
021    import com.liferay.portal.kernel.exception.PortalException;
022    import com.liferay.portal.kernel.util.StringPool;
023    import com.liferay.portal.kernel.util.UnicodeProperties;
024    import com.liferay.ratings.kernel.RatingsType;
025    import com.liferay.ratings.kernel.definition.PortletRatingsDefinitionUtil;
026    import com.liferay.ratings.kernel.definition.PortletRatingsDefinitionValues;
027    import com.liferay.ratings.kernel.model.RatingsEntry;
028    import com.liferay.ratings.kernel.service.RatingsEntryLocalServiceUtil;
029    import com.liferay.registry.Registry;
030    import com.liferay.registry.RegistryUtil;
031    import com.liferay.registry.ServiceTracker;
032    
033    import java.util.Map;
034    
035    import javax.portlet.PortletPreferences;
036    
037    /**
038     * @author Roberto D??az
039     * @author Sergio Gonz??lez
040     */
041    public class RatingsDataTransformerUtil {
042    
043            public static String getPropertyKey(String className) {
044                    return className + StringPool.UNDERLINE + "RatingsType";
045            }
046    
047            public static void transformCompanyRatingsData(
048                            final long companyId, PortletPreferences oldPortletPreferences,
049                            UnicodeProperties unicodeProperties)
050                    throws PortalException {
051    
052                    _instance._transformCompanyRatingsData(
053                            companyId, oldPortletPreferences, unicodeProperties);
054            }
055    
056            public static void transformGroupRatingsData(
057                            final long groupId, UnicodeProperties oldUnicodeProperties,
058                            UnicodeProperties unicodeProperties)
059                    throws PortalException {
060    
061                    _instance._transformGroupRatingsData(
062                            groupId, oldUnicodeProperties, unicodeProperties);
063            }
064    
065            private RatingsDataTransformerUtil() {
066                    Registry registry = RegistryUtil.getRegistry();
067    
068                    _serviceTracker = registry.trackServices(RatingsDataTransformer.class);
069    
070                    _serviceTracker.open();
071            }
072    
073            private void _transformCompanyRatingsData(
074                            final long companyId, PortletPreferences oldPortletPreferences,
075                            UnicodeProperties unicodeProperties)
076                    throws PortalException {
077    
078                    RatingsDataTransformer ratingsDataTransformer =
079                            _serviceTracker.getService();
080    
081                    if (ratingsDataTransformer == null) {
082                            return;
083                    }
084    
085                    Map<String, PortletRatingsDefinitionValues>
086                            portletRatingsDefinitionValuesMap =
087                                    PortletRatingsDefinitionUtil.
088                                            getPortletRatingsDefinitionValuesMap();
089    
090                    for (Map.Entry<String, PortletRatingsDefinitionValues> entry :
091                                    portletRatingsDefinitionValuesMap.entrySet()) {
092    
093                            String className = entry.getKey();
094    
095                            String propertyKey = getPropertyKey(className);
096    
097                            RatingsType fromRatingsType = RatingsType.parse(
098                                    oldPortletPreferences.getValue(propertyKey, StringPool.BLANK));
099    
100                            if (fromRatingsType == null) {
101                                    PortletRatingsDefinitionValues portletRatingsDefinitionValues =
102                                            entry.getValue();
103    
104                                    fromRatingsType =
105                                            portletRatingsDefinitionValues.getDefaultRatingsType();
106                            }
107    
108                            RatingsType toRatingsType = RatingsType.parse(
109                                    unicodeProperties.getProperty(propertyKey));
110    
111                            _transformRatingsData(
112                                    "companyId", companyId, className, fromRatingsType,
113                                    toRatingsType);
114                    }
115            }
116    
117            private void _transformGroupRatingsData(
118                            final long groupId, UnicodeProperties oldUnicodeProperties,
119                            UnicodeProperties unicodeProperties)
120                    throws PortalException {
121    
122                    RatingsDataTransformer ratingsDataTransformer =
123                            _serviceTracker.getService();
124    
125                    if (ratingsDataTransformer == null) {
126                            return;
127                    }
128    
129                    Map<String, PortletRatingsDefinitionValues>
130                            portletRatingsDefinitionValuesMap =
131                                    PortletRatingsDefinitionUtil.
132                                            getPortletRatingsDefinitionValuesMap();
133    
134                    for (Map.Entry<String, PortletRatingsDefinitionValues> entry :
135                                    portletRatingsDefinitionValuesMap.entrySet()) {
136    
137                            String className = entry.getKey();
138    
139                            String propertyKey = getPropertyKey(className);
140    
141                            RatingsType fromRatingsType = RatingsType.parse(
142                                    oldUnicodeProperties.getProperty(propertyKey));
143    
144                            if (fromRatingsType == null) {
145                                    PortletRatingsDefinitionValues portletRatingsDefinitionValues =
146                                            entry.getValue();
147    
148                                    fromRatingsType =
149                                            portletRatingsDefinitionValues.getDefaultRatingsType();
150                            }
151    
152                            RatingsType toRatingsType = RatingsType.parse(
153                                    unicodeProperties.getProperty(propertyKey));
154    
155                            _transformRatingsData(
156                                    "groupId", groupId, className, fromRatingsType, toRatingsType);
157                    }
158            }
159    
160            private void _transformRatingsData(
161                            final String classPKFieldName, final long classPKFieldValue,
162                            final String className, RatingsType fromRatingsType,
163                            RatingsType toRatingsType)
164                    throws PortalException {
165    
166                    if ((toRatingsType == null) || fromRatingsType.equals(toRatingsType)) {
167                            return;
168                    }
169    
170                    RatingsDataTransformer ratingsDataTransformer =
171                            _serviceTracker.getService();
172    
173                    ActionableDynamicQuery.PerformActionMethod<RatingsEntry>
174                            performActionMethod = ratingsDataTransformer.transformRatingsData(
175                                    fromRatingsType, toRatingsType);
176    
177                    if (performActionMethod == null) {
178                            return;
179                    }
180    
181                    ActionableDynamicQuery ratingsEntryActionableDynamicQuery =
182                            RatingsEntryLocalServiceUtil.getActionableDynamicQuery();
183    
184                    ratingsEntryActionableDynamicQuery.setAddCriteriaMethod(
185                            new ActionableDynamicQuery.AddCriteriaMethod() {
186    
187                                    @Override
188                                    public void addCriteria(DynamicQuery dynamicQuery) {
189                                            Property property = PropertyFactoryUtil.forName(
190                                                    classPKFieldName);
191    
192                                            dynamicQuery.add(property.eq(classPKFieldValue));
193    
194                                            property = PropertyFactoryUtil.forName("className");
195    
196                                            dynamicQuery.add(property.eq(className));
197                                    }
198    
199                            });
200    
201                    ratingsEntryActionableDynamicQuery.setPerformActionMethod(
202                            performActionMethod);
203    
204                    ratingsEntryActionableDynamicQuery.performActions();
205            }
206    
207            private static final RatingsDataTransformerUtil _instance =
208                    new RatingsDataTransformerUtil();
209    
210            private final ServiceTracker<RatingsDataTransformer, RatingsDataTransformer>
211                    _serviceTracker;
212    
213    }