/portlets/wsrp-portlet/docroot/WEB-INF/src/com/liferay/wsrp/service/impl/WSRPProducerLocalServiceImpl.java

https://github.com/kevincho/liferay-plugins · Java · 255 lines · 164 code · 68 blank · 23 comment · 6 complexity · 6c6e92fa0aef521d64de2e6107e5270b MD5 · raw file

  1. /**
  2. * Copyright (c) 2000-2012 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.wsrp.service.impl;
  15. import com.liferay.counter.service.CounterLocalServiceUtil;
  16. import com.liferay.portal.kernel.exception.PortalException;
  17. import com.liferay.portal.kernel.exception.SystemException;
  18. import com.liferay.portal.kernel.util.StringPool;
  19. import com.liferay.portal.kernel.util.StringUtil;
  20. import com.liferay.portal.kernel.util.Validator;
  21. import com.liferay.portal.model.Group;
  22. import com.liferay.portal.model.GroupConstants;
  23. import com.liferay.portal.model.LayoutConstants;
  24. import com.liferay.portal.model.Portlet;
  25. import com.liferay.portal.model.PortletConstants;
  26. import com.liferay.portal.model.User;
  27. import com.liferay.portal.service.PortletLocalServiceUtil;
  28. import com.liferay.portal.service.ServiceContext;
  29. import com.liferay.util.PwdGenerator;
  30. import com.liferay.wsrp.NoSuchProducerException;
  31. import com.liferay.wsrp.WSRPProducerNameException;
  32. import com.liferay.wsrp.model.WSRPProducer;
  33. import com.liferay.wsrp.service.base.WSRPProducerLocalServiceBaseImpl;
  34. import java.util.Date;
  35. import java.util.LinkedHashMap;
  36. import java.util.List;
  37. /**
  38. * @author Brian Wing Shun Chan
  39. */
  40. public class WSRPProducerLocalServiceImpl
  41. extends WSRPProducerLocalServiceBaseImpl {
  42. public WSRPProducer addWSRPProducer(
  43. long userId, String name, String version, String portletIds,
  44. ServiceContext serviceContext)
  45. throws PortalException, SystemException {
  46. Group group = addGroup(userId, name);
  47. return addWSRPProducer(
  48. userId, group.getGroupId(), name, version, portletIds,
  49. serviceContext);
  50. }
  51. public WSRPProducer addWSRPProducer(
  52. long userId, long groupId, String name, String version,
  53. String portletIds, ServiceContext serviceContext)
  54. throws PortalException, SystemException {
  55. User user = userPersistence.findByPrimaryKey(userId);
  56. portletIds = transformPortletIds(portletIds);
  57. Date now = new Date();
  58. validate(name);
  59. long wsrpProducerId = CounterLocalServiceUtil.increment();
  60. WSRPProducer wsrpProducer = wsrpProducerPersistence.create(
  61. wsrpProducerId);
  62. wsrpProducer.setUuid(serviceContext.getUuid());
  63. wsrpProducer.setGroupId(groupId);
  64. wsrpProducer.setCompanyId(user.getCompanyId());
  65. wsrpProducer.setCreateDate(now);
  66. wsrpProducer.setModifiedDate(now);
  67. wsrpProducer.setName(name);
  68. wsrpProducer.setVersion(version);
  69. wsrpProducer.setPortletIds(portletIds);
  70. wsrpProducerPersistence.update(wsrpProducer, false);
  71. return wsrpProducer;
  72. }
  73. @Override
  74. public void deleteWSRPProducer(long wsrpProducerId)
  75. throws PortalException, SystemException {
  76. WSRPProducer wsrpProducer = wsrpProducerPersistence.findByPrimaryKey(
  77. wsrpProducerId);
  78. deleteWSRPProducer(wsrpProducer);
  79. }
  80. @Override
  81. public void deleteWSRPProducer(WSRPProducer wsrpProducer)
  82. throws PortalException, SystemException {
  83. // WSRP producer
  84. wsrpProducerPersistence.remove(wsrpProducer);
  85. // Group
  86. groupLocalService.deleteGroup(wsrpProducer.getGroupId());
  87. }
  88. public WSRPProducer getWSRPProducer(String wsrpProducerUuid)
  89. throws PortalException, SystemException {
  90. List<WSRPProducer> wsrpProducers = wsrpProducerPersistence.findByUuid(
  91. wsrpProducerUuid);
  92. if (wsrpProducers.isEmpty()) {
  93. throw new NoSuchProducerException(
  94. "No WSRP producer exists with uuid " + wsrpProducerUuid);
  95. }
  96. return wsrpProducers.get(0);
  97. }
  98. public List<WSRPProducer> getWSRPProducers(
  99. long companyId, int start, int end)
  100. throws SystemException {
  101. return wsrpProducerPersistence.findByCompanyId(companyId, start, end);
  102. }
  103. public int getWSRPProducersCount(long companyId) throws SystemException {
  104. return wsrpProducerPersistence.countByCompanyId(companyId);
  105. }
  106. public WSRPProducer updateWSRPProducer(
  107. long wsrpProducerId, String name, String version, String portletIds)
  108. throws PortalException, SystemException {
  109. // WSRP producer
  110. portletIds = transformPortletIds(portletIds);
  111. validate(name);
  112. WSRPProducer wsrpProducer = wsrpProducerPersistence.findByPrimaryKey(
  113. wsrpProducerId);
  114. wsrpProducer.setModifiedDate(new Date());
  115. wsrpProducer.setName(name);
  116. wsrpProducer.setVersion(version);
  117. wsrpProducer.setPortletIds(portletIds);
  118. wsrpProducerPersistence.update(wsrpProducer, false);
  119. // Group
  120. updateGroup(wsrpProducer, name);
  121. return wsrpProducer;
  122. }
  123. protected Group addGroup(long userId, String name)
  124. throws PortalException, SystemException {
  125. User user = userPersistence.findByPrimaryKey(userId);
  126. name = getGroupName(name);
  127. LinkedHashMap<String, Object> params =
  128. new LinkedHashMap<String, Object>();
  129. int type = GroupConstants.TYPE_SITE_SYSTEM;
  130. params.put("type", type);
  131. List<Group> groups = groupLocalService.search(
  132. user.getCompanyId(), name, null, params, 0, 1);
  133. if (!groups.isEmpty()) {
  134. return groups.get(0);
  135. }
  136. Group group = groupLocalService.addGroup(
  137. user.getUserId(), null, 0, 0, name, null, type, null, true, true,
  138. null);
  139. layoutLocalService.addLayout(
  140. user.getUserId(), group.getGroupId(), false,
  141. LayoutConstants.DEFAULT_PARENT_LAYOUT_ID, "Portlets", null, null,
  142. LayoutConstants.TYPE_PORTLET, false, "/portlets",
  143. new ServiceContext());
  144. return group;
  145. }
  146. /**
  147. * @see com.liferay.portal.model.impl.LayoutTypePortletImpl
  148. */
  149. protected String getFullInstanceSeparator() {
  150. String instanceId = PwdGenerator.getPassword(
  151. PwdGenerator.KEY1 + PwdGenerator.KEY2 + PwdGenerator.KEY3, 4);
  152. return PortletConstants.INSTANCE_SEPARATOR + instanceId;
  153. }
  154. protected String getGroupName(String name) {
  155. return _WSRP_GROUP_NAME + StringPool.MINUS + name;
  156. }
  157. protected String transformPortletIds(String portletIds) {
  158. String[] portletIdsArray = StringUtil.split(portletIds);
  159. for (int i = 0; i < portletIdsArray.length; i++) {
  160. String portletId = portletIdsArray[i];
  161. if (portletId.contains(PortletConstants.INSTANCE_SEPARATOR)) {
  162. continue;
  163. }
  164. Portlet portlet = PortletLocalServiceUtil.getPortletById(portletId);
  165. if (!portlet.isInstanceable()) {
  166. continue;
  167. }
  168. String instanceId = PwdGenerator.getPassword(
  169. PwdGenerator.KEY1 + PwdGenerator.KEY2 + PwdGenerator.KEY3, 4);
  170. portletIdsArray[i] =
  171. portletId + PortletConstants.INSTANCE_SEPARATOR + instanceId;
  172. }
  173. return StringUtil.merge(portletIdsArray);
  174. }
  175. protected void updateGroup(WSRPProducer wsrpProducer, String name)
  176. throws PortalException, SystemException {
  177. Group group = groupLocalService.getGroup(wsrpProducer.getGroupId());
  178. group.setName(getGroupName(name));
  179. groupLocalService.updateGroup(group);
  180. }
  181. protected void validate(String name) throws PortalException {
  182. if (Validator.isNull(name)) {
  183. throw new WSRPProducerNameException();
  184. }
  185. }
  186. private static final String _WSRP_GROUP_NAME = "WSRP";
  187. }