1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.exoplatform.ecm.webui.component.explorer;
18
19 import java.util.ArrayList;
20 import java.util.Comparator;
21 import java.util.List;
22 import java.util.MissingResourceException;
23 import java.util.ResourceBundle;
24 import java.util.stream.Collectors;
25
26 import javax.jcr.AccessDeniedException;
27 import javax.jcr.NoSuchWorkspaceException;
28 import javax.jcr.Node;
29 import javax.jcr.Session;
30 import javax.servlet.http.Cookie;
31 import javax.servlet.http.HttpServletRequest;
32
33 import org.exoplatform.container.definition.PortalContainerConfig;
34 import org.exoplatform.container.xml.PortalContainerInfo;
35 import org.exoplatform.ecm.jcr.model.Preference;
36 import org.exoplatform.ecm.webui.component.explorer.control.UIActionBar;
37 import org.exoplatform.ecm.webui.component.explorer.control.UIAddressBar;
38 import org.exoplatform.ecm.webui.component.explorer.control.UIControl;
39 import org.exoplatform.ecm.webui.component.explorer.popup.actions.UIDocumentForm;
40 import org.exoplatform.ecm.webui.component.explorer.popup.actions.UIDocumentFormController;
41 import org.exoplatform.ecm.webui.component.explorer.sidebar.UIAllItems;
42 import org.exoplatform.ecm.webui.component.explorer.sidebar.UISideBar;
43 import org.exoplatform.ecm.webui.component.explorer.sidebar.UITreeExplorer;
44 import org.exoplatform.ecm.webui.utils.JCRExceptionManager;
45 import org.exoplatform.ecm.webui.utils.Utils;
46 import org.exoplatform.portal.webui.util.Util;
47 import org.exoplatform.services.cms.BasePath;
48 import org.exoplatform.services.cms.documents.AutoVersionService;
49 import org.exoplatform.services.cms.drives.DriveData;
50 import org.exoplatform.services.cms.drives.ManageDriveService;
51 import org.exoplatform.services.cms.drives.impl.ManageDriveServiceImpl;
52 import org.exoplatform.services.cms.views.ManageViewService;
53 import org.exoplatform.services.jcr.RepositoryService;
54 import org.exoplatform.services.jcr.core.ManageableRepository;
55 import org.exoplatform.services.jcr.ext.hierarchy.NodeHierarchyCreator;
56 import org.exoplatform.services.organization.OrganizationService;
57 import org.exoplatform.services.organization.User;
58 import org.exoplatform.services.security.ConversationState;
59 import org.exoplatform.services.wcm.core.NodetypeConstant;
60 import org.exoplatform.services.wcm.utils.WCMCoreUtils;
61 import org.exoplatform.web.application.ApplicationMessage;
62 import org.exoplatform.web.application.RequestContext;
63 import org.exoplatform.webui.config.annotation.ComponentConfig;
64 import org.exoplatform.webui.config.annotation.EventConfig;
65 import org.exoplatform.webui.core.UIApplication;
66 import org.exoplatform.webui.core.UIComponent;
67 import org.exoplatform.webui.core.UIContainer;
68 import org.exoplatform.webui.event.Event;
69 import org.exoplatform.webui.event.EventListener;
70
71
72
73
74
75
76
77
78 @ComponentConfig (
79 template = "app:/groovy/webui/component/explorer/UIDrivesArea.gtmpl",
80 events = {
81 @EventConfig(listeners = UIDrivesArea.SelectDriveActionListener.class)
82 }
83 )
84 public class UIDrivesArea extends UIContainer {
85
86 final public static String FIELD_SELECTREPO = "selectRepo" ;
87 private boolean firstVisit = true;
88 private List<String> userRoles_ = null;
89
90 public UIDrivesArea() throws Exception {
91 }
92
93 public void setFirstVisit(boolean firstVisit) {
94 this.firstVisit = firstVisit;
95 }
96
97 public boolean isFirstVisit() {
98 return firstVisit;
99 }
100
101 public String getLabel(String id) {
102 RequestContext context = RequestContext.getCurrentInstance();
103 ResourceBundle res = context.getApplicationResourceBundle();
104 String userDisplayName = "";
105 if (ManageDriveServiceImpl.USER_DRIVE_NAME.equals(id)) {
106 RequestContext ctx = Util.getPortalRequestContext();
107 if (ctx != null) {
108 String username = ctx.getRemoteUser();
109 try {
110 User user = this.getApplicationComponent(OrganizationService.class).getUserHandler().findUserByName(username);
111 if (user != null) {
112 userDisplayName = user.getDisplayName();
113 }
114 } catch (Exception ex) {
115 userDisplayName = username;
116 }
117 }
118 }
119 try {
120 return res.getString("Drives.label." + id.replace(" ", "")).replace("{0}", userDisplayName);
121 } catch (MissingResourceException ex) {
122 return id;
123 }
124 }
125
126 public String getGroupLabel(DriveData driveData) {
127 try {
128 RepositoryService repoService = WCMCoreUtils.getService(RepositoryService.class);
129 NodeHierarchyCreator nodeHierarchyCreator = WCMCoreUtils.getService(NodeHierarchyCreator.class);
130 String groupPath = nodeHierarchyCreator.getJcrPath(BasePath.CMS_GROUPS_PATH);
131 Node groupNode = (Node)WCMCoreUtils.getSystemSessionProvider().getSession(
132 repoService.getCurrentRepository().getConfiguration().getDefaultWorkspaceName(),
133 repoService.getCurrentRepository()).getItem(
134 groupPath + driveData.getName().replace(".", "/"));
135 return groupNode.getProperty(NodetypeConstant.EXO_LABEL).getString();
136 } catch(Exception e) {
137 return driveData.getName().replace(".", " / ");
138 }
139 }
140
141 public String getGroupLabel(String groupId, boolean isFull) {
142 String ret = groupId.replace(".", " / ");
143 if (!isFull) {
144 if (ret.startsWith(" / spaces")) {
145 return ret.substring(ret.lastIndexOf("/") + 1).trim();
146 }
147 int count = 0;
148 int slashPosition = -1;
149 for (int i = 0; i < ret.length(); i++) {
150 if ('/' == ret.charAt(i)) {
151 if (++count == 4) {
152 slashPosition = i;
153 break;
154 }
155 }
156 }
157 if (slashPosition > 0) {
158 ret = ret.substring(0, slashPosition) + "...";
159 } else if (ret.length() > 70) {
160 ret = ret.substring(0, 70) + "...";
161 }
162 }
163 return ret;
164 }
165
166 public String getPortalName() {
167 PortalContainerInfo containerInfo = WCMCoreUtils.getService(PortalContainerInfo.class);
168 return containerInfo.getContainerName();
169 }
170
171 public String getRestName() {
172 PortalContainerConfig portalContainerConfig = this.getApplicationComponent(PortalContainerConfig.class);
173 return portalContainerConfig.getRestContextName(this.getPortalName());
174 }
175
176 private List<String> getUserRoles(boolean newRoleUpdated) throws Exception {
177 ManageDriveService driveService = getApplicationComponent(ManageDriveService.class);
178 if (userRoles_ == null || (userRoles_ != null && newRoleUpdated)) {
179 userRoles_ = Utils.getMemberships();
180 if(newRoleUpdated) driveService.setNewRoleUpdated(false);
181 }
182 return userRoles_;
183 }
184
185 public List<DriveData> mainDrives() throws Exception {
186 ManageDriveService driveService = getApplicationComponent(ManageDriveService.class);
187 List<String> userRoles = getUserRoles(false);
188 String userId = Util.getPortalRequestContext().getRemoteUser();
189 return driveService.getMainDrives(userId, userRoles)
190 .stream()
191 .peek(x -> x.setLabel(getLabel(x.getName())))
192 .sorted(Comparator.comparing(DriveData::getLabel))
193 .collect(Collectors.toList());
194 }
195
196 public List<DriveData> groupDrives() throws Exception {
197 ManageDriveService driveService = getApplicationComponent(ManageDriveService.class);
198 List<String> userRoles = getUserRoles(driveService.newRoleUpdated());
199 String userId = Util.getPortalRequestContext().getRemoteUser();
200 return driveService.getGroupDrives(userId, userRoles)
201 .stream()
202 .peek(x -> x.setLabel(getGroupLabel(x)))
203 .sorted(Comparator.comparing(DriveData::getLabel))
204 .collect(Collectors.toList());
205 }
206
207 public List<DriveData> personalDrives() throws Exception {
208 ManageDriveService driveService = getApplicationComponent(ManageDriveService.class);
209 String userId = Util.getPortalRequestContext().getRemoteUser();
210 return driveService.getPersonalDrives(userId)
211 .stream()
212 .peek(x -> x.setLabel(getLabel(x.getName())))
213 .sorted(Comparator.comparing(DriveData::getLabel))
214 .collect(Collectors.toList());
215 }
216
217 static public class SelectDriveActionListener extends EventListener<UIDrivesArea> {
218 public void execute(Event<UIDrivesArea> event) throws Exception {
219 UIDrivesArea uiDrivesArea = event.getSource();
220 String driveName = event.getRequestContext().getRequestParameter(OBJECTID);
221 RepositoryService rservice = uiDrivesArea.getApplicationComponent(RepositoryService.class);
222 ManageDriveService dservice = uiDrivesArea.getApplicationComponent(ManageDriveService.class);
223 DriveData drive = dservice.getDriveByName(driveName);
224 String userId = Util.getPortalRequestContext().getRemoteUser();
225 UIApplication uiApp = uiDrivesArea.getAncestorOfType(UIApplication.class);
226 List<String> viewList = new ArrayList<String>();
227 for(String role : Utils.getMemberships()){
228 for(String viewName : drive.getViews().split(",")) {
229 if (!viewList.contains(viewName.trim())) {
230 Node viewNode = uiDrivesArea.getApplicationComponent(ManageViewService.class)
231 .getViewByName(viewName.trim(), WCMCoreUtils.getSystemSessionProvider());
232 String permiss = viewNode.getProperty("exo:accessPermissions").getString();
233 if(permiss.contains("${userId}")) permiss = permiss.replace("${userId}", userId);
234 String[] viewPermissions = permiss.split(",");
235 if(permiss.equals("*")) viewList.add(viewName.trim());
236 if(drive.hasPermission(viewPermissions, role)) viewList.add(viewName.trim());
237 }
238 }
239 }
240 if(viewList.isEmpty()) {
241 Object[] args = { driveName };
242 uiApp.addMessage(new ApplicationMessage("UIDrivesArea.msg.no-view-found", args));
243
244 return;
245 }
246 StringBuffer viewListStr = new StringBuffer();
247 for (String viewName : viewList) {
248 if (viewListStr.length() > 0)
249 viewListStr.append(",").append(viewName);
250 else
251 viewListStr.append(viewName);
252 }
253 drive.setViews(viewListStr.toString());
254 String homePath = drive.getHomePath();
255 if(homePath.contains("${userId}")) {
256 homePath = org.exoplatform.services.cms.impl.Utils.getPersonalDrivePath(homePath, userId);
257 if (drive.getParameters().get(ManageDriveServiceImpl.DRIVE_PARAMATER_USER_ID) == null) {
258 drive.getParameters().put(ManageDriveServiceImpl.DRIVE_PARAMATER_USER_ID, homePath);
259 }
260 }
261 UIJCRExplorerPortlet uiParent = uiDrivesArea.getAncestorOfType(UIJCRExplorerPortlet.class);
262 uiParent.setFlagSelect(true);
263 UIJcrExplorerContainer explorerContainer = uiParent.getChild(UIJcrExplorerContainer.class);
264 UIJCRExplorer uiJCRExplorer = explorerContainer.getChild(UIJCRExplorer.class);
265 UITreeExplorer uiTreeExplorer = uiJCRExplorer.findFirstComponentOfType(UITreeExplorer.class);
266
267
268 Preference pref = uiJCRExplorer.getPreference();
269
270 if (!pref.isShowSideBar())
271 pref.setShowSideBar(drive.getViewSideBar());
272 pref.setShowPreferenceDocuments(drive.getViewPreferences());
273 pref.setAllowCreateFoder(drive.getAllowCreateFolders());
274 HttpServletRequest request = Util.getPortalRequestContext().getRequest();
275 Cookie[] cookies = request.getCookies();
276 Cookie getCookieForUser = UIJCRExplorer.getCookieByCookieName(Preference.PREFERENCE_SHOW_HIDDEN_NODE, cookies);
277 if (uiJCRExplorer.findFirstComponentOfType(UIAllItems.class) == null || getCookieForUser == null) {
278 pref.setShowHiddenNode(drive.getShowHiddenNode());
279 }
280 if (getCookieForUser == null) {
281 pref.setShowNonDocumentType(drive.getViewNonDocument());
282 }
283 uiJCRExplorer.setDriveData(drive);
284 uiJCRExplorer.setIsReferenceNode(false);
285 uiJCRExplorer.setPreferencesSaved(true);
286 uiJCRExplorer.clearTagSelection();
287
288 ManageableRepository repository = rservice.getCurrentRepository();
289 try {
290 Session session = WCMCoreUtils.getUserSessionProvider().getSession(drive.getWorkspace(), repository);
291
292
293
294 session.getItem(homePath);
295 } catch(AccessDeniedException ace) {
296 Object[] args = { driveName };
297 uiApp.addMessage(new ApplicationMessage("UIDrivesArea.msg.access-denied", args,
298 ApplicationMessage.WARNING));
299
300 return;
301 } catch(NoSuchWorkspaceException nosuchWS) {
302 Object[] args = { driveName };
303 uiApp.addMessage(new ApplicationMessage("UIDrivesArea.msg.workspace-not-exist", args,
304 ApplicationMessage.WARNING));
305
306 return;
307 } catch(Exception e) {
308 JCRExceptionManager.process(uiApp, e);
309 return;
310 }
311 uiJCRExplorer.clearNodeHistory(homePath);
312 uiJCRExplorer.setRepositoryName(repository.getConfiguration().getName());
313 uiJCRExplorer.setWorkspaceName(drive.getWorkspace());
314 uiJCRExplorer.setRootPath(homePath);
315 uiJCRExplorer.setSelectNode(drive.getWorkspace(), homePath);
316 String selectedView = viewList.get(0);
317 UIControl uiControl = uiJCRExplorer.getChild(UIControl.class).setRendered(true);
318 UIWorkingArea uiWorkingArea = uiJCRExplorer.getChild(UIWorkingArea.class);
319 UIActionBar uiActionbar = uiWorkingArea.getChild(UIActionBar.class);
320 uiActionbar.setTabOptions(selectedView);
321 UIAddressBar uiAddressBar = uiControl.getChild(UIAddressBar.class);
322 uiAddressBar.setViewList(viewList);
323 uiAddressBar.setSelectedViewName(selectedView);
324 uiWorkingArea.getChild(UISideBar.class).initialize();
325 for(UIComponent uiComp : uiWorkingArea.getChildren()) {
326 if(uiComp instanceof UIDrivesArea) uiComp.setRendered(false);
327 else uiComp.setRendered(true);
328 }
329 UIDocumentWorkspace uiDocumentWorkspace = uiWorkingArea.getChild(UIDocumentWorkspace.class);
330 UIDocumentFormController controller = uiDocumentWorkspace.removeChild(UIDocumentFormController.class);
331 AutoVersionService autoVersionService = WCMCoreUtils.getService(AutoVersionService.class);
332 if (controller != null) {
333 controller.getChild(UIDocumentForm.class).releaseLock();
334 }
335 uiParent.setRenderedChild(UIJcrExplorerContainer.class);
336 event.getRequestContext().getJavascriptManager().
337 require("SHARED/multiUpload", "multiUpload").
338 addScripts("multiUpload.setLocation('" +
339 uiJCRExplorer.getWorkspaceName() + "','" +
340 uiJCRExplorer.getDriveData().getName() + "','" +
341 uiTreeExplorer.getLabel() + "','" +
342 uiJCRExplorer.getCurrentPath() + "','" +
343 org.exoplatform.services.cms.impl.Utils.getPersonalDrivePath(uiJCRExplorer.getDriveData().getHomePath(),
344 ConversationState.getCurrent().getIdentity().getUserId())+ "', '"+
345 autoVersionService.isVersionSupport(uiJCRExplorer.getCurrentPath(), uiJCRExplorer.getCurrentWorkspace())+"');");
346 uiJCRExplorer.findFirstComponentOfType(UIDocumentInfo.class).getExpandedFolders().clear();
347 uiJCRExplorer.updateAjax(event);
348 }
349 }
350
351 }