1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.exoplatform.services.cms.i18n.impl;
18
19 import java.util.ArrayList;
20 import java.util.Date;
21 import java.util.Iterator;
22 import java.util.List;
23 import java.util.regex.Matcher;
24 import java.util.regex.Pattern;
25
26 import javax.jcr.NoSuchWorkspaceException;
27 import javax.jcr.Node;
28 import javax.jcr.PathNotFoundException;
29 import javax.jcr.RepositoryException;
30 import javax.jcr.Session;
31
32 import org.apache.commons.lang.StringUtils;
33 import org.exoplatform.container.configuration.ConfigurationManager;
34 import org.exoplatform.container.xml.InitParams;
35 import org.exoplatform.container.xml.ObjectParameter;
36 import org.exoplatform.services.cms.i18n.MultiLanguageService;
37 import org.exoplatform.services.deployment.plugins.TranslationDeploymentDescriptor;
38 import org.exoplatform.services.jcr.RepositoryService;
39 import org.exoplatform.services.jcr.ext.common.SessionProvider;
40 import org.exoplatform.services.log.ExoLogger;
41 import org.exoplatform.services.log.Log;
42 import org.exoplatform.services.wcm.portal.artifacts.CreatePortalPlugin;
43 import org.exoplatform.services.wcm.utils.WCMCoreUtils;
44
45
46
47
48
49
50
51 public class AddTranslationPlugin extends CreatePortalPlugin {
52
53
54
55
56 private static final Log LOG = ExoLogger.getLogger(AddTranslationPlugin.class.getName());
57
58 private static final Pattern FILE_EXPLORER_URL_SYNTAX = Pattern.compile("([^:/]+):(/.*)");
59
60
61
62
63 private InitParams initParams;
64
65
66
67
68 private MultiLanguageService languageService;
69
70
71
72
73
74
75
76
77
78 public AddTranslationPlugin(InitParams initParams,
79 ConfigurationManager configurationManager,
80 RepositoryService repositoryService,
81 MultiLanguageService languageService) {
82 super(initParams, configurationManager, repositoryService);
83 this.initParams = initParams;
84 this.languageService = languageService;
85 }
86
87 @SuppressWarnings("rawtypes")
88 @Override
89 public void deployToPortal(SessionProvider sessionProvider, String portalName) throws Exception {
90 Iterator iterator = initParams.getObjectParamIterator();
91 TranslationDeploymentDescriptor translationDescriptor = null;
92 while (iterator.hasNext()) {
93 ObjectParameter objectParameter = (ObjectParameter) iterator.next();
94 translationDescriptor = (TranslationDeploymentDescriptor) objectParameter.getObject();
95 List<String> translationPaths = translationDescriptor.getTranslationNodePaths();
96 boolean isOverrideExistence = translationDescriptor.isOverrideExistence();
97
98
99 List<String> translationRealPaths = new ArrayList<String>();
100 if (portalName != null && portalName.length() > 0) {
101 for (String path : translationPaths) {
102 translationRealPaths.add(StringUtils.replace(path, "{portalName}", portalName));
103 }
104 }
105
106
107 int numOfTrans = translationRealPaths.size();
108 for (int i = 0; i < numOfTrans; i++) {
109 Node currNode = this.getNodeByPath(translationRealPaths.get(i));
110 if (currNode == null) continue;
111 for (int j = i + 1; j < numOfTrans; j++) {
112 Node targetTranslationNode = this.getNodeByPath(translationRealPaths.get(j));
113 if (targetTranslationNode == null) continue;
114 try {
115 languageService.addLinkedLanguage(currNode, targetTranslationNode, isOverrideExistence);
116 languageService.addLinkedLanguage(targetTranslationNode, currNode, isOverrideExistence);
117 } catch (Exception e) {
118 if (LOG.isErrorEnabled()) {
119 LOG.error("Add translation " + translationRealPaths.get(j) + " for " + translationRealPaths.get(i) + " FAILED at "
120 + new Date().toString() + "\n", e);
121 }
122 }
123 }
124 }
125 }
126 }
127
128
129
130
131
132
133
134
135
136 private Node getNodeByPath(String nodePath) throws RepositoryException
137 {
138 try
139 {
140 Matcher matcher = FILE_EXPLORER_URL_SYNTAX.matcher(nodePath);
141 if (!matcher.find()) return null;
142 String wsName = matcher.group(1);
143 nodePath = matcher.group(2);
144 Session session = WCMCoreUtils.getSystemSessionProvider().getSession(wsName, WCMCoreUtils.getRepository());
145 return (Node)session.getItem(nodePath);
146 }
147 catch (PathNotFoundException e)
148 {
149 return null;
150 }
151 catch (NoSuchWorkspaceException e)
152 {
153 return null;
154 }
155 }
156 }