Java tutorial
/** * Copyright (c) 2014-present Yg0R2. All rights reserved. * * This library is free software; you can redistribute it and/or modify it under * the terms of the GNU Lesser General Public License as published by the Free * Software Foundation; either version 2.1 of the License, or (at your option) * any later version. * * This library is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more * details. */ package yg0r2.datamanipulator.handler.content; import com.liferay.portal.kernel.exception.SystemException; import com.liferay.portal.kernel.util.KeyValuePair; import com.liferay.portal.kernel.util.StringPool; import com.liferay.portal.kernel.util.Validator; import com.liferay.portal.service.ServiceContext; import com.liferay.portal.util.PortalUtil; import com.liferay.portlet.dynamicdatamapping.model.DDMStructure; import com.liferay.portlet.dynamicdatamapping.model.DDMTemplate; import com.liferay.portlet.dynamicdatamapping.service.DDMStructureLocalServiceUtil; import com.liferay.portlet.dynamicdatamapping.service.DDMTemplateLocalServiceUtil; import com.liferay.portlet.journal.model.JournalArticle; import com.liferay.portlet.journal.model.JournalArticleConstants; import com.liferay.portlet.journal.service.JournalArticleLocalServiceUtil; import java.io.File; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import jodd.bean.BeanUtil; import yg0r2.datamanipulator.annotation.Handler; import yg0r2.datamanipulator.annotation.HandlerType; import yg0r2.datamanipulator.context.RequestContext; import yg0r2.datamanipulator.displayfield.DisplayFields; import yg0r2.datamanipulator.displayfield.FieldKeys; import yg0r2.datamanipulator.handler.BaseHandler; import yg0r2.datamanipulator.util.RandomUtil; import yg0r2.datamanipulator.util.StringUtil; /** * @author Yg0R2 */ @Handler(type = HandlerType.CONTENT, displayName = "Journal Handler") public class JournalArticleHandler extends BaseHandler { public static final String AUTO_ARTICLE_ID = "auto-article-id"; public static final String STRUCTURE_LIST = "structure-list"; public JournalArticleHandler() throws Exception { super("Journal Article", "journal-article"); } @Override public DisplayFields getDisplayFields(long groupId) throws Exception { DisplayFields displayFields = new DisplayFields(); displayFields.addUserMultiSelect(FieldKeys.MULTI_SELECT_USER_LIST); displayFields.addInfo(getDisplayFieldName(FieldKeys.MULTI_SELECT_USER_LIST)); displayFields.addSeparator(""); displayFields.addLabel(getDisplayFieldName()); displayFields.addCheckbox(getDisplayFieldName(AUTO_ARTICLE_ID), true); displayFields.addSelectList(getDisplayFieldName(STRUCTURE_LIST), _getStructureNameKeys(groupId)); displayFields.addCount(getDisplayFieldName(FieldKeys.INPUT_COUNT)); displayFields.addUpdateCount(getDisplayFieldName(FieldKeys.INPUT_UPDATE_COUNT)); /*displayFields.addSeparator(""); displayFields.addLabel(getDisplayFieldName("display-date")); displayFields.addDate(getDisplayFieldName("display-date")); displayFields.addLabel(getDisplayFieldName("expiration-date")); displayFields.addDate(getDisplayFieldName("expiration-date")); displayFields.addLabel(getDisplayFieldName("review-date")); displayFields.addDate(getDisplayFieldName("review-date")); displayFields.addInfo(getDisplayFieldName("date-info")); displayFields.addSeparator("");*/ return displayFields; } @Override protected Class<?>[] getAddArgClazzs() { if (buildNumber < 6200) { return new Class<?>[] { Long.TYPE, Long.TYPE, Long.TYPE, Long.TYPE, String.class, Boolean.TYPE, Double.TYPE, Map.class, Map.class, String.class, String.class, String.class, String.class, String.class, Integer.TYPE, Integer.TYPE, Integer.TYPE, Integer.TYPE, Integer.TYPE, Integer.TYPE, Integer.TYPE, Integer.TYPE, Integer.TYPE, Integer.TYPE, Boolean.TYPE, Integer.TYPE, Integer.TYPE, Integer.TYPE, Integer.TYPE, Integer.TYPE, Boolean.TYPE, Boolean.TYPE, Boolean.TYPE, String.class, File.class, Map.class, String.class, ServiceContext.class }; } return new Class<?>[] { Long.TYPE, Long.TYPE, Long.TYPE, Long.TYPE, Long.TYPE, String.class, Boolean.TYPE, Double.TYPE, Map.class, Map.class, String.class, String.class, String.class, String.class, String.class, Integer.TYPE, Integer.TYPE, Integer.TYPE, Integer.TYPE, Integer.TYPE, Integer.TYPE, Integer.TYPE, Integer.TYPE, Integer.TYPE, Integer.TYPE, Boolean.TYPE, Integer.TYPE, Integer.TYPE, Integer.TYPE, Integer.TYPE, Integer.TYPE, Boolean.TYPE, Boolean.TYPE, Boolean.TYPE, String.class, File.class, Map.class, String.class, ServiceContext.class }; } @Override protected String[] getAddArgNames() { if (buildNumber < 6200) { return new String[] { "userId", "groupId", "classNameId", "classPK", "articleId", "autoArticleId", "version", "titleMap", "descriptionMap", "content", "type", "structureId", "templateId", "layoutUuid", "displayDateMonth", "displayDateDay", "displayDateYear", "displayDateHour", "displayDateMinute", "expirationDateMonth", "expirationDateDay", "expirationDateYear", "expirationDateHour", "expirationDateMinute", "neverExpire", "reviewDateMonth", "reviewDateDay", "reviewDateYear", "reviewDateHour", "reviewDateMinute", "neverReview", "indexable", "smallImage", "smallImageURL", "smallImageFile", "images", "articleURL", "serviceContext" }; } return new String[] { "userId", "groupId", "folderId", "classNameId", "classPK", "articleId", "autoArticleId", "version", "titleMap", "descriptionMap", "content", "type", "ddmStructureKey", "ddmTemplateKey", "layoutUuid", "displayDateMonth", "displayDateDay", "displayDateYear", "displayDateHour", "displayDateMinute", "expirationDateMonth", "expirationDateDay", "expirationDateYear", "expirationDateHour", "expirationDateMinute", "neverExpire", "reviewDateMonth", "reviewDateDay", "reviewDateYear", "reviewDateHour", "reviewDateMinute", "neverReview", "indexable", "smallImage", "smallImageURL", "smallImageFile", "images", "articleURL", "serviceContext" }; } @Override protected Class<?> getAddClass() throws ClassNotFoundException { return JournalArticleLocalServiceUtil.class; } @Override protected Map<String, Object> getAddEntrySpecifiedArgs(RequestContext requestContext) throws Exception { boolean autoArticleId = requestContext.getBoolean(getDisplayFieldName(AUTO_ARTICLE_ID)); String articleId; if (autoArticleId) { articleId = StringPool.BLANK; } else { StringBuilder sb = new StringBuilder(); sb.append(requestContext.getString("entryName")); sb.append(requestContext.getString("entryCount")); sb.append("-ARTICLE-ID-"); sb.append(RandomUtil.nextString()); articleId = sb.toString().toUpperCase(); articleId = articleId.replace(StringPool.SPACE, StringPool.DASH); } long structureId = requestContext.getLong(getDisplayFieldName(STRUCTURE_LIST)); long templateId = 0; String structureKey = StringPool.BLANK; String templateKey = StringPool.BLANK; if (structureId != 0) { DDMStructure strucrute = DDMStructureLocalServiceUtil.getStructure(structureId); structureKey = strucrute.getStructureKey(); List<DDMTemplate> templates = DDMTemplateLocalServiceUtil.getTemplates(structureId); DDMTemplate template = templates.get(0); if (Validator.isNotNull(templates) && templates.size() > 0) { templateId = template.getTemplateId(); } if (templateId != 0) { try { templateKey = (String) BeanUtil.getProperty(template, "templateKey"); } catch (Exception e) { templateKey = String.valueOf(templateId); } } } int index = RandomUtil.nextInt(JournalArticleConstants.TYPES.length); String type = JournalArticleConstants.TYPES[index]; Map<String, Object> args = new HashMap<String, Object>(); args.put("articleId", articleId); args.put("articleURL", requestContext.getString("createURL")); args.put("autoArticleId", autoArticleId); args.put("classNameId", (long) 0); args.put("classPK", (long) 0); args.put("content", StringUtil.getLocalizedContent("")); args.put("layoutUuid", null); //args.put("neverExpire", RandomUtil.nextBoolean()); args.put("neverExpire", true); //args.put("neverReview", RandomUtil.nextBoolean()); args.put("neverReview", true); args.put("structureId", structureKey); args.put("structureKey", structureKey); args.put("ddmStructureKey", structureKey); args.put("templateId", structureKey); args.put("templateKey", templateKey); args.put("ddmTemplateKey", templateKey); args.put("type", type); args.put("version", JournalArticleConstants.VERSION_DEFAULT); return args; } @Override protected String getAddMethodName() { return "addArticle"; } @Override protected List<String> getChildHandlerNames() { return new ArrayList<String>(0); } @Override protected Object getClassPK(Object entry) { if (Validator.isNull(entry)) { return StringPool.BLANK; } return BeanUtil.getDeclaredProperty(entry, getClassPKName()); } @Override protected String getClassPKName() { return "articleId"; } @Override protected String getParentClassPKName() { //if (buildNumber < 6200) { return null; //} //return "folderId"; } @Override protected Class<?>[] getUpdateArgClazzs() { if (buildNumber < 6200) { return new Class<?>[] { Long.TYPE, Long.TYPE, String.class, Double.TYPE, Map.class, Map.class, String.class, String.class, ServiceContext.class }; } return new Class<?>[] { Long.TYPE, Long.TYPE, Long.TYPE, String.class, Double.TYPE, Map.class, Map.class, String.class, String.class, ServiceContext.class }; } @Override protected String[] getUpdateArgNames() { if (buildNumber < 6200) { return new String[] { "userId", "groupId", "articleId", "version", "titleMap", "descriptionMap", "content", "layoutUuid", "serviceContext" }; } return new String[] { "userId", "groupId", "folderId", "articleId", "version", "titleMap", "descriptionMap", "content", "layoutUuid", "serviceContext" }; } @Override protected Class<?> getUpdateClass() throws ClassNotFoundException { return JournalArticleLocalServiceUtil.class; } @Override protected Map<String, Object> getUpdateEntrySpecifiedArgs(Object entry, RequestContext requestContext) throws Exception { Map<String, Object> args = new HashMap<String, Object>(1); args.put("content", StringUtil.getLocalizedContent("")); return args; } @Override protected String getUpdateMethodName() { return "updateArticle"; } private List<KeyValuePair> _getStructureNameKeys(long groupId) throws SystemException { List<DDMStructure> structures = DDMStructureLocalServiceUtil.getStructures(groupId); List<KeyValuePair> structureNameKeyPairs = new ArrayList<KeyValuePair>(structures.size()); for (DDMStructure structure : structures) { if (structure.getClassNameId() == _journalClassNameId) { String structureKey = String.valueOf(structure.getStructureId()); String structureName = structure.getName(); structureNameKeyPairs.add(new KeyValuePair(structureName, structureKey)); } } return structureNameKeyPairs; } private long _journalClassNameId = PortalUtil.getClassNameId(JournalArticle.class); }