001
014
015 package com.liferay.portal.verify;
016
017 import com.liferay.portal.exception.LayoutFriendlyURLException;
018 import com.liferay.portal.kernel.dao.orm.ActionableDynamicQuery;
019 import com.liferay.portal.kernel.language.LanguageUtil;
020 import com.liferay.portal.kernel.log.Log;
021 import com.liferay.portal.kernel.log.LogFactoryUtil;
022 import com.liferay.portal.kernel.util.LocaleUtil;
023 import com.liferay.portal.kernel.util.StringBundler;
024 import com.liferay.portal.kernel.util.StringPool;
025 import com.liferay.portal.kernel.util.Validator;
026 import com.liferay.portal.model.Layout;
027 import com.liferay.portal.model.LayoutFriendlyURL;
028 import com.liferay.portal.service.LayoutFriendlyURLLocalServiceUtil;
029 import com.liferay.portal.service.LayoutLocalServiceUtil;
030
031 import java.util.ArrayList;
032 import java.util.List;
033
034
039 public class VerifyLayout extends VerifyProcess {
040
041 protected void deleteOrphanedLayouts() throws Exception {
042 runSQL(
043 "delete from Layout where layoutPrototypeUuid != '' and " +
044 "layoutPrototypeUuid not in (select uuid_ from " +
045 "LayoutPrototype)");
046 }
047
048 @Override
049 protected void doVerify() throws Exception {
050 deleteOrphanedLayouts();
051 verifyFriendlyURL();
052 verifyLayoutIdFriendlyURL();
053 verifyLayoutPrototypeLinkEnabled();
054 verifyUuid();
055 }
056
057 protected List<Layout> getInvalidLayoutIdFriendlyURLLayouts()
058 throws Exception {
059
060 final List<Layout> layouts = new ArrayList<>();
061
062 ActionableDynamicQuery actionableDynamicQuery =
063 LayoutLocalServiceUtil.getActionableDynamicQuery();
064
065 actionableDynamicQuery.setPerformActionMethod(
066 new ActionableDynamicQuery.PerformActionMethod<Layout>() {
067
068 @Override
069 public void performAction(Layout layout) {
070 String friendlyURL = layout.getFriendlyURL();
071
072 friendlyURL = friendlyURL.substring(1);
073
074 if (Validator.isNumber(friendlyURL) &&
075 !friendlyURL.equals(
076 String.valueOf(layout.getLayoutId()))) {
077
078 layouts.add(layout);
079 }
080 }
081
082 });
083
084 actionableDynamicQuery.performActions();
085
086 return layouts;
087 }
088
089 protected void verifyFriendlyURL() throws Exception {
090 List<Layout> layouts =
091 LayoutLocalServiceUtil.getNullFriendlyURLLayouts();
092
093 for (Layout layout : layouts) {
094 List<LayoutFriendlyURL> layoutFriendlyURLs =
095 LayoutFriendlyURLLocalServiceUtil.getLayoutFriendlyURLs(
096 layout.getPlid());
097
098 for (LayoutFriendlyURL layoutFriendlyURL : layoutFriendlyURLs) {
099 String friendlyURL = StringPool.SLASH + layout.getLayoutId();
100
101 LayoutLocalServiceUtil.updateFriendlyURL(
102 layout.getUserId(), layout.getPlid(), friendlyURL,
103 layoutFriendlyURL.getLanguageId());
104 }
105 }
106 }
107
108 protected void verifyLayoutIdFriendlyURL() throws Exception {
109 while (true) {
110 List<Layout> layouts = getInvalidLayoutIdFriendlyURLLayouts();
111
112 if (layouts.isEmpty()) {
113 break;
114 }
115
116 for (Layout layout : layouts) {
117 if (verifyLayoutIdFriendlyURL(layout)) {
118 continue;
119 }
120 }
121 }
122 }
123
124 protected boolean verifyLayoutIdFriendlyURL(Layout layout)
125 throws Exception {
126
127 String oldFriendlyURL = layout.getFriendlyURL();
128 String newFriendlyURL = StringPool.SLASH + layout.getLayoutId();
129
130 if (_log.isDebugEnabled()) {
131 _log.debug(
132 "Updating layout " + layout.getPlid() + " from friendly URL " +
133 oldFriendlyURL + " to friendly URL " + newFriendlyURL);
134 }
135
136 List<LayoutFriendlyURL> layoutFriendlyURLs =
137 LayoutFriendlyURLLocalServiceUtil.getLayoutFriendlyURLs(
138 layout.getPlid());
139
140 for (LayoutFriendlyURL layoutFriendlyURL : layoutFriendlyURLs) {
141 if (!oldFriendlyURL.equals(layoutFriendlyURL.getFriendlyURL())) {
142 return true;
143 }
144
145 try {
146 layout = LayoutLocalServiceUtil.updateFriendlyURL(
147 layout.getUserId(), layout.getPlid(), newFriendlyURL,
148 layoutFriendlyURL.getLanguageId());
149 }
150 catch (LayoutFriendlyURLException lfurle) {
151 int type = lfurle.getType();
152
153 if (type == LayoutFriendlyURLException.DUPLICATE) {
154 return true;
155 }
156 else {
157 throw lfurle;
158 }
159 }
160 }
161
162 try {
163 Layout duplicateLayout =
164 LayoutLocalServiceUtil.fetchLayoutByFriendlyURL(
165 layout.getGroupId(), layout.isPrivateLayout(),
166 newFriendlyURL);
167
168 if (duplicateLayout != null) {
169 throw new LayoutFriendlyURLException(
170 LayoutFriendlyURLException.DUPLICATE);
171 }
172
173 LayoutLocalServiceUtil.updateFriendlyURL(
174 layout.getUserId(), layout.getPlid(), newFriendlyURL,
175 LanguageUtil.getLanguageId(LocaleUtil.getSiteDefault()));
176 }
177 catch (LayoutFriendlyURLException lfurle) {
178 int type = lfurle.getType();
179
180 if (type == LayoutFriendlyURLException.DUPLICATE) {
181 return true;
182 }
183 else {
184 throw lfurle;
185 }
186 }
187
188 return false;
189 }
190
191 protected void verifyLayoutPrototypeLinkEnabled() throws Exception {
192 runSQL(
193 "update Layout set layoutPrototypeLinkEnabled = [$FALSE$] where " +
194 "type_ = 'link_to_layout' and layoutPrototypeLinkEnabled = " +
195 "[$TRUE$]");
196 }
197
198 protected void verifyUuid() throws Exception {
199 verifyUuid("AssetEntry");
200
201 runSQL(
202 "update Layout set uuid_ = sourcePrototypeLayoutUuid where " +
203 "sourcePrototypeLayoutUuid != '' and uuid_ != " +
204 "sourcePrototypeLayoutUuid");
205 }
206
207 protected void verifyUuid(String tableName) throws Exception {
208 StringBundler sb = new StringBundler(12);
209
210 sb.append("update ");
211 sb.append(tableName);
212 sb.append(" set layoutUuid = (select distinct ");
213 sb.append("sourcePrototypeLayoutUuid from Layout where ");
214 sb.append("Layout.uuid_ = ");
215 sb.append(tableName);
216 sb.append(".layoutUuid) where exists (select 1 from Layout where ");
217 sb.append("Layout.uuid_ = ");
218 sb.append(tableName);
219 sb.append(".layoutUuid and Layout.uuid_ != ");
220 sb.append("Layout.sourcePrototypeLayoutUuid and ");
221 sb.append("Layout.sourcePrototypeLayoutUuid != '')");
222
223 runSQL(sb.toString());
224 }
225
226 private static final Log _log = LogFactoryUtil.getLog(VerifyLayout.class);
227
228 }