PageRenderTime 26ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/src/main/java/org/andya/confluence/utils/ContentService.java

https://bitbucket.org/jwalton/metadata-confluence-plugin
Java | 280 lines | 220 code | 19 blank | 41 comment | 84 complexity | 738c33993df81e07f50adbdf3ee122c8 MD5 | raw file
  1. /*
  2. * Copyright (c) 2006, 2007 Andy Armstrong, Kelsey Grant and other contributors.
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * * Redistributions of source code must retain the above copyright notice,
  9. * this list of conditions and the following disclaimer.
  10. * * Redistributions in binary form must reproduce the above copyright notice,
  11. * this list of conditions and the following disclaimer in the documentation
  12. * and/or other materials provided with the distribution.
  13. * * The names of contributors may not
  14. * be used to endorse or promote products derived from this software without
  15. * specific prior written permission.
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  18. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  19. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  20. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
  21. * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  22. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  23. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
  24. * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  25. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  26. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  27. */
  28. package org.andya.confluence.utils;
  29. import com.atlassian.confluence.core.*;
  30. import com.atlassian.confluence.spaces.SpaceManager;
  31. import com.atlassian.confluence.spaces.SpaceDescription;
  32. import com.atlassian.confluence.spaces.Space;
  33. import com.atlassian.confluence.pages.Page;
  34. import com.atlassian.confluence.pages.BlogPost;
  35. import com.atlassian.confluence.pages.Comment;
  36. import com.atlassian.confluence.pages.Attachment;
  37. import com.atlassian.confluence.user.PersonalInformation;
  38. import com.atlassian.confluence.user.UserAccessor;
  39. import com.atlassian.confluence.user.AuthenticatedUserThreadLocal;
  40. import com.atlassian.user.UserManager;
  41. import com.atlassian.user.User;
  42. import com.atlassian.spring.container.ContainerManager;
  43. import org.andya.confluence.plugins.metadata.space.SpaceUtils;
  44. import org.andya.confluence.plugins.metadata.model.MetadataValue;
  45. import org.apache.commons.lang.StringUtils;
  46. import java.util.Date;
  47. /**
  48. * This provides a number of useful access routines for content.
  49. */
  50. public class ContentService {
  51. public static final String ATTACHMENT_KEY = "Attachment";
  52. public static final String AUTHOR_KEY = "Author";
  53. public static final String BLOG_POST_KEY = "Blog Post";
  54. public static final String COMMENT_KEY = "Comment";
  55. public static final String COMMENTS_KEY = "Comments";
  56. public static final String CREATOR_KEY = "Creator";
  57. public static final String DATE_KEY = "Date";
  58. public static final String DISPLAY_TITLE_KEY = "DisplayTitle";
  59. public static final String DOWNLOAD_KEY = "Download";
  60. public static final String EMAIL_KEY = "Email";
  61. public static final String EXCERPT_KEY = "Excerpt";
  62. public static final String EXCERPT_METADATA_KEY = "confluence.excerpt";
  63. public static final String FILE_SIZE_KEY = "File Size";
  64. public static final String FILE_TYPE_KEY = "File Type";
  65. public static final String LAST_CHANGED_BY_KEY = "Last Changed By";
  66. public static final String LAST_TIME_CHANGED_KEY = "Last Time Changed";
  67. public static final String PAGE_KEY = "Page";
  68. public static final String PARENT_KEY = "Parent";
  69. public static final String POSTED_KEY = "Posted";
  70. public static final String POSTER_KEY = "Poster";
  71. public static final String SIZE_KEY = "Size";
  72. public static final String SPACE_KEY = "Space";
  73. public static final String TIME_KEY = "Time";
  74. public static final String TIME_CREATED_KEY = "Time Created";
  75. public static final String TITLE_KEY = "Title";
  76. public static final String TYPE_KEY = "Type";
  77. public static final String USER_KEY = "User";
  78. public static final String VERSION_KEY = "Version";
  79. private final ContentPropertyManager contentPropertyManager;
  80. private final SpaceManager spaceManager;
  81. private final UserManager userManager;
  82. private final FormatSettingsManager formatSettingsManager;
  83. public ContentService(ContentPropertyManager contentPropertyManager,
  84. SpaceManager spaceManager, UserManager userManager,
  85. FormatSettingsManager formatSettingsManager) {
  86. this.contentPropertyManager = contentPropertyManager;
  87. this.spaceManager = spaceManager;
  88. this.userManager = userManager;
  89. this.formatSettingsManager = formatSettingsManager;
  90. }
  91. public ContentPropertyManager getContentPropertyManager() {
  92. return contentPropertyManager;
  93. }
  94. public SpaceManager getSpaceManager() {
  95. return spaceManager;
  96. }
  97. public UserManager getUserManager() {
  98. return userManager;
  99. }
  100. public FormatSettingsManager getFormatSettingsManager() {
  101. return formatSettingsManager;
  102. }
  103. /**
  104. * Returns a named Confluence value from the specified content entity
  105. * object.
  106. * <p/>
  107. * The following values are supported: Title - a link to the page Page - a
  108. * synonym for title Excerpt - shows the page's excerpt Space - shows the
  109. * page's space key
  110. */
  111. public Object getContentValue(ConfluenceEntityObject ceo, String valueName) {
  112. ContentPropertyManager contentPropertyManager = getContentPropertyManager();
  113. UserManager userManager = getUserManager();
  114. DateFormatter dateFormatter = getDateFormatter();
  115. if (AUTHOR_KEY.equals(valueName) || CREATOR_KEY.equals(valueName)
  116. || POSTER_KEY.equals(valueName))
  117. return ContentUtils.getUserLink(ceo.getCreatorName());
  118. else if (LAST_CHANGED_BY_KEY.equals(valueName))
  119. return ContentUtils.getUserLink(ceo.getLastModifierName());
  120. else if (LAST_TIME_CHANGED_KEY.equals(valueName))
  121. return ceo.getLastModificationDate();
  122. else if (DATE_KEY.equals(valueName)) {
  123. Date date = ceo.getLastModificationDate();
  124. return new MetadataValue(valueName, date, dateFormatter
  125. .format(date));
  126. } else if (TIME_KEY.equals(valueName)) {
  127. Date date = ceo.getLastModificationDate();
  128. return new MetadataValue(valueName, date, dateFormatter
  129. .formatTime(date));
  130. } else if (TIME_CREATED_KEY.equals(valueName)
  131. || POSTED_KEY.equals(valueName))
  132. return ceo.getCreationDate();
  133. if (ceo instanceof ContentEntityObject) {
  134. ContentEntityObject c = (ContentEntityObject) ceo;
  135. if (EXCERPT_KEY.equals(valueName))
  136. return contentPropertyManager.getTextProperty(c,
  137. EXCERPT_METADATA_KEY);
  138. }
  139. if (ceo instanceof Page) {
  140. Page page = (Page) ceo;
  141. if (TITLE_KEY.equals(valueName) || PAGE_KEY.equals(valueName)) {
  142. return ContentUtils.getPageLink(page);
  143. } else if (DISPLAY_TITLE_KEY.equals(valueName)){
  144. return page.getTitle();
  145. } else if (SPACE_KEY.equals(valueName)) {
  146. Space space = page.getSpace();
  147. return ContentUtils.getSpaceLink(space);
  148. } else if (PARENT_KEY.equals(valueName)) {
  149. Page parent = page.getParent();
  150. if (parent != null)
  151. return ContentUtils.getPageLink(parent);
  152. }
  153. } else if (ceo instanceof BlogPost) {
  154. BlogPost blogPost = (BlogPost) ceo;
  155. if (TITLE_KEY.equals(valueName) || BLOG_POST_KEY.equals(valueName)) {
  156. return ContentUtils.getBlogPostLink(blogPost);
  157. } else if (DISPLAY_TITLE_KEY.equals(valueName)){
  158. return blogPost.getTitle();
  159. } else if (SPACE_KEY.equals(valueName)) {
  160. Space space = blogPost.getSpace();
  161. return ContentUtils.getSpaceLink(space);
  162. } else if (COMMENTS_KEY.equals(valueName)) {
  163. int count = blogPost.getComments().size();
  164. if (count == 0) {
  165. String url = blogPost.getUrlPath()
  166. + "?showComments=true#comments";
  167. return ContentUtils.getRelativeLink("0 comments", url);
  168. } else {
  169. Comment firstComment = (Comment) blogPost.getComments()
  170. .get(0);
  171. String title = count + " comment" + (count == 1 ? "" : "s");
  172. return ContentUtils.getCommentLink(title, firstComment);
  173. }
  174. }
  175. } else if (ceo instanceof SpaceDescription) {
  176. SpaceDescription spaceDescription = (SpaceDescription) ceo;
  177. Space space = spaceDescription.getSpace();
  178. if (SPACE_KEY.equals(valueName) || TITLE_KEY.equals(valueName)) {
  179. return ContentUtils.getSpaceLink(space);
  180. } else if (PARENT_KEY.equals(valueName)) {
  181. Space parentSpace = SpaceUtils.getParentSpace(this, space);
  182. return ContentUtils.getSpaceLink(parentSpace);
  183. } else if (USER_KEY.equals(valueName)) {
  184. return ContentUtils.getUserLink(ceo.getCreatorName());
  185. } else if (EMAIL_KEY.equals(valueName) && space.isPersonal()) {
  186. User user = SpaceUtils
  187. .getPersonalSpaceOwner(userManager, space);
  188. if (user != null)
  189. return ContentUtils.getEmailLink(user);
  190. } else if (LAST_TIME_CHANGED_KEY.equals(valueName)) {
  191. return space.getLastModificationDate();
  192. }
  193. } else if (ceo instanceof PersonalInformation) {
  194. PersonalInformation personalInformation = (PersonalInformation) ceo;
  195. if (USER_KEY.equals(valueName))
  196. return ContentUtils.getUserLink(personalInformation
  197. .getUsername());
  198. else if (EMAIL_KEY.equals(valueName)) {
  199. String email = personalInformation.getEmail();
  200. if (!StringUtils.isBlank(email))
  201. return ContentUtils.getEmailLink(email);
  202. }
  203. } else if (ceo instanceof Attachment) {
  204. Attachment attachment = (Attachment) ceo;
  205. Integer version = attachment.getAttachmentVersion();
  206. if (ATTACHMENT_KEY.equals(valueName))
  207. return ContentUtils.getAttachmentLink(attachment);
  208. else if (TITLE_KEY.equals(valueName)
  209. || COMMENT_KEY.equals(valueName))
  210. return ContentUtils.getAttachmentLink(attachment, attachment
  211. .getComment());
  212. else if (FILE_SIZE_KEY.equals(valueName)
  213. || SIZE_KEY.equals(valueName))
  214. return new MetadataValue(valueName, new Long(attachment
  215. .getFileSize()), attachment.getNiceFileSize());
  216. else if (FILE_TYPE_KEY.equals(valueName)
  217. || TYPE_KEY.equals(valueName))
  218. return attachment.getNiceType();
  219. else if (DOWNLOAD_KEY.equals(valueName))
  220. return ContentUtils.getAttachmentLink(attachment, attachment
  221. .getDisplayTitle());
  222. else if (VERSION_KEY.equals(valueName))
  223. return ContentUtils.getAttachmentLink(attachment, attachment
  224. .getDisplayTitle())
  225. + " (" + version + ")";
  226. }
  227. return null;
  228. }
  229. public MetadataValue createMetadataValue(String name, Object value) {
  230. return createMetadataValue(name, value, null);
  231. }
  232. public MetadataValue createMetadataValue(String name, Object value,
  233. Object defaultValue) {
  234. if (value == null) {
  235. if (defaultValue == null)
  236. return null;
  237. value = defaultValue;
  238. }
  239. return new MetadataValue(name, value, createWikiSnippet(value));
  240. }
  241. /**
  242. * Returns the Wiki snippet to use to represent the specified value.
  243. */
  244. public String createWikiSnippet(Object value) {
  245. if (value instanceof Date)
  246. return getDateForUser((Date) value);
  247. return value != null ? value.toString() : "";
  248. }
  249. public DateFormatter getDateFormatter() {
  250. UserAccessor userAccessor = (UserAccessor) ContainerManager
  251. .getComponent("userAccessor");
  252. User user = AuthenticatedUserThreadLocal.getUser();
  253. FormatSettingsManager formatSettingsManager = getFormatSettingsManager();
  254. return userAccessor.getConfluenceUserPreferences(user)
  255. .getDateFormatter(formatSettingsManager);
  256. }
  257. public String getDateForUser(Date date) {
  258. DateFormatter dateFormatter = getDateFormatter();
  259. return dateFormatter.formatDateTime(date);
  260. }
  261. }