View Javadoc
1   /*
2    * Copyright (C) 2003-2008 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.wcm.webui.selector.content.multi;
18  
19  import javax.jcr.ItemNotFoundException;
20  import javax.jcr.Node;
21  import javax.jcr.RepositoryException;
22  
23  import org.apache.commons.lang.StringUtils;
24  import org.exoplatform.ecm.utils.text.Text;
25  import org.exoplatform.ecm.webui.selector.UISelectable;
26  import org.exoplatform.services.cms.drives.DriveData;
27  import org.exoplatform.services.cms.drives.ManageDriveService;
28  import org.exoplatform.services.cms.link.LinkManager;
29  import org.exoplatform.services.log.ExoLogger;
30  import org.exoplatform.services.log.Log;
31  import org.exoplatform.services.wcm.publication.WCMComposer;
32  import org.exoplatform.services.wcm.utils.WCMCoreUtils;
33  import org.exoplatform.wcm.webui.Utils;
34  import org.exoplatform.wcm.webui.selector.content.UIContentBrowsePanel;
35  import org.exoplatform.wcm.webui.selector.content.UIContentSelector;
36  import org.exoplatform.webui.config.annotation.ComponentConfig;
37  import org.exoplatform.webui.config.annotation.EventConfig;
38  import org.exoplatform.webui.core.lifecycle.Lifecycle;
39  import org.exoplatform.webui.event.Event;
40  import org.exoplatform.webui.event.EventListener;
41  /**
42   * The Class UIContentBrowsePanelMulti.
43   */
44  @ComponentConfig(
45    lifecycle = Lifecycle.class,
46    template = "classpath:groovy/wcm/webui/selector/content/multi/UIContentBrowsePanel.gtmpl",
47    events = {
48      @EventConfig(listeners = UIContentBrowsePanel.ChangeContentTypeActionListener.class),
49      @EventConfig(listeners = UIContentBrowsePanelMulti.SelectActionListener.class),
50      @EventConfig(listeners = UIContentBrowsePanelMulti.CloseActionListener.class),
51      @EventConfig(listeners = UIContentBrowsePanelMulti.SaveTemporaryActionListener.class)
52    }
53  )
54  
55  public class UIContentBrowsePanelMulti extends UIContentBrowsePanel {
56  
57    private static final Log LOG = ExoLogger.getLogger(UIContentBrowsePanelMulti.class.getName());
58  
59    /** The item paths. */
60    private String itemPaths;
61    private String itemTarget;
62    /** i18n Delete Confirmation message */
63    private String deleteConfirmationMsg = "UIBrowserPanel.Confirm.Delete";
64    /**
65     * Gets the item paths.
66     *
67     * @return the item paths
68     */
69    public String getItemPaths() {
70      return itemPaths;
71    }
72    private String _initPath = "";
73    private String _initDrive = "";
74  
75    public void setInitPath(String initDrive, String initPath) {
76      this._initPath = initPath;
77      this._initDrive = initDrive;
78    }
79  
80    public String getInitDrive() { return this._initDrive; }
81    public String getInitPath() { return this._initPath; }
82  
83    /**
84     * Sets the item paths.
85     *
86     * @param itemPaths the new item paths
87     */
88    public void setItemPaths(String itemPaths) {
89      this.itemPaths = itemPaths;
90      setItemTargetPath(getTargetPath(itemPaths));
91    }
92  
93    public void setItemTargetPath(String _itemTarget) {
94     this.itemTarget = _itemTarget;
95    }
96    public String getItemTargetPath(){
97     return this.itemTarget;
98    }
99     /**
100    *
101    * @param savedItems
102    * @return
103    */
104   protected String getTargetPath(String savedItems) {
105    int i, n;
106    LinkManager linkManager;
107    String[] savedItemList =savedItems.split(";");
108    String savedItem;
109    n = savedItemList.length;
110    StringBuilder result = new StringBuilder("");
111    linkManager = WCMCoreUtils.getService(LinkManager.class);
112    for (i = 0; i<n; i++) {
113      savedItem = savedItemList[i];
114       String[] locations = (savedItem == null) ? null : savedItem.split(":");
115       Node node = (locations != null && locations.length >= 3) ? Utils.getViewableNodeByComposer(
116           locations[0], locations[1], locations[2]) : null;
117       savedItem = StringUtils.EMPTY;
118       if (node != null){
119         try {
120          savedItem = node.getPath();
121          if (linkManager.isLink(node)) {
122            node = linkManager.getTarget(node);
123            savedItem = node.getPath();
124          }
125         } catch (ItemNotFoundException e){
126           savedItem = StringUtils.EMPTY;
127         } catch (RepositoryException e){
128           savedItem = StringUtils.EMPTY;
129         }
130       }
131       result.append(savedItem).append(";");
132    }
133    return result.toString();
134   }
135 
136   /**
137    * The listener interface for receiving selectAction events.
138    * The class that is interested in processing a selectAction
139    * event implements this interface, and the object created
140    * with that class is registered with a component using the
141    * component's <code>addSelectActionListener</code> method. When
142    * the selectAction event occurs, that object's appropriate
143    * method is invoked.
144    */
145   public static class SelectActionListener extends EventListener<UIContentBrowsePanelMulti> {
146 
147     /* (non-Javadoc)
148      * @see org.exoplatform.webui.event.EventListener#execute(org.exoplatform.webui.event.Event)
149      */
150     public void execute(Event<UIContentBrowsePanelMulti> event) throws Exception {
151       UIContentBrowsePanelMulti contentBrowsePanelMulti = event.getSource();
152       String returnFieldName = contentBrowsePanelMulti.getReturnFieldName();
153       ((UISelectable)(contentBrowsePanelMulti.getSourceComponent())).doSelect(returnFieldName, contentBrowsePanelMulti.getItemPaths());
154     }
155   }
156   /**
157    * The listener interface for receiving SaveTemporaryAction events.
158    * The class that is interested in processing a SaveTemporaryAction
159    * event implements this interface, and the object created
160    * with that class is registered with a component using the
161    * component's <code>SaveTemporaryActionListener</code> method. When
162    * the selectAction event occurs, that object's appropriate
163    * method is invoked.
164    *
165    * @see SaveTemporaryActionListener
166    */
167   public static class SaveTemporaryActionListener extends EventListener<UIContentBrowsePanelMulti> {
168 
169     /* (non-Javadoc)
170      * @see org.exoplatform.webui.event.EventListener#execute(org.exoplatform.webui.event.Event)
171      */
172     public void execute(Event<UIContentBrowsePanelMulti> event) throws Exception {
173         UIContentBrowsePanelMulti contentBrowsePanelMulti = event.getSource();
174         Node node = null;
175         String itemPathtemp = "";
176 
177         String operationType = event.getRequestContext().getRequestParameter("oper");
178         String dPath = event.getRequestContext().getRequestParameter("path");
179         String iDriver = event.getRequestContext().getRequestParameter("driverName");
180         String iPath = event.getRequestContext().getRequestParameter("currentPath");
181         String tempIPath = iPath;
182         String[] locations = (iPath == null) ? null : iPath.split(":");
183         if (operationType.equals("clean") && contentBrowsePanelMulti.getItemPaths() != null) {
184             contentBrowsePanelMulti.setItemPaths("");
185             return;
186         }
187         if (iDriver != null && iDriver.length() > 0) {
188             if (locations != null && locations.length > 2)
189                 node = Utils.getViewableNodeByComposer(Text.escapeIllegalJcrChars(locations[0]),
190                         Text.escapeIllegalJcrChars(locations[1]),
191                         Text.escapeIllegalJcrChars(locations[2]),
192                         WCMComposer.BASE_VERSION);
193 
194             if (node != null) {
195                 iPath = fixPath(iDriver, node.getPath(), contentBrowsePanelMulti);
196                 contentBrowsePanelMulti.setInitPath(iDriver, iPath);
197             } else {
198                 contentBrowsePanelMulti.setInitPath(iDriver, iPath);
199             }
200         } else
201              contentBrowsePanelMulti.setInitPath("", "");
202                if (operationType.equals("add") && contentBrowsePanelMulti.getItemPaths() != null) {
203                   itemPathtemp = contentBrowsePanelMulti.getItemPaths().concat(tempIPath).concat(";");
204                    contentBrowsePanelMulti.setItemPaths(itemPathtemp);
205                }
206                else if (operationType.equals("delete") && contentBrowsePanelMulti.getItemPaths() != null) {
207                    itemPathtemp = contentBrowsePanelMulti.getItemPaths();
208                    itemPathtemp = StringUtils.remove(itemPathtemp, dPath.concat(";"));
209                     contentBrowsePanelMulti.setItemPaths(itemPathtemp);
210                }
211           else
212               contentBrowsePanelMulti.setItemPaths(tempIPath.concat(";"));
213               UIContentSelector contentSelector = contentBrowsePanelMulti.getAncestorOfType(UIContentSelector.class);
214               contentSelector.setSelectedTab(contentBrowsePanelMulti.getId());
215     }
216 
217       private String fixPath(String driveName,
218                            String path,
219                            UIContentBrowsePanelMulti uiBrowser) throws Exception {
220       if (path == null || path.length() == 0)
221         return "";
222       path = Text.escapeIllegalJcrChars(path);
223       ManageDriveService managerDriveService = uiBrowser.getApplicationComponent(ManageDriveService.class);
224       DriveData driveData = managerDriveService.getDriveByName(driveName);
225       if (!path.startsWith(driveData.getHomePath()))
226         return "";
227       if ("/".equals(driveData.getHomePath()))
228         return path;
229       return path.substring(driveData.getHomePath().length());
230     }
231   }
232 
233   public String getDeleteConfirmationMsg() {
234     return org.exoplatform.ecm.webui.utils.Utils.getResourceBundle(org.exoplatform.ecm.webui.utils.Utils.LOCALE_WEBUI_DMS,
235                                                                    deleteConfirmationMsg,
236                                                                    UIContentBrowsePanelMulti.class.getClassLoader());
237   }
238 
239   public String getLocaleMsg(String key) {
240     return org.exoplatform.ecm.webui.utils.Utils.getResourceBundle(org.exoplatform.ecm.webui.utils.Utils.LOCALE_WEBUI_DMS,
241                                                                    key,
242                                                                    UIContentBrowsePanelMulti.class.getClassLoader());
243   }
244   /**
245    * The listener interface for receiving closeAction events.
246    * The class that is interested in processing a closeAction
247    * event implements this interface, and the object created
248    * with that class is registered with a component using the
249    * component's <code>addCloseActionListener</code> method. When
250    * the closeAction event occurs, that object's appropriate
251    * method is invoked.
252    */
253   public static class CloseActionListener extends EventListener<UIContentBrowsePanelMulti> {
254 
255     /* (non-Javadoc)
256      * @see org.exoplatform.webui.event.EventListener#execute(org.exoplatform.webui.event.Event)
257      */
258     public void execute(Event<UIContentBrowsePanelMulti> event) throws Exception {
259       UIContentBrowsePanelMulti contentBrowsePanelMulti = event.getSource();
260       ((UISelectable)(contentBrowsePanelMulti.getSourceComponent())).doSelect(null, null);
261     }
262   }
263 
264 }