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.exportimport.lar;
016    
017    import com.liferay.portal.kernel.log.Log;
018    import com.liferay.portal.kernel.log.LogFactoryUtil;
019    import com.liferay.portal.kernel.util.GetterUtil;
020    import com.liferay.portal.kernel.util.StringBundler;
021    import com.liferay.portal.kernel.util.StringPool;
022    import com.liferay.portal.kernel.util.StringUtil;
023    import com.liferay.portal.kernel.util.Validator;
024    import com.liferay.portal.model.Portlet;
025    
026    import java.util.Map;
027    
028    import javax.portlet.PortletPreferences;
029    
030    /**
031     * @author Eduardo Garcia
032     */
033    public class DefaultConfigurationPortletDataHandler
034            extends BasePortletDataHandler {
035    
036            public DefaultConfigurationPortletDataHandler() {
037                    setDataLevel(DataLevel.PORTLET_INSTANCE);
038            }
039    
040            @Override
041            public PortletPreferences deleteData(
042                    PortletDataContext portletDataContext, String portletId,
043                    PortletPreferences portletPreferences) {
044    
045                    return null;
046            }
047    
048            @Override
049            public String exportData(
050                    PortletDataContext portletDataContext, String portletId,
051                    PortletPreferences portletPreferences) {
052    
053                    return null;
054            }
055    
056            @Override
057            public long getExportModelCount(ManifestSummary manifestSummary) {
058                    return 0;
059            }
060    
061            @Override
062            public PortletPreferences importData(
063                    PortletDataContext portletDataContext, String portletId,
064                    PortletPreferences portletPreferences, String data) {
065    
066                    return null;
067            }
068    
069            protected String getExportPortletPreferencesUuid(
070                            PortletDataContext portletDataContext, Portlet portlet,
071                            String className, long primaryKeyLong)
072                    throws Exception {
073    
074                    return StringPool.BLANK;
075            }
076    
077            protected Long getImportPortletPreferencesNewPrimaryKey(
078                            PortletDataContext portletDataContext, Class<?> clazz,
079                            long companyGroupId, Map<Long, Long> primaryKeys, String uuid)
080                    throws Exception {
081    
082                    return Long.valueOf(0L);
083            }
084    
085            protected void updateExportPortletPreferencesClassPKs(
086                            PortletDataContext portletDataContext, Portlet portlet,
087                            PortletPreferences portletPreferences, String key, String className)
088                    throws Exception {
089    
090                    String[] oldValues = portletPreferences.getValues(key, null);
091    
092                    if (oldValues == null) {
093                            return;
094                    }
095    
096                    String[] newValues = new String[oldValues.length];
097    
098                    for (int i = 0; i < oldValues.length; i++) {
099                            String oldValue = oldValues[i];
100    
101                            String newValue = oldValue;
102    
103                            String[] primaryKeys = StringUtil.split(oldValue);
104    
105                            for (String primaryKey : primaryKeys) {
106                                    if (!Validator.isNumber(primaryKey)) {
107                                            break;
108                                    }
109    
110                                    long primaryKeyLong = GetterUtil.getLong(primaryKey);
111    
112                                    String uuid = getExportPortletPreferencesUuid(
113                                            portletDataContext, portlet, className, primaryKeyLong);
114    
115                                    if (Validator.isNull(uuid)) {
116                                            if (_log.isWarnEnabled()) {
117                                                    _log.warn(
118                                                            "Unable to get UUID for class " + className +
119                                                                    " with primary key " + primaryKeyLong);
120                                            }
121    
122                                            continue;
123                                    }
124    
125                                    newValue = StringUtil.replace(newValue, primaryKey, uuid);
126                            }
127    
128                            newValues[i] = newValue;
129                    }
130    
131                    portletPreferences.setValues(key, newValues);
132            }
133    
134            protected void updateImportPortletPreferencesClassPKs(
135                            PortletDataContext portletDataContext,
136                            PortletPreferences portletPreferences, String key, Class<?> clazz,
137                            long companyGroupId)
138                    throws Exception {
139    
140                    String[] oldValues = portletPreferences.getValues(key, null);
141    
142                    if (oldValues == null) {
143                            return;
144                    }
145    
146                    Map<Long, Long> primaryKeys =
147                            (Map<Long, Long>)portletDataContext.getNewPrimaryKeysMap(clazz);
148    
149                    String[] newValues = new String[oldValues.length];
150    
151                    for (int i = 0; i < oldValues.length; i++) {
152                            String oldValue = oldValues[i];
153    
154                            String newValue = oldValue;
155    
156                            String[] uuids = StringUtil.split(oldValue);
157    
158                            for (String uuid : uuids) {
159                                    Long newPrimaryKey = getImportPortletPreferencesNewPrimaryKey(
160                                            portletDataContext, clazz, companyGroupId, primaryKeys,
161                                            uuid);
162    
163                                    if (Validator.isNull(newPrimaryKey)) {
164                                            if (_log.isWarnEnabled()) {
165                                                    StringBundler sb = new StringBundler(8);
166    
167                                                    sb.append("Unable to get primary key for ");
168                                                    sb.append(clazz);
169                                                    sb.append(" with UUID ");
170                                                    sb.append(uuid);
171                                                    sb.append(" in company group ");
172                                                    sb.append(companyGroupId);
173                                                    sb.append(" or in group ");
174                                                    sb.append(portletDataContext.getScopeGroupId());
175    
176                                                    _log.warn(sb.toString());
177                                            }
178                                    }
179                                    else {
180                                            newValue = StringUtil.replace(
181                                                    newValue, uuid, newPrimaryKey.toString());
182                                    }
183                            }
184    
185                            newValues[i] = newValue;
186                    }
187    
188                    portletPreferences.setValues(key, newValues);
189            }
190    
191            private static final Log _log = LogFactoryUtil.getLog(
192                    DefaultConfigurationPortletDataHandler.class);
193    
194    }