View Javadoc
1   /*
2    * Copyright (C) 2003-2007 eXo Platform SAS.
3    *
4    * This program is free software; you can redistribute it and/or
5    * modify it under the terms of the GNU Affero General Public License
6    * as published by the Free Software Foundation; either version 3
7    * of the License, or (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 General Public License for more details.
13   *
14   * You should have received a copy of the GNU General Public License
15   * along with this program; if not, see<http://www.gnu.org/licenses/>.
16   */
17  package org.exoplatform.ecm.webui.component.explorer.symlink;
18  import java.security.AccessControlException;
19  import java.util.ArrayList;
20  import java.util.List;
21  
22  import javax.jcr.AccessDeniedException;
23  import javax.jcr.ItemExistsException;
24  import javax.jcr.ItemNotFoundException;
25  import javax.jcr.Node;
26  import javax.jcr.RepositoryException;
27  import javax.jcr.UnsupportedRepositoryOperationException;
28  import javax.jcr.nodetype.ConstraintViolationException;
29  
30  import org.exoplatform.ecm.webui.component.explorer.UIJCRExplorer;
31  import org.exoplatform.ecm.webui.selector.UISelectable;
32  import org.exoplatform.ecm.webui.tree.selectone.UIOneNodePathSelector;
33  import org.exoplatform.ecm.webui.utils.Utils;
34  import org.exoplatform.services.cms.drives.DriveData;
35  import org.exoplatform.services.cms.drives.ManageDriveService;
36  import org.exoplatform.services.cms.i18n.MultiLanguageService;
37  import org.exoplatform.services.cms.link.NodeFinder;
38  import org.exoplatform.services.exceptions.SameAsDefaultLangException;
39  import org.exoplatform.services.jcr.util.Text;
40  import org.exoplatform.services.log.ExoLogger;
41  import org.exoplatform.services.log.Log;
42  import org.exoplatform.services.wcm.utils.WCMCoreUtils;
43  import org.exoplatform.wcm.webui.selector.content.one.UIContentBrowsePanelOne;
44  import org.exoplatform.wcm.webui.selector.content.one.UIContentSelectorOne;
45  import org.exoplatform.web.application.ApplicationMessage;
46  import org.exoplatform.webui.config.annotation.ComponentConfig;
47  import org.exoplatform.webui.config.annotation.ComponentConfigs;
48  import org.exoplatform.webui.config.annotation.EventConfig;
49  import org.exoplatform.webui.core.UIApplication;
50  import org.exoplatform.webui.core.UIComponent;
51  import org.exoplatform.webui.core.UIPopupComponent;
52  import org.exoplatform.webui.core.UIPopupWindow;
53  import org.exoplatform.webui.core.lifecycle.UIFormLifecycle;
54  import org.exoplatform.webui.event.Event;
55  import org.exoplatform.webui.event.Event.Phase;
56  import org.exoplatform.webui.event.EventListener;
57  import org.exoplatform.webui.form.UIForm;
58  import org.exoplatform.webui.form.UIFormStringInput;
59  
60  /**
61   * Created by The eXo Platform SARL
62   * Author : Chien Nguyen Van
63   *
64   */
65  
66  @ComponentConfigs( {
67      @ComponentConfig(lifecycle = UIFormLifecycle.class,
68          template = "app:/groovy/webui/component/explorer/thumbnail/UIAddTranslationForm.gtmpl",
69          events = {
70        @EventConfig(listeners = UIAddTranslationForm.SaveActionListener.class),
71        @EventConfig(listeners = UIAddTranslationForm.CancelActionListener.class, phase = Phase.DECODE) }),
72      @ComponentConfig(type = UITranslationFormMultiValueInputSet.class, id = "SymLinkMultipleInputset", events = {
73        @EventConfig(listeners = UIAddTranslationForm.RemoveActionListener.class, phase = Phase.DECODE),
74        @EventConfig(listeners = UIAddTranslationForm.SelectDocumentActionListener.class, phase = Phase.DECODE) }) })
75  
76  public class UIAddTranslationForm extends UIForm implements UIPopupComponent, UISelectable {
77    
78    public static final String FIELD_PATH = "pathNode";
79    public static final String FIELD_SYMLINK = "fieldPathNode";
80    public static final String POPUP_SYMLINK = "UIPopupSymLink";
81  
82    /**
83     * Logger.
84     */
85    private static final Log LOG  = ExoLogger.getLogger(UIAddTranslationForm.class.getName());
86  
87    final static public byte DRIVE_SELECTOR_MODE = 0;
88    final static public byte WORSPACE_SELECTOR_MODE = 1;
89  
90    private byte selectorMode=DRIVE_SELECTOR_MODE;
91  
92    public UIAddTranslationForm() throws Exception {
93    }
94  
95    public void activate() {}
96    public void deActivate() {}
97  
98    public void initFieldInput() throws Exception {
99      UITranslationFormMultiValueInputSet uiTranslationFormMultiValue = createUIComponent(UITranslationFormMultiValueInputSet.class,
100                                                                   "SymLinkMultipleInputset",
101                                                                   null);
102     uiTranslationFormMultiValue.setId(FIELD_PATH);
103     uiTranslationFormMultiValue.setName(FIELD_PATH);
104     uiTranslationFormMultiValue.setEditable(false);
105     uiTranslationFormMultiValue.setType(UIFormStringInput.class);
106     addUIFormInput(uiTranslationFormMultiValue);
107   }
108 
109   public void doSelect(String selectField, Object value) throws Exception {
110     String valueNodeName = String.valueOf(value).trim();
111     String workspaceName = valueNodeName.substring(0, valueNodeName.lastIndexOf(":/"));
112     valueNodeName = valueNodeName.substring(workspaceName.lastIndexOf(":")+1);
113     List<String> listNodeName = new ArrayList<String>();
114     listNodeName.add(valueNodeName);
115     UITranslationFormMultiValueInputSet uiTranslationFormMultiValueInputSet = getChild(UITranslationFormMultiValueInputSet.class);
116     uiTranslationFormMultiValueInputSet.setValue(listNodeName);
117     String symLinkName = valueNodeName.substring(valueNodeName.lastIndexOf("/") + 1);
118     int squareBracketIndex = symLinkName.indexOf('[');
119     if (squareBracketIndex > -1)
120       symLinkName = symLinkName.substring(0, squareBracketIndex);
121     if (symLinkName.indexOf(".lnk") < 0) {
122       StringBuffer sb = new StringBuffer();
123       sb.append(symLinkName).append(".lnk");
124       symLinkName = sb.toString();
125     }
126     symLinkName = Text.unescapeIllegalJcrChars(symLinkName);
127     UIAddTranslationManager uiAddTranslationManager = getParent();
128     uiAddTranslationManager.removeChildById(POPUP_SYMLINK);
129   }
130 
131   static  public class SaveActionListener extends EventListener<UIAddTranslationForm> {
132     public void execute(Event<UIAddTranslationForm> event) throws Exception {
133       UIAddTranslationForm uiTranslationForm = event.getSource();
134       UIJCRExplorer uiExplorer = uiTranslationForm.getAncestorOfType(UIJCRExplorer.class);
135       UIApplication uiApp = uiTranslationForm.getAncestorOfType(UIApplication.class);
136       String pathNode = "";
137       UITranslationFormMultiValueInputSet uiSet = uiTranslationForm.getChild(UITranslationFormMultiValueInputSet.class);
138       List<UIComponent> listChildren = uiSet.getChildren();
139       for (UIComponent component : listChildren) {
140         UIFormStringInput uiStringInput = (UIFormStringInput)component;
141         if(uiStringInput.getValue() != null) {
142           pathNode = uiStringInput.getValue().trim();
143         }
144       }
145 
146       Node node = uiExplorer.getCurrentNode() ;
147       if(uiExplorer.nodeIsLocked(node)) {
148         uiApp.addMessage(new ApplicationMessage("UIPopupMenu.msg.node-locked", null)) ;
149 
150         return ;
151       }
152       if(pathNode == null || pathNode.length() ==0) {
153         uiApp.addMessage(new ApplicationMessage("UIAddTranslationForm.msg.path-node-invalid", null));
154 
155         return ;
156       }
157       String workspaceName = pathNode.substring(0, pathNode.lastIndexOf(":/"));
158       pathNode = pathNode.substring(pathNode.lastIndexOf(":/") + 1);
159       
160       NodeFinder nodeFinder = uiTranslationForm.getApplicationComponent(NodeFinder.class);
161       try {
162         nodeFinder.getItem(workspaceName, pathNode);
163       } catch (ItemNotFoundException e) {
164         uiApp.addMessage(new ApplicationMessage("UIAddTranslationForm.msg.non-node",
165                                                 null,
166                                                 ApplicationMessage.WARNING));
167 
168         return;
169       } catch (RepositoryException re) {
170         uiApp.addMessage(new ApplicationMessage("UIAddTranslationForm.msg.non-node",
171                                                 null,
172                                                 ApplicationMessage.WARNING));
173 
174         return;
175       } catch(Exception e) {
176         if (LOG.isErrorEnabled()) {
177           LOG.error("An unexpected error occurs", e);
178         }
179         uiApp.addMessage(new ApplicationMessage("UIAddTranslationForm.msg.non-node",
180                                                 null,
181                                                 ApplicationMessage.WARNING));
182 
183         return;
184       }
185       try {
186         Node targetNode = (Node) nodeFinder.getItem(workspaceName, pathNode);
187         MultiLanguageService langService = uiTranslationForm.getApplicationComponent(MultiLanguageService.class);
188         langService.addSynchronizedLinkedLanguage(node, targetNode);
189         uiExplorer.updateAjax(event);
190       } catch (AccessControlException ace) {
191         uiApp.addMessage(new ApplicationMessage("UIAddTranslationForm.msg.repository-exception",
192                                                 null,
193                                                 ApplicationMessage.WARNING));
194 
195         return;
196       } catch (AccessDeniedException ade) {
197         uiApp.addMessage(new ApplicationMessage("UIAddTranslationForm.msg.repository-exception",
198                                                 null,
199                                                 ApplicationMessage.WARNING));
200 
201         return;
202       } catch(NumberFormatException nume) {
203         uiApp.addMessage(new ApplicationMessage("UIAddTranslationForm.msg.numberformat-exception",
204                                                 null,
205                                                 ApplicationMessage.WARNING));
206 
207         return;
208       } catch(ConstraintViolationException cve) {
209         uiApp.addMessage(new ApplicationMessage("UIAddTranslationForm.msg.cannot-save",
210                                                 null,
211                                                 ApplicationMessage.WARNING));
212 
213         return;
214       } catch(ItemExistsException iee) {
215         uiApp.addMessage(new ApplicationMessage("UIAddTranslationForm.msg.item-exists-exception",
216                                                 null,
217                                                 ApplicationMessage.WARNING));
218 
219         return;
220       } catch(UnsupportedRepositoryOperationException unOperationException) {
221         uiApp.addMessage(new ApplicationMessage("UIAddTranslationForm.msg.UnsupportedRepositoryOperationException",
222                                                 null,
223                                                 ApplicationMessage.WARNING));
224 
225         return;
226       } catch (SameAsDefaultLangException unOperationException) {
227         uiApp.addMessage(new ApplicationMessage("UIAddTranslationForm.msg.translation-node-same-language-default",
228                                                 null,
229                                                 ApplicationMessage.WARNING));
230 
231         return;
232       } catch (Exception e) {
233         if (LOG.isErrorEnabled()) {
234           LOG.error("Unexpected error", e);
235         }
236         uiApp.addMessage(new ApplicationMessage("UIAddTranslationForm.msg.cannot-save",
237                                                 null,
238                                                 ApplicationMessage.WARNING));
239 
240         return;
241       }
242     }
243   }
244 
245   static  public class CancelActionListener extends EventListener<UIAddTranslationForm> {
246     public void execute(Event<UIAddTranslationForm> event) throws Exception {
247       UIJCRExplorer uiExplorer = event.getSource().getAncestorOfType(UIJCRExplorer.class);
248       uiExplorer.cancelAction();
249     }
250   }
251 
252   static  public class RemoveActionListener extends EventListener<UITranslationFormMultiValueInputSet> {
253     public void execute(Event<UITranslationFormMultiValueInputSet> event) throws Exception {
254       UITranslationFormMultiValueInputSet uiSet = event.getSource();
255       UIComponent uiComponent = uiSet.getParent();
256       if (uiComponent instanceof UIAddTranslationForm) {
257         UIAddTranslationForm uiTranslationForm = (UIAddTranslationForm)uiComponent;
258         String id = event.getRequestContext().getRequestParameter(OBJECTID);
259         uiSet.removeChildById(id);
260         event.getRequestContext().addUIComponentToUpdateByAjax(uiTranslationForm);
261       }
262     }
263   }
264 
265   static public class SelectDocumentActionListener extends EventListener<UITranslationFormMultiValueInputSet> {
266     private String fixPath(String path, String driveName, String repo, UIAddTranslationForm uiAddTranslationForm) throws Exception {
267       if (path == null || path.length() == 0 ||
268           driveName == null || driveName.length() == 0 ||
269           repo == null || repo.length() == 0)
270         return "";
271       ManageDriveService managerDriveService = uiAddTranslationForm.getApplicationComponent(ManageDriveService.class);
272       DriveData driveData = managerDriveService.getDriveByName(driveName);
273       if (!path.startsWith(driveData.getHomePath()))
274         return "";
275       if ("/".equals(driveData.getHomePath()))
276         return path;
277       return path.substring(driveData.getHomePath().length());
278     }
279     public void execute(Event<UITranslationFormMultiValueInputSet> event) throws Exception {
280       UITranslationFormMultiValueInputSet uiSet = event.getSource();
281       UIAddTranslationForm uiTranslationForm =  (UIAddTranslationForm) uiSet.getParent();
282       UIAddTranslationManager uiAddTranslationManager = uiTranslationForm.getParent();
283       UIJCRExplorer uiExplorer = uiTranslationForm.getAncestorOfType(UIJCRExplorer.class);
284       String workspaceName = uiExplorer.getCurrentWorkspace();
285       String param = "returnField=" + FIELD_SYMLINK;
286       UIPopupWindow uiPopupWindow = uiAddTranslationManager.initPopupTaxonomy(POPUP_SYMLINK);
287 
288       if (uiTranslationForm.isUseWorkspaceSelector()) {
289         UIOneNodePathSelector uiNodePathSelector = uiAddTranslationManager.createUIComponent(UIOneNodePathSelector.class, null, null);
290         uiPopupWindow.setUIComponent(uiNodePathSelector);
291         uiNodePathSelector.setIsDisable(workspaceName, false);
292         uiNodePathSelector.setExceptedNodeTypesInPathPanel(new String[] {Utils.EXO_SYMLINK});
293         uiNodePathSelector.setRootNodeLocation(uiExplorer.getRepositoryName(), workspaceName, "/");
294         uiNodePathSelector.setIsShowSystem(false);
295         uiNodePathSelector.init(WCMCoreUtils.getUserSessionProvider());
296         uiNodePathSelector.setSourceComponent(uiTranslationForm, new String[]{param});
297       }else {
298         Node node =uiExplorer.getCurrentNode();
299         UIContentSelectorOne uiNodePathSelector = uiTranslationForm.createUIComponent(UIContentSelectorOne.class, null, null);
300         uiPopupWindow.setUIComponent(uiNodePathSelector);
301         uiNodePathSelector.init(uiExplorer.getDriveData().getName(),
302                                 fixPath(node == null ? "" : node.getPath(),
303                                         uiExplorer.getDriveData().getName(),
304                                         uiExplorer.getRepositoryName(),
305                                         uiTranslationForm));
306         uiNodePathSelector.getChild(UIContentBrowsePanelOne.class).setSourceComponent(uiTranslationForm, new String[] { param });
307       }
308       uiPopupWindow.setRendered(true);
309       uiPopupWindow.setShow(true);
310       event.getRequestContext().addUIComponentToUpdateByAjax(uiAddTranslationManager);
311     }
312   }
313   public void useWorkspaceSelector() {
314     this.selectorMode = WORSPACE_SELECTOR_MODE;
315   }
316   public void useDriveSelector() {
317     this.selectorMode = DRIVE_SELECTOR_MODE;
318   }
319   public boolean isUseWorkspaceSelector() {
320     return this.selectorMode==WORSPACE_SELECTOR_MODE;
321   }
322 }