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.admin.drives;
18  
19  import java.util.ArrayList;
20  import java.util.Collections;
21  import java.util.Comparator;
22  import java.util.List;
23  import java.util.MissingResourceException;
24  import java.util.ResourceBundle;
25  
26  import javax.jcr.PathNotFoundException;
27  import javax.jcr.Session;
28  
29  import org.exoplatform.commons.utils.LazyPageList;
30  import org.exoplatform.commons.utils.ListAccessImpl;
31  import org.exoplatform.container.xml.PortalContainerInfo;
32  import org.exoplatform.ecm.webui.component.admin.UIECMAdminPortlet;
33  import org.exoplatform.ecm.webui.core.UIPagingGridDecorator;
34  import org.exoplatform.services.cms.drives.DriveData;
35  import org.exoplatform.services.cms.drives.ManageDriveService;
36  import org.exoplatform.services.jcr.RepositoryService;
37  import org.exoplatform.services.jcr.core.ManageableRepository;
38  import org.exoplatform.services.wcm.utils.WCMCoreUtils;
39  import org.exoplatform.web.application.RequestContext;
40  import org.exoplatform.webui.config.annotation.ComponentConfig;
41  import org.exoplatform.webui.config.annotation.EventConfig;
42  import org.exoplatform.webui.event.Event;
43  import org.exoplatform.webui.event.EventListener;
44  
45  /**
46   * Created by The eXo Platform SARL
47   * Author : Dang Van Minh
48   *          minh.dang@exoplatform.com
49   * Nov 23, 2006
50   * 11:39:49 AM
51   */
52  @ComponentConfig(
53                   template = "app:/groovy/webui/component/admin/drives/UIDriveList.gtmpl",
54                   events = {
55                       @EventConfig(listeners = UIDriveList.DeleteActionListener.class, confirm = "UIDriveList.msg.confirm-delete"),
56                       @EventConfig(listeners = UIDriveList.EditInfoActionListener.class),
57                       @EventConfig(listeners = UIDriveList.AddDriveActionListener.class)
58                   }
59      )
60  public class UIDriveList extends UIPagingGridDecorator {
61  
62    final static public String[] ACTIONS = {"AddDrive"} ;
63    final  static public String ST_ADD = "AddDriveManagerPopup" ;
64    final  static public String ST_EDIT = "EditDriveManagerPopup" ;
65  
66    public UIDriveList() throws Exception {
67      getUIPageIterator().setId("UIDriveListIterator");
68    }
69  
70    public String[] getActions() { return ACTIONS ; }
71  
72    public void refresh(int currentPage) throws Exception {
73      LazyPageList<DriveData> dataPageList = new LazyPageList<DriveData>(new ListAccessImpl<DriveData>(DriveData.class,
74          getDrives()),
75          getUIPageIterator().getItemsPerPage());
76      getUIPageIterator().setPageList(dataPageList);
77      if (currentPage > getUIPageIterator().getAvailablePage())
78        getUIPageIterator().setCurrentPage(getUIPageIterator().getAvailablePage());
79      else
80        getUIPageIterator().setCurrentPage(currentPage);
81    }
82  
83    public List<DriveData> getDriveList() throws Exception { return getUIPageIterator().getCurrentPageData() ; }
84  
85    public String getRequestContextName() {
86      return WCMCoreUtils.getRestContextName();
87    }
88  
89    public List<DriveData> getDrives() throws Exception {
90      RepositoryService rservice = getApplicationComponent(RepositoryService.class) ;
91      ManageDriveService driveService = getApplicationComponent(ManageDriveService.class) ;
92      ManageableRepository repository = rservice.getCurrentRepository() ;
93      List<DriveData> driveList = new ArrayList<DriveData>() ;
94      Session session = null ;
95      List<DriveData> drives = driveService.getAllDrives(true) ;
96      if(drives != null && drives.size() > 0) {
97        for(DriveData drive : drives) {      	
98          if(drive.getIcon() != null && drive.getIcon().length() > 0) {
99            try {
100             String[] iconPath = drive.getIcon().split(":/") ;
101             session = repository.getSystemSession(iconPath[0]) ;
102             session.getItem("/" + iconPath[1]) ;
103             session.logout() ;
104           } catch(PathNotFoundException pnf) {
105             drive.setIcon("") ;
106           }
107         }
108         if(isExistWorkspace(repository, drive)) driveList.add(drive) ;
109       }
110     }
111     Collections.sort(driveList) ;
112     return driveList ;
113   }
114 
115   /**
116    * Get Drive Views Labels from resource Bundle.
117    *
118    * @param driveData DriveData
119    * @return Views Labels
120    */
121   public String getDriveViews(DriveData driveData) {
122     ResourceBundle res = RequestContext.getCurrentInstance().getApplicationResourceBundle();
123     String[] viewNames = driveData.getViews().split(",");
124     StringBuilder strBuilder = new StringBuilder();
125     String viewName = null;
126     for (int i = 0; i < viewNames.length; i++) {
127       viewName = viewNames[i].trim();
128       String label = null;
129       try {
130         label = res.getString("Views.label." + viewName);
131       } catch (MissingResourceException e) {
132         label = viewName;
133       }
134 
135       if (strBuilder.length() > 0) {
136         strBuilder.append(", ");
137       }
138 
139       strBuilder.append(label);
140     }
141     return strBuilder.toString();
142   }
143 
144   public String getPortalName() {
145     PortalContainerInfo containerInfo = WCMCoreUtils.getService(PortalContainerInfo.class);
146     return containerInfo.getContainerName();
147   }
148 
149   public String getRepository() throws Exception {
150     return getAncestorOfType(UIECMAdminPortlet.class).getPreferenceRepository() ;
151   }
152 
153   private boolean isExistWorkspace(ManageableRepository repository, DriveData drive) {
154     for(String ws:  repository.getWorkspaceNames()) {
155       if(ws.equals(drive.getWorkspace())) return true ;
156     }
157     return false ;
158   }
159   static public class DriveComparator implements Comparator<DriveData> {
160     public int compare(DriveData o1, DriveData o2) throws ClassCastException {
161       String name1 = o1.getName() ;
162       String name2 = o2.getName() ;
163       return name1.compareToIgnoreCase(name2) ;
164     }
165   }
166 
167   static  public class AddDriveActionListener extends EventListener<UIDriveList> {
168     public void execute(Event<UIDriveList> event) throws Exception {
169       UIDriveManager uiDriveManager = event.getSource().getParent() ;
170       uiDriveManager.removeChildById(UIDriveList.ST_EDIT);
171       uiDriveManager.initPopup(UIDriveList.ST_ADD) ;
172       UIDriveForm uiForm = uiDriveManager.findFirstComponentOfType(UIDriveForm.class) ;
173       uiForm.refresh(null) ;
174       event.getRequestContext().addUIComponentToUpdateByAjax(uiDriveManager) ;
175     }
176   }
177 
178   static  public class DeleteActionListener extends EventListener<UIDriveList> {
179     public void execute(Event<UIDriveList> event) throws Exception {
180       String name = event.getRequestContext().getRequestParameter(OBJECTID) ;
181       UIDriveList uiDriveList = event.getSource();
182       ManageDriveService driveService = uiDriveList.getApplicationComponent(ManageDriveService.class) ;
183       driveService.removeDrive(name) ;
184       uiDriveList.refresh(uiDriveList.getUIPageIterator().getCurrentPage()) ;
185       event.getRequestContext().addUIComponentToUpdateByAjax(uiDriveList.getParent()) ;
186     }
187   }
188 
189   static  public class EditInfoActionListener extends EventListener<UIDriveList> {
190     public void execute(Event<UIDriveList> event) throws Exception {
191       UIDriveManager uiDriveManager = event.getSource().getParent() ;
192       uiDriveManager.removeChildById(UIDriveList.ST_ADD);
193       uiDriveManager.initPopup(UIDriveList.ST_EDIT) ;
194       String driveName = event.getRequestContext().getRequestParameter(OBJECTID) ;
195       uiDriveManager.findFirstComponentOfType(UIDriveForm.class).refresh(driveName) ;
196       event.getRequestContext().addUIComponentToUpdateByAjax(uiDriveManager) ;
197     }
198   }
199 
200   public String standardizeGroupName(String groupName) throws Exception {
201     groupName = groupName.replaceAll("-", " ");
202     char[] stringArray = groupName.toCharArray();
203     stringArray[0] = Character.toUpperCase(stringArray[0]);
204     groupName = new String(stringArray);
205     return groupName;
206   }
207 }