PageRenderTime 123ms CodeModel.GetById 36ms RepoModel.GetById 1ms app.codeStats 0ms

/src/main/java/com/valiantys/confluence/plugin/web/CopySpaceAction.java

https://bitbucket.org/valiantys-dev/ccpsp
Java | 310 lines | 214 code | 59 blank | 37 comment | 41 complexity | 7215a41676062971e9c9e425360b5cdd MD5 | raw file
  1. package com.valiantys.confluence.plugin.web;
  2. import java.io.IOException;
  3. import java.util.ArrayList;
  4. import java.util.List;
  5. import java.util.Properties;
  6. import java.util.regex.Matcher;
  7. import java.util.regex.Pattern;
  8. import com.valiantys.confluence.plugin.service.CopyPartialSpaceService;
  9. import com.valiantys.confluence.plugin.utils.ICopySpaceKeys;
  10. import org.apache.log4j.Logger;
  11. import com.atlassian.confluence.pages.Page;
  12. import com.atlassian.confluence.pages.actions.AbstractPageAwareAction;
  13. import com.atlassian.confluence.spaces.Space;
  14. import com.atlassian.confluence.spaces.SpaceManager;
  15. import com.atlassian.plugin.PluginAccessor;
  16. import com.atlassian.plugin.PluginState;
  17. import com.opensymphony.util.TextUtils;
  18. public class CopySpaceAction extends AbstractPageAwareAction {
  19. /**
  20. * UID serial version generated.
  21. */
  22. private static final long serialVersionUID = 1L;
  23. /**
  24. * Logger.
  25. */
  26. private static final Logger LOG = Logger.getLogger(CopySpaceAction.class);
  27. /**
  28. * Plugin's properties
  29. */
  30. private Properties copySpaceProperties;
  31. /**
  32. * true if the space's key is automaticaly generated.
  33. */
  34. private boolean spaceKeyGenerated;
  35. /**
  36. * Name of the new Space.
  37. */
  38. private String newSpaceName;
  39. /**
  40. * Name of the new Space key pattern.
  41. */
  42. private String spaceKeyPattern;
  43. /**
  44. * Name of the new Space key pattern.
  45. */
  46. private String spaceKeyReplace;
  47. /**
  48. * Name of the new Space.
  49. */
  50. private String newSpaceKey;
  51. /**
  52. * Name of the new Space.
  53. */
  54. private List<String> errorMessage;
  55. /**
  56. * Manager de space.
  57. */
  58. private CopyPartialSpaceService particialCopySpaceManager;
  59. private boolean adHocWorkflowInstalled;
  60. private PluginAccessor pluginAccessor;
  61. private static final String AD_HOC_WORKFLOW_PLUGIN_KEY = "com.comalatech.workflow";
  62. private boolean checkPublishedActivated;
  63. /**
  64. *
  65. * @return
  66. * @throws Exception
  67. */
  68. public String doPrepare() throws Exception {
  69. spaceKeyGenerated = false;
  70. LOG.error("spaceKeyGenerated : " + spaceKeyGenerated);
  71. LOG.debug("spaceKeyGenerated : " + spaceKeyGenerated);
  72. if (copySpaceProperties == null) {
  73. init();
  74. }
  75. spaceKeyGenerated = Boolean.parseBoolean(copySpaceProperties
  76. .getProperty(ICopySpaceKeys.SPACE_KEY_AUTO_GENERATION));
  77. spaceKeyPattern = copySpaceProperties.getProperty(ICopySpaceKeys.SPACE_KEY_PATTERN);
  78. spaceKeyReplace = copySpaceProperties.getProperty(ICopySpaceKeys.SPACE_KEY_REPLACE);
  79. if (spaceKeyReplace != null && spaceKeyReplace.trim().length() == 0) {
  80. spaceKeyReplace = null;
  81. }
  82. LOG.debug("spaceKeyPattern : " + spaceKeyPattern);
  83. adHocWorkflowInstalled = checkIfAdHocWorkflowInstalled();
  84. return SUCCESS;
  85. }
  86. private boolean checkIfAdHocWorkflowInstalled() {
  87. return pluginAccessor.getPlugin(AD_HOC_WORKFLOW_PLUGIN_KEY) != null
  88. && pluginAccessor.getPlugin(AD_HOC_WORKFLOW_PLUGIN_KEY).getPluginState() == PluginState.ENABLED;
  89. }
  90. @Override
  91. public String execute() throws Exception {
  92. Page rootPage = (Page) getPage();
  93. if (copySpaceProperties == null) {
  94. init();
  95. }
  96. LOG.debug(" PROPERTY : " + copySpaceProperties.getProperty(ICopySpaceKeys.COPY_PROPERTY_PAGE));
  97. String propertyPageTitle = null;
  98. if (Boolean.parseBoolean(copySpaceProperties.getProperty(ICopySpaceKeys.COPY_PROPERTY_PAGE))) {
  99. LOG.debug(" PROPERTY : " + copySpaceProperties.getProperty(ICopySpaceKeys.COPY_PROPERTY_PAGE));
  100. propertyPageTitle = copySpaceProperties.getProperty(ICopySpaceKeys.PROPERTY_PAGE_TITLE);
  101. LOG.debug(ICopySpaceKeys.PROPERTY_PAGE_TITLE);
  102. LOG.debug(copySpaceProperties.getProperty(ICopySpaceKeys.PROPERTY_PAGE_TITLE));
  103. }
  104. LOG.debug("propertyPageTitle : " + propertyPageTitle);
  105. Space currentSpace = (Space) getSpace();
  106. Space newSpace = new Space();
  107. LOG.debug("newSpaceName : " + newSpaceName);
  108. LOG.debug("particialCopySpaceManager : " + particialCopySpaceManager);
  109. String newKey = generateSpaceKeyBySpaceName(newSpaceName, getSpaceKey());
  110. checkFieldsValidity(newSpaceName, newKey);
  111. if (errorMessage != null && errorMessage.size() > 0) {
  112. return INPUT;
  113. }
  114. // CCPS-4 The name of destination space must be the name of the source
  115. // plus the baseline name
  116. newSpace = particialCopySpaceManager.copySpace(getSpace(), newKey, newSpaceName, getRemoteUser());
  117. particialCopySpaceManager.populateSpace(currentSpace, newSpace, rootPage, propertyPageTitle, checkPublishedActivated);
  118. newSpaceKey = newSpace.getKey();
  119. LOG.debug("newSpace : " + newSpace);
  120. return SUCCESS;
  121. }
  122. private void checkFieldsValidity(String name, String key) {
  123. if (!TextUtils.stringSet(name)) {
  124. if (errorMessage == null) {
  125. errorMessage = new ArrayList<String>();
  126. }
  127. errorMessage.add(getText("space.name.not.specified"));
  128. }
  129. if (!Space.isValidGlobalSpaceKey(key)) {
  130. if (errorMessage == null) {
  131. errorMessage = new ArrayList<String>();
  132. }
  133. errorMessage.add(getText("space.custom.key.invalid"));
  134. }
  135. if (spaceManager.getSpace(key) != null) {
  136. if (errorMessage == null) {
  137. errorMessage = new ArrayList<String>();
  138. }
  139. errorMessage.add(getText("space.custom.key.exists"));
  140. }
  141. }
  142. private void init() {
  143. try {
  144. ClassLoader loader = CopySpaceAction.class.getClassLoader();
  145. LOG.debug("loader : " + loader);
  146. copySpaceProperties = new Properties();
  147. copySpaceProperties.load(loader.getResourceAsStream(ICopySpaceKeys.CONFIG_FILE));
  148. } catch (IOException e) {
  149. LOG.error("An error occurs during the loading of plugin property : " + e.getMessage());
  150. e.printStackTrace();
  151. }
  152. }
  153. protected String generateSpaceKeyBySpaceName(String nameSpace, String originalSpacekey) {
  154. LOG.debug("spaceKeyGenerated : " + spaceKeyGenerated);
  155. if (spaceKeyGenerated) {
  156. StringBuffer generatedKey = null;
  157. LOG.debug("spaceKeyPattern : " + spaceKeyPattern);
  158. if (copySpaceProperties == null) {
  159. init();
  160. }
  161. if (Boolean.valueOf(copySpaceProperties.getProperty(ICopySpaceKeys.SPACE_KEY_IS_PREFIXED))) {
  162. nameSpace = originalSpacekey + " " + nameSpace;
  163. }
  164. if (spaceKeyPattern != null) {
  165. nameSpace = nameSpace.toUpperCase();
  166. LOG.debug("nameSpace : " + nameSpace);
  167. generatedKey = new StringBuffer();
  168. if (spaceKeyReplace == null || spaceKeyReplace.trim().length() == 0) {
  169. Pattern p = Pattern.compile(spaceKeyPattern);
  170. Matcher m = p.matcher(nameSpace.toUpperCase());
  171. while (m.find()) {
  172. generatedKey.append(nameSpace.substring(m.start(), m.end()));
  173. }
  174. } else {
  175. int length = nameSpace.length();
  176. for (int i = 0; i < length; i++) {
  177. String s = nameSpace.substring(i, i + 1);
  178. if (Pattern.matches(spaceKeyPattern, s)) {
  179. generatedKey.append(s);
  180. } else {
  181. generatedKey.append(spaceKeyReplace);
  182. }
  183. }
  184. }
  185. }
  186. return generatedKey.toString();
  187. } else {
  188. return newSpaceKey;
  189. }
  190. }
  191. public boolean isSpaceKeyGenerated() {
  192. return spaceKeyGenerated;
  193. }
  194. public void setSpaceKeyGenerated(boolean spaceKeyGenerated) {
  195. this.spaceKeyGenerated = spaceKeyGenerated;
  196. }
  197. public String getNewSpaceName() {
  198. return newSpaceName;
  199. }
  200. public void setNewSpaceName(String newSpaceName) {
  201. this.newSpaceName = newSpaceName;
  202. }
  203. public String getNewSpaceKey() {
  204. return newSpaceKey;
  205. }
  206. public void setNewSpaceKey(String newSpaceKey) {
  207. this.newSpaceKey = newSpaceKey;
  208. }
  209. public void setSpaceManager(SpaceManager spaceManager) {
  210. this.spaceManager = spaceManager;
  211. }
  212. public void setParticialCopySpaceManager(CopyPartialSpaceService particialCopySpaceManager) {
  213. this.particialCopySpaceManager = particialCopySpaceManager;
  214. }
  215. public String getSpaceKeyPattern() {
  216. return spaceKeyPattern;
  217. }
  218. public void setSpaceKeyPattern(String spaceKeyPattern) {
  219. this.spaceKeyPattern = spaceKeyPattern;
  220. }
  221. public String getSpaceKeyReplace() {
  222. return spaceKeyReplace;
  223. }
  224. public void setSpaceKeyReplace(String spaceKeyReplace) {
  225. this.spaceKeyReplace = spaceKeyReplace;
  226. }
  227. public List<String> getErrorMessage() {
  228. return errorMessage;
  229. }
  230. public void setErrorMessage(List<String> errorMessage) {
  231. this.errorMessage = errorMessage;
  232. }
  233. public boolean isAdHocWorkflowInstalled() {
  234. return adHocWorkflowInstalled;
  235. }
  236. public void setAdHocWorkflowInstalled(boolean adHocWorkflowInstalled) {
  237. this.adHocWorkflowInstalled = adHocWorkflowInstalled;
  238. }
  239. public PluginAccessor getPluginAccessor() {
  240. return pluginAccessor;
  241. }
  242. public void setPluginAccessor(PluginAccessor pluginAccessor) {
  243. this.pluginAccessor = pluginAccessor;
  244. }
  245. public boolean isCheckPublishedActivated() {
  246. return checkPublishedActivated;
  247. }
  248. public void setCheckPublishedActivated(boolean checkPublishedActivated) {
  249. this.checkPublishedActivated = checkPublishedActivated;
  250. }
  251. }