001
014
015 package com.liferay.portal.verify;
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.ProjectionFactoryUtil;
020 import com.liferay.portal.kernel.dao.orm.Property;
021 import com.liferay.portal.kernel.dao.orm.PropertyFactoryUtil;
022 import com.liferay.portal.kernel.exception.PortalException;
023 import com.liferay.portal.kernel.exception.SystemException;
024 import com.liferay.portal.kernel.log.Log;
025 import com.liferay.portal.kernel.log.LogFactoryUtil;
026 import com.liferay.portal.kernel.util.ArrayUtil;
027 import com.liferay.portal.kernel.util.CharPool;
028 import com.liferay.portal.kernel.util.StringUtil;
029 import com.liferay.portal.model.GroupConstants;
030 import com.liferay.portal.model.Layout;
031 import com.liferay.portal.model.PortletPreferences;
032 import com.liferay.portal.service.ClassNameLocalServiceUtil;
033 import com.liferay.portal.service.LayoutLocalServiceUtil;
034 import com.liferay.portal.service.PortletPreferencesLocalServiceUtil;
035 import com.liferay.portal.service.persistence.PortletPreferencesActionableDynamicQuery;
036 import com.liferay.portlet.PortletPreferencesFactoryUtil;
037 import com.liferay.portlet.assetpublisher.util.AssetPublisher;
038 import com.liferay.portlet.dynamicdatamapping.model.DDMStructure;
039 import com.liferay.portlet.dynamicdatamapping.service.DDMStructureLocalServiceUtil;
040 import com.liferay.portlet.journal.model.JournalArticle;
041
042 import java.util.Arrays;
043 import java.util.List;
044
045 import javax.portlet.ReadOnlyException;
046
047
051 public class VerifyPortletPreferences extends VerifyProcess {
052
053 public static void cleanUpScopeIdPortletPreferences() throws Exception {
054 final long classNameId = ClassNameLocalServiceUtil.getClassNameId(
055 JournalArticle.class.getName());
056
057 ActionableDynamicQuery actionableDynamicQuery =
058 new PortletPreferencesActionableDynamicQuery() {
059
060 @Override
061 protected void addCriteria(DynamicQuery dynamicQuery) {
062 Property plidProperty = PropertyFactoryUtil.forName("plid");
063
064 DynamicQuery layoutDynamicQuery =
065 LayoutLocalServiceUtil.dynamicQuery();
066
067 layoutDynamicQuery.setProjection(
068 ProjectionFactoryUtil.property("plid"));
069
070 dynamicQuery.add(plidProperty.in(layoutDynamicQuery));
071 }
072
073 @Override
074 protected void performAction(Object object)
075 throws PortalException, SystemException {
076
077 PortletPreferences portletPreferences =
078 (PortletPreferences)object;
079
080 long plid = portletPreferences.getPlid();
081
082 Layout layout = LayoutLocalServiceUtil.getLayout(plid);
083
084 if (!layout.isTypePortlet()) {
085 return;
086 }
087
088 javax.portlet.PortletPreferences jxPortletPreferences =
089 PortletPreferencesFactoryUtil.strictFromXML(
090 layout.getCompanyId(), portletPreferences.getOwnerId(),
091 portletPreferences.getOwnerType(), plid,
092 portletPreferences.getPortletId(),
093 portletPreferences.getPreferences());
094
095 String[] scopeIds = jxPortletPreferences.getValues(
096 "scopeIds", null);
097
098 String preferenceName =
099 "classTypeIdsJournalArticleAssetRendererFactory";
100
101 String[] classTypeIds = jxPortletPreferences.getValues(
102 preferenceName, null);
103
104 try {
105 if (ArrayUtil.isNotEmpty(classTypeIds) ||
106 ArrayUtil.isNotEmpty(scopeIds)) {
107
108 if (ArrayUtil.isEmpty(scopeIds)) {
109 scopeIds = new String[] {
110 AssetPublisher.SCOPE_ID_GROUP_PREFIX +
111 GroupConstants.DEFAULT
112 };
113
114 jxPortletPreferences.setValue(
115 "scopeIds", scopeIds[0]);
116 }
117
118 long[] groupIds = getGroupIds(
119 scopeIds, layout.getGroupId());
120
121 List<DDMStructure> structures =
122 DDMStructureLocalServiceUtil.getStructures(
123 groupIds, classNameId);
124
125 long[] structureIds = new long[structures.size()];
126
127 for (DDMStructure structure : structures) {
128 structureIds = ArrayUtil.append(
129 structureIds, structure.getStructureId());
130 }
131
132 if (ArrayUtil.isNotEmpty(structureIds)) {
133 String structureIdsString = StringUtil.strip(
134 Arrays.toString(structureIds),
135 new char[] {
136 CharPool.CLOSE_BRACKET,
137 CharPool.OPEN_BRACKET, CharPool.SPACE
138 });
139
140 jxPortletPreferences.setValue(
141 preferenceName, structureIdsString);
142 }
143 else {
144 jxPortletPreferences.reset(preferenceName);
145 }
146 }
147 else {
148 jxPortletPreferences.reset(preferenceName);
149 }
150 }
151 catch (ReadOnlyException roe) {
152 if (_log.isWarnEnabled()) {
153 _log.warn(
154 "Unable to update portlet preferences " +
155 portletPreferences.getPortletPreferencesId());
156 }
157
158 return;
159 }
160
161 if (_log.isInfoEnabled()) {
162 _log.info(
163 "Updating portlet preferences " +
164 portletPreferences.getPortletPreferencesId());
165 }
166
167 PortletPreferencesLocalServiceUtil.updatePreferences(
168 portletPreferences.getOwnerId(),
169 portletPreferences.getOwnerType(), plid,
170 portletPreferences.getPortletId(), jxPortletPreferences);
171 }
172
173 };
174
175 actionableDynamicQuery.performActions();
176 }
177
178 private static long[] getGroupIds(String[] scopeIds, long defaultGroupId) {
179 long[] groupIds = new long[0];
180
181 for (String scopeId : scopeIds) {
182 if (!scopeId.startsWith(AssetPublisher.SCOPE_ID_GROUP_PREFIX)) {
183 continue;
184 }
185
186 long siteGroupId = 0;
187
188 String scopeIdSuffix = scopeId.substring(
189 AssetPublisher.SCOPE_ID_GROUP_PREFIX.length());
190
191 if (scopeIdSuffix.equals(GroupConstants.DEFAULT)) {
192 siteGroupId = defaultGroupId;
193 }
194 else {
195 siteGroupId = Long.valueOf(scopeIdSuffix);
196 }
197
198 groupIds = ArrayUtil.append(groupIds, siteGroupId);
199 }
200
201 return groupIds;
202 }
203
204 @Override
205 protected void doVerify() throws Exception {
206 cleanUpScopeIdPortletPreferences();
207 }
208
209 private static Log _log = LogFactoryUtil.getLog(
210 VerifyPortletPreferences.class);
211
212 }