PageRenderTime 28ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/src/main/java/com/atlassian/crowd/integration/rest/util/SearchRestrictionEntityTranslator.java

https://bitbucket.org/atlassian/crowd-rest-client
Java | 340 lines | 197 code | 27 blank | 116 comment | 21 complexity | b2b19d1765f9c9f01a6f9e25d1260cd6 MD5 | raw file
  1. /*
  2. * Copyright © 2010 - 2015 Atlassian Corporation Pty Ltd.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * https://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package com.atlassian.crowd.integration.rest.util;
  17. import java.text.ParseException;
  18. import java.text.SimpleDateFormat;
  19. import java.util.ArrayList;
  20. import java.util.Collection;
  21. import java.util.Date;
  22. import com.atlassian.crowd.embedded.api.SearchRestriction;
  23. import com.atlassian.crowd.integration.rest.entity.BooleanRestrictionEntity;
  24. import com.atlassian.crowd.integration.rest.entity.NullRestrictionEntity;
  25. import com.atlassian.crowd.integration.rest.entity.PropertyEntity;
  26. import com.atlassian.crowd.integration.rest.entity.PropertyRestrictionEntity;
  27. import com.atlassian.crowd.integration.rest.entity.SearchRestrictionEntity;
  28. import com.atlassian.crowd.search.builder.Combine;
  29. import com.atlassian.crowd.search.query.entity.restriction.BooleanRestriction;
  30. import com.atlassian.crowd.search.query.entity.restriction.MatchMode;
  31. import com.atlassian.crowd.search.query.entity.restriction.NullRestriction;
  32. import com.atlassian.crowd.search.query.entity.restriction.NullRestrictionImpl;
  33. import com.atlassian.crowd.search.query.entity.restriction.Property;
  34. import com.atlassian.crowd.search.query.entity.restriction.PropertyImpl;
  35. import com.atlassian.crowd.search.query.entity.restriction.PropertyRestriction;
  36. import com.atlassian.crowd.search.query.entity.restriction.TermRestriction;
  37. /**
  38. * Utility class to convert from a SearchRestriction interface to one of SearchRestrictionEntity classes.
  39. */
  40. public class SearchRestrictionEntityTranslator
  41. {
  42. /**
  43. * Represents a supported value type.
  44. */
  45. public enum SupportedType
  46. {
  47. BOOLEAN(Boolean.class),
  48. DATE(Date.class),
  49. STRING(String.class);
  50. private final Class type;
  51. SupportedType(final Class type)
  52. {
  53. this.type = type;
  54. }
  55. public Class getType()
  56. {
  57. return type;
  58. }
  59. /**
  60. * Returns the SupportedType from the specified name. The matching is case-insensitive.
  61. *
  62. * @param supportedType Name of the enum constant.
  63. * @return SupportedType enum constant.
  64. */
  65. public static SupportedType of(final String supportedType)
  66. {
  67. return SupportedType.valueOf(supportedType.toUpperCase());
  68. }
  69. /**
  70. * Returns the SupportedType from the specified Class type.
  71. *
  72. * @param type Class of the supported type
  73. * @return SupportedType enum constant.
  74. */
  75. public static SupportedType of(final Class type)
  76. {
  77. for (SupportedType supportedType : SupportedType.values())
  78. {
  79. if (supportedType.getType().equals(type))
  80. {
  81. return supportedType;
  82. }
  83. }
  84. throw new IllegalArgumentException(type.getCanonicalName() + " is an unsupported type.");
  85. }
  86. }
  87. /**
  88. * The format used for times in the REST plugin. Conforms to ISO 8601. Format is also used in JIRA.
  89. */
  90. public static final String TIME_FORMAT = "yyyy-MM-dd'T'HH:mm:ss.SSSZ";
  91. /**
  92. * Converts from a BooleanRestriction to a BooleanRestrictionEntity.
  93. *
  94. * @param booleanRestriction BooleanRestriction
  95. * @return BooleanRestrictionEntity
  96. */
  97. public static BooleanRestrictionEntity toBooleanRestrictionEntity(final BooleanRestriction booleanRestriction)
  98. {
  99. Collection<SearchRestrictionEntity> restrictionEntities = new ArrayList<SearchRestrictionEntity>();
  100. for (SearchRestriction sr : booleanRestriction.getRestrictions())
  101. {
  102. restrictionEntities.add(toSearchRestrictionEntity(sr));
  103. }
  104. return new BooleanRestrictionEntity(booleanRestriction.getBooleanLogic().name(), restrictionEntities);
  105. }
  106. /**
  107. * Converts from a BooleanRestrictionEntity to a BooleanRestriction.
  108. *
  109. * @param booleanRestrictionEntity boolean restriction entity to convert from
  110. * @return BooleanRestriction
  111. * @throws IllegalArgumentException if no BooleanLogic enum constant could be found for {@link com.atlassian.crowd.integration.rest.entity.BooleanRestrictionEntity#getBooleanLogic()}.
  112. */
  113. public static BooleanRestriction toBooleanRestriction(final BooleanRestrictionEntity booleanRestrictionEntity)
  114. {
  115. final BooleanRestriction.BooleanLogic booleanLogic = BooleanRestriction.BooleanLogic.valueOf(booleanRestrictionEntity.getBooleanLogic().toUpperCase());
  116. final Collection<SearchRestriction> restrictions = new ArrayList<SearchRestriction>();
  117. for (SearchRestrictionEntity searchRestrictionEntity : booleanRestrictionEntity.getRestrictions())
  118. {
  119. restrictions.add(toSearchRestriction(searchRestrictionEntity));
  120. }
  121. switch (booleanLogic)
  122. {
  123. case AND:
  124. return Combine.allOf(restrictions);
  125. case OR:
  126. return Combine.anyOf(restrictions);
  127. default:
  128. throw new AssertionError("Unknown BooleanLogic type: " + booleanLogic);
  129. }
  130. }
  131. /**
  132. * Converts from a PropertyRestriction to a PropertyRestrictionEntity.
  133. *
  134. * @param propertyRestriction PropertyRestriction
  135. * @return PropertyRestrictionEntity
  136. */
  137. public static PropertyRestrictionEntity toPropertyRestrictionEntity(final PropertyRestriction propertyRestriction)
  138. {
  139. PropertyEntity propertyEntity = toPropertyEntity(propertyRestriction.getProperty());
  140. String valueString;
  141. MatchMode mm = propertyRestriction.getMatchMode();
  142. if (mm == MatchMode.NULL)
  143. {
  144. valueString = null;
  145. }
  146. else
  147. {
  148. valueString = valueToString(propertyRestriction.getValue());
  149. }
  150. return new PropertyRestrictionEntity(propertyEntity, mm.name(), valueString);
  151. }
  152. /**
  153. * Converts from a PropertyRestrictionEntity to a PropertyRestriction.
  154. *
  155. * @param propertyRestrictionEntity property restriction entity to convert from
  156. * @return PropertyRestriction
  157. */
  158. public static PropertyRestriction toPropertyRestriction(final PropertyRestrictionEntity propertyRestrictionEntity)
  159. {
  160. final Property property = toProperty(propertyRestrictionEntity.getProperty());
  161. final MatchMode matchMode = MatchMode.valueOf(propertyRestrictionEntity.getMatchMode().toUpperCase());
  162. final SupportedType supportedType = SupportedType.of(property.getPropertyType());
  163. return new TermRestriction(property, matchMode, valueFromString(supportedType, propertyRestrictionEntity.getValue()));
  164. }
  165. /**
  166. * Converts from a Property to a PropertyEntity.
  167. *
  168. * @param property Property to convert from
  169. * @return PropertyEntity
  170. */
  171. public static PropertyEntity toPropertyEntity(final Property property)
  172. {
  173. final SupportedType supportedType = SupportedType.of(property.getPropertyType());
  174. return new PropertyEntity(property.getPropertyName(), supportedType.name());
  175. }
  176. /**
  177. * Converts from a PropertyEntity to a Property.
  178. *
  179. * @param propertyEntity PropertyEntity to convert from.
  180. * @return Property
  181. * @throws IllegalArgumentException if the property value type is unknown
  182. */
  183. public static Property toProperty(final PropertyEntity propertyEntity)
  184. {
  185. final String typeString = propertyEntity.getType();
  186. SupportedType supportedType = SupportedType.of(typeString);
  187. return new PropertyImpl(propertyEntity.getName(), supportedType.getType());
  188. }
  189. /**
  190. * Converts from a SearchRestriction a SearchRestrictionEntity.
  191. *
  192. * @param searchRestriction search restriction to convert
  193. * @return SearchRestrictionEntity
  194. */
  195. public static SearchRestrictionEntity toSearchRestrictionEntity(final SearchRestriction searchRestriction)
  196. {
  197. if (searchRestriction instanceof BooleanRestriction)
  198. {
  199. return toBooleanRestrictionEntity((BooleanRestriction) searchRestriction);
  200. }
  201. else if (searchRestriction instanceof PropertyRestriction)
  202. {
  203. return toPropertyRestrictionEntity((PropertyRestriction) searchRestriction);
  204. }
  205. else if (searchRestriction instanceof NullRestriction)
  206. {
  207. return NullRestrictionEntity.INSTANCE;
  208. }
  209. else
  210. {
  211. throw new IllegalArgumentException("Unknown search restriction type");
  212. }
  213. }
  214. /**
  215. * Converts from a SearchRestrictionEntity to a SearchRestriction.
  216. *
  217. * @param searchRestrictionEntity search restriction entity to convert from
  218. * @return SearchRestriction
  219. */
  220. public static SearchRestriction toSearchRestriction(final SearchRestrictionEntity searchRestrictionEntity)
  221. {
  222. if (searchRestrictionEntity instanceof BooleanRestrictionEntity)
  223. {
  224. return toBooleanRestriction((BooleanRestrictionEntity) searchRestrictionEntity);
  225. }
  226. else if (searchRestrictionEntity instanceof PropertyRestrictionEntity)
  227. {
  228. return toPropertyRestriction((PropertyRestrictionEntity) searchRestrictionEntity);
  229. }
  230. else if (searchRestrictionEntity instanceof NullRestrictionEntity)
  231. {
  232. return NullRestrictionImpl.INSTANCE;
  233. }
  234. else
  235. {
  236. throw new IllegalArgumentException("Unknown search restriction entity type");
  237. }
  238. }
  239. /**
  240. * Converts the value to a String.
  241. *
  242. * @param value value
  243. * @return String format of the value
  244. */
  245. public static String valueToString(final Object value)
  246. {
  247. if (value instanceof Enum)
  248. {
  249. return ((Enum) value).name();
  250. }
  251. else if (value instanceof Date)
  252. {
  253. return asTimeString((Date) value);
  254. }
  255. else
  256. {
  257. return value.toString();
  258. }
  259. }
  260. /**
  261. * Converts from a String to a value type.
  262. *
  263. * @param supportedType the supported type of the value
  264. * @param value value
  265. * @return value type
  266. */
  267. public static Object valueFromString(final SupportedType supportedType, final String value)
  268. {
  269. switch (supportedType)
  270. {
  271. case BOOLEAN:
  272. return Boolean.valueOf(value);
  273. case DATE:
  274. return fromTimeString(value);
  275. case STRING:
  276. return value;
  277. default:
  278. throw new AssertionError("Unknown supported type: " + supportedType);
  279. }
  280. }
  281. /**
  282. * Converts the given Date object to a String using. The string is in the format <pre>{@value #TIME_FORMAT}</pre>.
  283. *
  284. * @param date a Date
  285. * @return a String representation of the date and time
  286. * @see java.text.SimpleDateFormat
  287. */
  288. public static String asTimeString(final Date date)
  289. {
  290. return new SimpleDateFormat(TIME_FORMAT).format(date);
  291. }
  292. /**
  293. * Converts the given date and time String to a Date object. The time parameter is expected to be in the format
  294. * <pre>{@value #TIME_FORMAT}</pre>.
  295. *
  296. * @param time a String representation of a date and time
  297. * @return a Date
  298. * @throws RuntimeException if there is an error parsing the date
  299. * @throws IllegalArgumentException if the input string is not in the expected format
  300. * @see java.text.SimpleDateFormat
  301. */
  302. public static Date fromTimeString(final String time) throws IllegalArgumentException
  303. {
  304. try
  305. {
  306. return new SimpleDateFormat(TIME_FORMAT).parse(time);
  307. }
  308. catch (ParseException e)
  309. {
  310. throw new IllegalArgumentException("Error parsing time: " + time, e);
  311. }
  312. }
  313. }