PageRenderTime 24ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/portlets/opensocial-portlet/docroot/WEB-INF/src/com/liferay/opensocial/shindig/service/LiferayAppDataService.java

https://github.com/stevenjiancao/liferay-plugins
Java | 296 lines | 212 code | 66 blank | 18 comment | 15 complexity | 08345386b632bfd1885506431d5a649e MD5 | raw file
  1. /**
  2. * Copyright (c) 2000-2011 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.opensocial.shindig.service;
  15. import com.liferay.opensocial.shindig.util.ShindigUtil;
  16. import com.liferay.portal.kernel.log.Log;
  17. import com.liferay.portal.kernel.log.LogFactoryUtil;
  18. import com.liferay.portal.kernel.util.GetterUtil;
  19. import com.liferay.portal.kernel.util.StringPool;
  20. import com.liferay.portal.kernel.util.Validator;
  21. import com.liferay.portal.model.User;
  22. import com.liferay.portal.service.UserLocalServiceUtil;
  23. import com.liferay.portlet.expando.NoSuchTableException;
  24. import com.liferay.portlet.expando.model.ExpandoColumn;
  25. import com.liferay.portlet.expando.model.ExpandoColumnConstants;
  26. import com.liferay.portlet.expando.model.ExpandoTable;
  27. import com.liferay.portlet.expando.model.ExpandoValue;
  28. import com.liferay.portlet.expando.service.ExpandoColumnLocalServiceUtil;
  29. import com.liferay.portlet.expando.service.ExpandoTableLocalServiceUtil;
  30. import com.liferay.portlet.expando.service.ExpandoValueLocalServiceUtil;
  31. import java.util.HashMap;
  32. import java.util.LinkedHashSet;
  33. import java.util.List;
  34. import java.util.Map;
  35. import java.util.Set;
  36. import java.util.concurrent.Future;
  37. import javax.servlet.http.HttpServletResponse;
  38. import org.apache.shindig.auth.SecurityToken;
  39. import org.apache.shindig.common.util.ImmediateFuture;
  40. import org.apache.shindig.protocol.DataCollection;
  41. import org.apache.shindig.protocol.ProtocolException;
  42. import org.apache.shindig.social.opensocial.spi.AppDataService;
  43. import org.apache.shindig.social.opensocial.spi.GroupId;
  44. import org.apache.shindig.social.opensocial.spi.UserId;
  45. /**
  46. * @author Michael Young
  47. */
  48. public class LiferayAppDataService implements AppDataService {
  49. public Future<Void> deletePersonData(
  50. UserId userId, GroupId groupId, String appId, Set<String> fields,
  51. SecurityToken securityToken)
  52. throws ProtocolException {
  53. try {
  54. doDeletePersonData(userId, groupId, appId, fields, securityToken);
  55. return ImmediateFuture.newInstance(null);
  56. }
  57. catch (Exception e) {
  58. if (_log.isDebugEnabled()) {
  59. _log.debug(e, e);
  60. }
  61. throw new ProtocolException(
  62. HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e.getMessage(),
  63. e);
  64. }
  65. }
  66. public Future<DataCollection> getPersonData(
  67. Set<UserId> userIds, GroupId groupId, String appId,
  68. Set<String> fields, SecurityToken securityToken)
  69. throws ProtocolException {
  70. try {
  71. DataCollection dataCollection = doGetPersonData(
  72. userIds, groupId, appId, fields, securityToken);
  73. return ImmediateFuture.newInstance(dataCollection);
  74. }
  75. catch (Exception e) {
  76. if (_log.isDebugEnabled()) {
  77. _log.debug(e, e);
  78. }
  79. throw new ProtocolException(
  80. HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e.getMessage(),
  81. e);
  82. }
  83. }
  84. public Future<Void> updatePersonData(
  85. UserId userId, GroupId groupId, String appId, Set<String> fields,
  86. Map<String, String> values, SecurityToken securityToken)
  87. throws ProtocolException {
  88. try {
  89. doUpdatePersonData(
  90. userId, groupId, appId, fields, values, securityToken);
  91. return ImmediateFuture.newInstance(null);
  92. }
  93. catch (Exception e) {
  94. if (_log.isDebugEnabled()) {
  95. _log.debug(e, e);
  96. }
  97. throw new ProtocolException(
  98. HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e.getMessage(),
  99. e);
  100. }
  101. }
  102. protected void doDeletePersonData(
  103. UserId userId, GroupId groupId, String appId, Set<String> fields,
  104. SecurityToken securityToken)
  105. throws Exception {
  106. long companyId = getCompanyId(securityToken);
  107. long userIdLong = GetterUtil.getLong(userId.getUserId(securityToken));
  108. for (String field : fields) {
  109. ExpandoColumn expandoColumn = getExpandoColumn(
  110. companyId, getColumnName(appId, field));
  111. ExpandoValueLocalServiceUtil.deleteValue(
  112. companyId, User.class.getName(),
  113. ShindigUtil.getTableOpenSocial(), expandoColumn.getName(),
  114. userIdLong);
  115. }
  116. return;
  117. }
  118. protected DataCollection doGetPersonData(
  119. Set<UserId> userIds, GroupId groupId, String appId,
  120. Set<String> fields, SecurityToken securityToken)
  121. throws Exception {
  122. long companyId = getCompanyId(securityToken);
  123. Map<String, Map<String, String>> peopleAppData =
  124. new HashMap<String, Map<String, String>>();
  125. List<ExpandoColumn> expandoColumns = getExpandoColumns(
  126. companyId, appId);
  127. if (expandoColumns == null) {
  128. return null;
  129. }
  130. if (fields.isEmpty()) {
  131. fields = new LinkedHashSet<String>();
  132. for (ExpandoColumn expandoColumn : expandoColumns) {
  133. fields.add(expandoColumn.getName());
  134. }
  135. }
  136. for (UserId userId : userIds) {
  137. String userIdString = userId.getUserId(securityToken);
  138. long userIdLong = GetterUtil.getLong(userIdString);
  139. Map<String, String> personAppData = new HashMap<String, String>();
  140. for (String field : fields) {
  141. String value = getExpandoValue(
  142. companyId, appId, userIdLong, getColumnName(appId, field));
  143. personAppData.put(field, value);
  144. }
  145. peopleAppData.put(userIdString, personAppData);
  146. }
  147. return new DataCollection(peopleAppData);
  148. }
  149. protected void doUpdatePersonData(UserId userId, GroupId groupId,
  150. String appId, Set<String> fields, Map<String, String> values,
  151. SecurityToken securityToken)
  152. throws Exception {
  153. long companyId = getCompanyId(securityToken);
  154. long userIdLong = GetterUtil.getLong(userId.getUserId(securityToken));
  155. for (String key : values.keySet()) {
  156. // Workaround for a Shindig bug that stores a Long in value instead
  157. // of the expected String so we cannot use generics here
  158. String value = String.valueOf(values.get(key));
  159. ExpandoColumn expandoColumn =
  160. getExpandoColumn(companyId, getColumnName(appId, key));
  161. ExpandoValueLocalServiceUtil.addValue(
  162. companyId, User.class.getName(),
  163. ShindigUtil.getTableOpenSocial(), expandoColumn.getName(),
  164. userIdLong, value);
  165. }
  166. return;
  167. }
  168. protected String getColumnName(String appId, String field) {
  169. if (Validator.isNotNull(appId)) {
  170. return appId.concat(field);
  171. }
  172. else {
  173. return field;
  174. }
  175. }
  176. protected long getCompanyId(SecurityToken securityToken) throws Exception {
  177. long userIdLong = GetterUtil.getLong(securityToken.getViewerId());
  178. User user = UserLocalServiceUtil.getUser(userIdLong);
  179. return user.getCompanyId();
  180. }
  181. protected ExpandoColumn getExpandoColumn(
  182. long companyId, String columnName)
  183. throws Exception {
  184. ExpandoTable expandoTable = null;
  185. try {
  186. expandoTable = ExpandoTableLocalServiceUtil.getTable(
  187. companyId, User.class.getName(),
  188. ShindigUtil.getTableOpenSocial());
  189. }
  190. catch (NoSuchTableException nste) {
  191. _log.error(nste, nste);
  192. }
  193. ExpandoColumn expandoColumn = ExpandoColumnLocalServiceUtil.getColumn(
  194. expandoTable.getTableId(), columnName);
  195. if (expandoColumn == null) {
  196. expandoColumn = ExpandoColumnLocalServiceUtil.addColumn(
  197. expandoTable.getTableId(), columnName,
  198. ExpandoColumnConstants.STRING);
  199. }
  200. return expandoColumn;
  201. }
  202. protected List<ExpandoColumn> getExpandoColumns(
  203. long companyId, String appId) {
  204. try {
  205. List<ExpandoColumn> expandoColumns =
  206. ExpandoColumnLocalServiceUtil.getColumns(
  207. companyId, User.class.getName(),
  208. ShindigUtil.getTableOpenSocial());
  209. return expandoColumns;
  210. }
  211. catch (Exception e) {
  212. return null;
  213. }
  214. }
  215. protected String getExpandoValue(
  216. long companyId, String appId, long userId, String columnName) {
  217. try {
  218. getExpandoColumn(companyId, columnName);
  219. ExpandoValue expandoValue = ExpandoValueLocalServiceUtil.getValue(
  220. companyId, User.class.getName(),
  221. ShindigUtil.getTableOpenSocial(), columnName, userId);
  222. return expandoValue.getData();
  223. }
  224. catch (Exception e) {
  225. return StringPool.BLANK;
  226. }
  227. }
  228. private static Log _log = LogFactoryUtil.getLog(
  229. LiferayAppDataService.class);
  230. }