View Javadoc
1   /*
2    * Copyright (C) 2003-2014 eXo Platform SAS.
3    *
4    * This program is free software: you can redistribute it and/or modify
5    * it under the terms of the GNU Affero General Public License as published by
6    * the Free Software Foundation, either version 3 of the License, or
7    * (at your option) any later version.
8    *
9    * This program is distributed in the hope that it will be useful,
10   * but WITHOUT ANY WARRANTY; without even the implied warranty of
11   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12   * GNU Affero General Public License for more details.
13   *
14   * You should have received a copy of the GNU Affero General Public License
15   * along with this program. If not, see <http://www.gnu.org/licenses/>.
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   * Created by The eXo Platform SAS
47   * Author : eXoPlatform
48   *          dongpd@exoplatform.com
49   * Oct 12, 2012
50   */
51  public class AddTranslationPlugin  extends CreatePortalPlugin {
52    
53    /**
54     * Logger.
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     * The init params.
62     */
63    private InitParams initParams;
64  
65    /**
66     * The Multilanguage service.
67     */
68    private MultiLanguageService languageService;
69  
70    /**
71     * Instantiates a new AddTranslationPlugin plugin.
72     * 
73     * @param initParams
74     *          the init params
75     * @param languageService
76     *          Multilanguage service
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        // Replace {portalName} with specific portal
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       // Add translation
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    * Get node by node path.
130    * 
131    * @param nodePath node path of specific node with syntax [workspace:node path]
132    * @return Node of specific node nath
133    * @throws RepositoryException 
134    * @throws Exception
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 }