/modules/apps/portal-rules-engine/portal-rules-engine-sample-web/src/main/java/com/liferay/portal/rules/engine/sample/web/internal/portlet/action/SampleDroolsConfigurationAction.java

https://github.com/danielreuther/liferay-portal · Java · 156 lines · 112 code · 28 blank · 16 comment · 10 complexity · b35a3f6985e32a22596e476ae825e1eb MD5 · raw file

  1. /**
  2. * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
  3. *
  4. * This library is free software; you can redistribute it and/or modify it under
  5. * the terms of the GNU Lesser General Public License as published by the Free
  6. * Software Foundation; either version 2.1 of the License, or (at your option)
  7. * any later version.
  8. *
  9. * This library is distributed in the hope that it will be useful, but WITHOUT
  10. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  11. * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
  12. * details.
  13. */
  14. package com.liferay.portal.rules.engine.sample.web.internal.portlet.action;
  15. import com.liferay.portal.kernel.log.Log;
  16. import com.liferay.portal.kernel.log.LogFactoryUtil;
  17. import com.liferay.portal.kernel.portlet.ConfigurationAction;
  18. import com.liferay.portal.kernel.portlet.DefaultConfigurationAction;
  19. import com.liferay.portal.kernel.resource.StringResourceRetriever;
  20. import com.liferay.portal.kernel.servlet.SessionErrors;
  21. import com.liferay.portal.kernel.servlet.SessionMessages;
  22. import com.liferay.portal.kernel.util.ArrayUtil;
  23. import com.liferay.portal.kernel.util.Constants;
  24. import com.liferay.portal.kernel.util.ParamUtil;
  25. import com.liferay.portal.kernel.util.Portal;
  26. import com.liferay.portal.kernel.util.StringUtil;
  27. import com.liferay.portal.kernel.util.Validator;
  28. import com.liferay.portal.rules.engine.RulesEngine;
  29. import com.liferay.portal.rules.engine.RulesEngineException;
  30. import com.liferay.portal.rules.engine.RulesLanguage;
  31. import com.liferay.portal.rules.engine.RulesResourceRetriever;
  32. import com.liferay.portal.rules.engine.sample.web.internal.constants.SampleDroolsPortletKeys;
  33. import javax.portlet.ActionRequest;
  34. import javax.portlet.ActionResponse;
  35. import javax.portlet.PortletConfig;
  36. import javax.portlet.PortletPreferences;
  37. import javax.servlet.ServletContext;
  38. import javax.servlet.http.HttpServletRequest;
  39. import org.osgi.service.component.annotations.Component;
  40. import org.osgi.service.component.annotations.Reference;
  41. /**
  42. * @author Michael C. Han
  43. */
  44. @Component(
  45. immediate = true,
  46. property = "javax.portlet.name=" + SampleDroolsPortletKeys.SAMPLE_DROOLS,
  47. service = ConfigurationAction.class
  48. )
  49. public class SampleDroolsConfigurationAction
  50. extends DefaultConfigurationAction {
  51. @Override
  52. public String getJspPath(HttpServletRequest httpServletRequest) {
  53. return "/configuration.jsp";
  54. }
  55. @Override
  56. public void processAction(
  57. PortletConfig portletConfig, ActionRequest actionRequest,
  58. ActionResponse actionResponse)
  59. throws Exception {
  60. String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
  61. if (!cmd.equals(Constants.UPDATE)) {
  62. return;
  63. }
  64. PortletPreferences preferences = actionRequest.getPreferences();
  65. _updatePreferences(actionRequest, preferences);
  66. if (SessionErrors.isEmpty(actionRequest)) {
  67. preferences.store();
  68. SessionMessages.add(
  69. actionRequest,
  70. _portal.getPortletId(actionRequest) +
  71. SessionMessages.KEY_SUFFIX_UPDATED_CONFIGURATION);
  72. }
  73. }
  74. @Override
  75. @Reference(
  76. target = "(osgi.web.symbolicname=com.liferay.portal.rules.engine.sample.web)",
  77. unbind = "-"
  78. )
  79. public void setServletContext(ServletContext servletContext) {
  80. super.setServletContext(servletContext);
  81. }
  82. private void _updatePreferences(
  83. ActionRequest actionRequest, PortletPreferences preferences)
  84. throws Exception {
  85. String domainName = ParamUtil.getString(actionRequest, "domainName");
  86. String rules = ParamUtil.getString(actionRequest, "rules");
  87. long[] classNameIds = StringUtil.split(
  88. ParamUtil.getString(actionRequest, "classNameIds"), 0L);
  89. if (Validator.isNull(domainName)) {
  90. SessionErrors.add(actionRequest, "domainName");
  91. }
  92. else if (Validator.isNull(rules)) {
  93. SessionErrors.add(actionRequest, "rules");
  94. }
  95. else if (classNameIds.length == 0) {
  96. SessionErrors.add(actionRequest, "classNameIds");
  97. }
  98. else {
  99. RulesResourceRetriever rulesResourceRetriever =
  100. new RulesResourceRetriever(
  101. new StringResourceRetriever(rules),
  102. String.valueOf(RulesLanguage.DROOLS_RULE_LANGUAGE));
  103. try {
  104. _rulesEngine.update(domainName, rulesResourceRetriever);
  105. }
  106. catch (RulesEngineException rulesEngineException) {
  107. _log.error(rulesEngineException);
  108. SessionErrors.add(actionRequest, "rulesEngineException");
  109. }
  110. }
  111. if (SessionErrors.isEmpty(actionRequest)) {
  112. preferences.setValue("rules", rules);
  113. preferences.setValue("domain-name", domainName);
  114. String userCustomAttributeNames = ParamUtil.getString(
  115. actionRequest, "userCustomAttributeNames");
  116. preferences.setValue(
  117. "user-custom-attribute-names", userCustomAttributeNames);
  118. preferences.setValues(
  119. "class-name-ids", ArrayUtil.toStringArray(classNameIds));
  120. }
  121. }
  122. private static final Log _log = LogFactoryUtil.getLog(
  123. SampleDroolsConfigurationAction.class);
  124. @Reference
  125. private Portal _portal;
  126. @Reference
  127. private RulesEngine _rulesEngine;
  128. }