001
014
015 package com.liferay.portal.verify;
016
017 import com.liferay.portal.kernel.dao.db.DB;
018 import com.liferay.portal.kernel.dao.db.DBFactoryUtil;
019 import com.liferay.portal.kernel.log.Log;
020 import com.liferay.portal.kernel.log.LogFactoryUtil;
021 import com.liferay.portal.kernel.util.GetterUtil;
022 import com.liferay.portal.kernel.util.HtmlUtil;
023 import com.liferay.portal.kernel.util.Validator;
024 import com.liferay.portal.service.ResourceLocalServiceUtil;
025 import com.liferay.portlet.asset.NoSuchEntryException;
026 import com.liferay.portlet.asset.service.AssetEntryLocalServiceUtil;
027 import com.liferay.portlet.journal.model.JournalArticle;
028 import com.liferay.portlet.journal.model.JournalStructure;
029 import com.liferay.portlet.journal.model.JournalTemplate;
030 import com.liferay.portlet.journal.service.JournalArticleLocalServiceUtil;
031 import com.liferay.portlet.journal.service.JournalStructureLocalServiceUtil;
032 import com.liferay.portlet.journal.service.JournalTemplateLocalServiceUtil;
033
034 import java.util.List;
035
036
039 public class VerifyJournal extends VerifyProcess {
040
041 public static final long DEFAULT_GROUP_ID = 14;
042
043 public static final int NUM_OF_ARTICLES = 5;
044
045 protected void doVerify() throws Exception {
046
047
048
049 verifyOracleNewLine();
050
051
052
053 List<JournalStructure> structures =
054 JournalStructureLocalServiceUtil.getStructures();
055
056 for (JournalStructure structure : structures) {
057 ResourceLocalServiceUtil.addResources(
058 structure.getCompanyId(), 0, 0,
059 JournalStructure.class.getName(), structure.getId(), false,
060 true, true);
061 }
062
063 if (_log.isDebugEnabled()) {
064 _log.debug("Permissions verified for structures");
065 }
066
067
068
069 List<JournalTemplate> templates =
070 JournalTemplateLocalServiceUtil.getTemplates();
071
072 for (JournalTemplate template : templates) {
073 ResourceLocalServiceUtil.addResources(
074 template.getCompanyId(), 0, 0,
075 JournalTemplate.class.getName(), template.getId(), false, true,
076 true);
077 }
078
079 if (_log.isDebugEnabled()) {
080 _log.debug("Permissions verified for templates");
081 }
082
083
084
085 List<JournalArticle> articles =
086 JournalArticleLocalServiceUtil.getArticles();
087
088 for (JournalArticle article : articles) {
089 long groupId = article.getGroupId();
090 String articleId = article.getArticleId();
091 double version = article.getVersion();
092 String structureId = article.getStructureId();
093
094 if (article.getResourcePrimKey() <= 0) {
095 article =
096 JournalArticleLocalServiceUtil.checkArticleResourcePrimKey(
097 groupId, articleId, version);
098 }
099
100 ResourceLocalServiceUtil.addResources(
101 article.getCompanyId(), 0, 0, JournalArticle.class.getName(),
102 article.getResourcePrimKey(), false, true, true);
103
104 try {
105 AssetEntryLocalServiceUtil.getEntry(
106 JournalArticle.class.getName(),
107 article.getResourcePrimKey());
108 }
109 catch (NoSuchEntryException nsee) {
110 try {
111 JournalArticleLocalServiceUtil.updateAsset(
112 article.getUserId(), article, null, null);
113 }
114 catch (Exception e) {
115 if (_log.isWarnEnabled()) {
116 _log.warn(
117 "Unable to update asset for article " +
118 article.getId() + ": " + e.getMessage());
119 }
120 }
121 }
122
123 String content = GetterUtil.getString(article.getContent());
124
125 String newContent = HtmlUtil.replaceMsWordCharacters(content);
126
127 if (Validator.isNotNull(structureId)) {
128
134 }
135
136 if (!content.equals(newContent)) {
137 JournalArticleLocalServiceUtil.updateContent(
138 groupId, articleId, version, newContent);
139 }
140
141 JournalArticleLocalServiceUtil.checkStructure(
142 groupId, articleId, version);
143 }
144
145 if (_log.isDebugEnabled()) {
146 _log.debug("Permissions and assets verified for articles");
147 }
148 }
149
150 protected void verifyOracleNewLine() throws Exception {
151 DB db = DBFactoryUtil.getDB();
152
153 if (!db.getType().equals(DB.TYPE_ORACLE)) {
154 return;
155 }
156
157
158
159
160
161
162
163 boolean checkNewLine = false;
164
165 List<JournalArticle> articles =
166 JournalArticleLocalServiceUtil.getArticles(
167 DEFAULT_GROUP_ID, 0, NUM_OF_ARTICLES);
168
169 for (JournalArticle article : articles) {
170 String content = article.getContent();
171
172 if ((content != null) && (content.indexOf("\\n") != -1)) {
173 articles = JournalArticleLocalServiceUtil.getArticles(
174 DEFAULT_GROUP_ID);
175
176 for (int j = 0; j < articles.size(); j++) {
177 article = articles.get(j);
178
179 JournalArticleLocalServiceUtil.checkNewLine(
180 article.getGroupId(), article.getArticleId(),
181 article.getVersion());
182 }
183
184 checkNewLine = true;
185
186 break;
187 }
188 }
189
190
191
192 if (!checkNewLine) {
193 if (_log.isInfoEnabled()) {
194 _log.debug("Do not fix oracle new line");
195 }
196
197 return;
198 }
199 else {
200 if (_log.isInfoEnabled()) {
201 _log.info("Fix oracle new line");
202 }
203 }
204
205 List<JournalStructure> structures =
206 JournalStructureLocalServiceUtil.getStructures(
207 DEFAULT_GROUP_ID, 0, 1);
208
209 if (structures.size() == 1) {
210 JournalStructure structure = structures.get(0);
211
212 String xsd = structure.getXsd();
213
214 if ((xsd != null) && (xsd.indexOf("\\n") != -1)) {
215 structures = JournalStructureLocalServiceUtil.getStructures(
216 DEFAULT_GROUP_ID);
217
218 for (int i = 0; i < structures.size(); i++) {
219 structure = structures.get(i);
220
221 JournalStructureLocalServiceUtil.checkNewLine(
222 structure.getGroupId(), structure.getStructureId());
223 }
224 }
225 }
226
227 List<JournalTemplate> templates =
228 JournalTemplateLocalServiceUtil.getTemplates(
229 DEFAULT_GROUP_ID, 0, 1);
230
231 if (templates.size() == 1) {
232 JournalTemplate template = templates.get(0);
233
234 String xsl = template.getXsl();
235
236 if ((xsl != null) && (xsl.indexOf("\\n") != -1)) {
237 templates = JournalTemplateLocalServiceUtil.getTemplates(
238 DEFAULT_GROUP_ID);
239
240 for (int i = 0; i < templates.size(); i++) {
241 template = templates.get(i);
242
243 JournalTemplateLocalServiceUtil.checkNewLine(
244 template.getGroupId(), template.getTemplateId());
245 }
246 }
247 }
248 }
249
250 private static Log _log = LogFactoryUtil.getLog(VerifyJournal.class);
251
252 }