PageRenderTime 27ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/portlets/twitter-portlet/docroot/WEB-INF/service/com/liferay/twitter/service/ClpSerializer.java

https://github.com/l15k4/liferay-plugins
Java | 344 lines | 223 code | 105 blank | 16 comment | 20 complexity | 669004d051bee6053aa88dbe6bfae9c7 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.twitter.service;
  15. import com.liferay.portal.kernel.log.Log;
  16. import com.liferay.portal.kernel.log.LogFactoryUtil;
  17. import com.liferay.portal.kernel.util.PropsUtil;
  18. import com.liferay.portal.kernel.util.Validator;
  19. import com.liferay.portal.model.BaseModel;
  20. import com.liferay.twitter.model.FeedClp;
  21. import java.lang.reflect.Method;
  22. import java.util.ArrayList;
  23. import java.util.Date;
  24. import java.util.List;
  25. /**
  26. * @author Brian Wing Shun Chan
  27. */
  28. public class ClpSerializer {
  29. public static String getServletContextName() {
  30. if (Validator.isNotNull(_servletContextName)) {
  31. return _servletContextName;
  32. }
  33. synchronized (ClpSerializer.class) {
  34. if (Validator.isNotNull(_servletContextName)) {
  35. return _servletContextName;
  36. }
  37. try {
  38. ClassLoader classLoader = ClpSerializer.class.getClassLoader();
  39. Class<?> portletPropsClass = classLoader.loadClass(
  40. "com.liferay.util.portlet.PortletProps");
  41. Method getMethod = portletPropsClass.getMethod("get",
  42. new Class<?>[] { String.class });
  43. String portletPropsServletContextName = (String)getMethod.invoke(null,
  44. "twitter-portlet-deployment-context");
  45. if (Validator.isNotNull(portletPropsServletContextName)) {
  46. _servletContextName = portletPropsServletContextName;
  47. }
  48. }
  49. catch (Throwable t) {
  50. if (_log.isInfoEnabled()) {
  51. _log.info(
  52. "Unable to locate deployment context from portlet properties");
  53. }
  54. }
  55. if (Validator.isNull(_servletContextName)) {
  56. try {
  57. String propsUtilServletContextName = PropsUtil.get(
  58. "twitter-portlet-deployment-context");
  59. if (Validator.isNotNull(propsUtilServletContextName)) {
  60. _servletContextName = propsUtilServletContextName;
  61. }
  62. }
  63. catch (Throwable t) {
  64. if (_log.isInfoEnabled()) {
  65. _log.info(
  66. "Unable to locate deployment context from portal properties");
  67. }
  68. }
  69. }
  70. if (Validator.isNull(_servletContextName)) {
  71. _servletContextName = "twitter-portlet";
  72. }
  73. return _servletContextName;
  74. }
  75. }
  76. public static void setClassLoader(ClassLoader classLoader) {
  77. _classLoader = classLoader;
  78. }
  79. public static Object translateInput(BaseModel<?> oldModel) {
  80. Class<?> oldModelClass = oldModel.getClass();
  81. String oldModelClassName = oldModelClass.getName();
  82. if (oldModelClassName.equals(FeedClp.class.getName())) {
  83. return translateInputFeed(oldModel);
  84. }
  85. return oldModel;
  86. }
  87. public static Object translateInput(List<Object> oldList) {
  88. List<Object> newList = new ArrayList<Object>(oldList.size());
  89. for (int i = 0; i < oldList.size(); i++) {
  90. Object curObj = oldList.get(i);
  91. newList.add(translateInput(curObj));
  92. }
  93. return newList;
  94. }
  95. public static Object translateInputFeed(BaseModel<?> oldModel) {
  96. FeedClp oldCplModel = (FeedClp)oldModel;
  97. Thread currentThread = Thread.currentThread();
  98. ClassLoader contextClassLoader = currentThread.getContextClassLoader();
  99. try {
  100. currentThread.setContextClassLoader(_classLoader);
  101. try {
  102. Class<?> newModelClass = Class.forName("com.liferay.twitter.model.impl.FeedImpl",
  103. true, _classLoader);
  104. Object newModel = newModelClass.newInstance();
  105. Method method0 = newModelClass.getMethod("setFeedId",
  106. new Class[] { Long.TYPE });
  107. Long value0 = new Long(oldCplModel.getFeedId());
  108. method0.invoke(newModel, value0);
  109. Method method1 = newModelClass.getMethod("setCompanyId",
  110. new Class[] { Long.TYPE });
  111. Long value1 = new Long(oldCplModel.getCompanyId());
  112. method1.invoke(newModel, value1);
  113. Method method2 = newModelClass.getMethod("setUserId",
  114. new Class[] { Long.TYPE });
  115. Long value2 = new Long(oldCplModel.getUserId());
  116. method2.invoke(newModel, value2);
  117. Method method3 = newModelClass.getMethod("setUserName",
  118. new Class[] { String.class });
  119. String value3 = oldCplModel.getUserName();
  120. method3.invoke(newModel, value3);
  121. Method method4 = newModelClass.getMethod("setCreateDate",
  122. new Class[] { Date.class });
  123. Date value4 = oldCplModel.getCreateDate();
  124. method4.invoke(newModel, value4);
  125. Method method5 = newModelClass.getMethod("setModifiedDate",
  126. new Class[] { Date.class });
  127. Date value5 = oldCplModel.getModifiedDate();
  128. method5.invoke(newModel, value5);
  129. Method method6 = newModelClass.getMethod("setTwitterUserId",
  130. new Class[] { Long.TYPE });
  131. Long value6 = new Long(oldCplModel.getTwitterUserId());
  132. method6.invoke(newModel, value6);
  133. Method method7 = newModelClass.getMethod("setTwitterScreenName",
  134. new Class[] { String.class });
  135. String value7 = oldCplModel.getTwitterScreenName();
  136. method7.invoke(newModel, value7);
  137. Method method8 = newModelClass.getMethod("setLastStatusId",
  138. new Class[] { Long.TYPE });
  139. Long value8 = new Long(oldCplModel.getLastStatusId());
  140. method8.invoke(newModel, value8);
  141. return newModel;
  142. }
  143. catch (Exception e) {
  144. _log.error(e, e);
  145. }
  146. }
  147. finally {
  148. currentThread.setContextClassLoader(contextClassLoader);
  149. }
  150. return oldModel;
  151. }
  152. public static Object translateInput(Object obj) {
  153. if (obj instanceof BaseModel<?>) {
  154. return translateInput((BaseModel<?>)obj);
  155. }
  156. else if (obj instanceof List<?>) {
  157. return translateInput((List<Object>)obj);
  158. }
  159. else {
  160. return obj;
  161. }
  162. }
  163. public static Object translateOutput(BaseModel<?> oldModel) {
  164. Class<?> oldModelClass = oldModel.getClass();
  165. String oldModelClassName = oldModelClass.getName();
  166. if (oldModelClassName.equals("com.liferay.twitter.model.impl.FeedImpl")) {
  167. return translateOutputFeed(oldModel);
  168. }
  169. return oldModel;
  170. }
  171. public static Object translateOutput(List<Object> oldList) {
  172. List<Object> newList = new ArrayList<Object>(oldList.size());
  173. for (int i = 0; i < oldList.size(); i++) {
  174. Object curObj = oldList.get(i);
  175. newList.add(translateOutput(curObj));
  176. }
  177. return newList;
  178. }
  179. public static Object translateOutput(Object obj) {
  180. if (obj instanceof BaseModel<?>) {
  181. return translateOutput((BaseModel<?>)obj);
  182. }
  183. else if (obj instanceof List<?>) {
  184. return translateOutput((List<Object>)obj);
  185. }
  186. else {
  187. return obj;
  188. }
  189. }
  190. public static Object translateOutputFeed(BaseModel<?> oldModel) {
  191. Thread currentThread = Thread.currentThread();
  192. ClassLoader contextClassLoader = currentThread.getContextClassLoader();
  193. try {
  194. currentThread.setContextClassLoader(_classLoader);
  195. try {
  196. FeedClp newModel = new FeedClp();
  197. Class<?> oldModelClass = oldModel.getClass();
  198. Method method0 = oldModelClass.getMethod("getFeedId");
  199. Long value0 = (Long)method0.invoke(oldModel, (Object[])null);
  200. newModel.setFeedId(value0);
  201. Method method1 = oldModelClass.getMethod("getCompanyId");
  202. Long value1 = (Long)method1.invoke(oldModel, (Object[])null);
  203. newModel.setCompanyId(value1);
  204. Method method2 = oldModelClass.getMethod("getUserId");
  205. Long value2 = (Long)method2.invoke(oldModel, (Object[])null);
  206. newModel.setUserId(value2);
  207. Method method3 = oldModelClass.getMethod("getUserName");
  208. String value3 = (String)method3.invoke(oldModel, (Object[])null);
  209. newModel.setUserName(value3);
  210. Method method4 = oldModelClass.getMethod("getCreateDate");
  211. Date value4 = (Date)method4.invoke(oldModel, (Object[])null);
  212. newModel.setCreateDate(value4);
  213. Method method5 = oldModelClass.getMethod("getModifiedDate");
  214. Date value5 = (Date)method5.invoke(oldModel, (Object[])null);
  215. newModel.setModifiedDate(value5);
  216. Method method6 = oldModelClass.getMethod("getTwitterUserId");
  217. Long value6 = (Long)method6.invoke(oldModel, (Object[])null);
  218. newModel.setTwitterUserId(value6);
  219. Method method7 = oldModelClass.getMethod("getTwitterScreenName");
  220. String value7 = (String)method7.invoke(oldModel, (Object[])null);
  221. newModel.setTwitterScreenName(value7);
  222. Method method8 = oldModelClass.getMethod("getLastStatusId");
  223. Long value8 = (Long)method8.invoke(oldModel, (Object[])null);
  224. newModel.setLastStatusId(value8);
  225. return newModel;
  226. }
  227. catch (Exception e) {
  228. _log.error(e, e);
  229. }
  230. }
  231. finally {
  232. currentThread.setContextClassLoader(contextClassLoader);
  233. }
  234. return oldModel;
  235. }
  236. private static Log _log = LogFactoryUtil.getLog(ClpSerializer.class);
  237. private static ClassLoader _classLoader;
  238. private static String _servletContextName;
  239. }