001/* 002 * Copyright 2016 Anyware Services 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 017package org.ametys.plugins.workspaces.project.helper; 018 019import java.time.ZonedDateTime; 020import java.util.ArrayList; 021import java.util.Arrays; 022import java.util.Collections; 023import java.util.HashMap; 024import java.util.Iterator; 025import java.util.List; 026import java.util.Map; 027import java.util.Set; 028 029import org.apache.avalon.framework.context.Context; 030import org.apache.avalon.framework.context.ContextException; 031import org.apache.avalon.framework.context.Contextualizable; 032import org.apache.avalon.framework.logger.LogEnabled; 033import org.apache.avalon.framework.logger.Logger; 034import org.apache.avalon.framework.service.ServiceException; 035import org.apache.avalon.framework.service.ServiceManager; 036import org.apache.avalon.framework.service.Serviceable; 037import org.apache.cocoon.components.ContextHelper; 038import org.apache.cocoon.environment.Request; 039import org.w3c.dom.Node; 040import org.w3c.dom.NodeList; 041 042import org.ametys.cms.languages.LanguagesManager; 043import org.ametys.cms.tag.Tag; 044import org.ametys.core.user.CurrentUserProvider; 045import org.ametys.core.user.UserIdentity; 046import org.ametys.core.util.DateUtils; 047import org.ametys.core.util.dom.AmetysNodeList; 048import org.ametys.core.util.dom.MapElement; 049import org.ametys.plugins.repository.RepositoryConstants; 050import org.ametys.plugins.repository.data.holder.ModelAwareDataHolder; 051import org.ametys.plugins.repository.provider.RequestAttributeWorkspaceSelector; 052import org.ametys.plugins.workspaces.ObservationConstants; 053import org.ametys.plugins.workspaces.WorkspacesHelper; 054import org.ametys.plugins.workspaces.activities.activitystream.ActivityStreamClientInteraction; 055import org.ametys.plugins.workspaces.alert.AlertWorkspaceModule; 056import org.ametys.plugins.workspaces.categories.Category; 057import org.ametys.plugins.workspaces.categories.CategoryHelper; 058import org.ametys.plugins.workspaces.categories.CategoryProviderExtensionPoint; 059import org.ametys.plugins.workspaces.forum.ForumWorkspaceModule; 060import org.ametys.plugins.workspaces.keywords.KeywordsDAO; 061import org.ametys.plugins.workspaces.news.NewsWorkspaceModule; 062import org.ametys.plugins.workspaces.project.ProjectManager; 063import org.ametys.plugins.workspaces.project.objects.Project; 064import org.ametys.plugins.workspaces.project.rights.ProjectRightHelper; 065import org.ametys.runtime.i18n.I18nizableText; 066import org.ametys.web.WebConstants; 067import org.ametys.web.cocoon.I18nUtils; 068import org.ametys.web.repository.page.Page; 069import org.ametys.web.repository.page.ZoneItem; 070import org.ametys.web.repository.site.Site; 071import org.ametys.web.transformation.xslt.AmetysXSLTHelper; 072 073/** 074 * Helper component to be used from XSL stylesheets to get info related to projects. 075 */ 076public class ProjectXsltHelper implements Serviceable, Contextualizable, LogEnabled 077{ 078 private static Logger _logger; 079 private static Context _context; 080 private static ProjectManager _projectManager; 081 private static CategoryProviderExtensionPoint _categoryProviderEP; 082 private static CategoryHelper _categoryHelper; 083 private static I18nUtils _i18nUtils; 084 private static ActivityStreamClientInteraction _activityStream; 085 private static LanguagesManager _languagesManager; 086 private static CurrentUserProvider _currentUserProvider; 087 private static KeywordsDAO _keywordsDAO; 088 private static WorkspacesHelper _workspaceHelper; 089 private static ProjectRightHelper _projectRightHelper; 090 private static ProjectRightHelper _projectRightsHelper; 091 092 @Override 093 public void contextualize(Context context) throws ContextException 094 { 095 _context = context; 096 } 097 098 @Override 099 public void enableLogging(Logger logger) 100 { 101 _logger = logger; 102 } 103 104 @Override 105 public void service(ServiceManager manager) throws ServiceException 106 { 107 _projectManager = (ProjectManager) manager.lookup(ProjectManager.ROLE); 108 _categoryProviderEP = (CategoryProviderExtensionPoint) manager.lookup(CategoryProviderExtensionPoint.ROLE); 109 _categoryHelper = (CategoryHelper) manager.lookup(CategoryHelper.ROLE); 110 _i18nUtils = (I18nUtils) manager.lookup(org.ametys.core.util.I18nUtils.ROLE); 111 _activityStream = (ActivityStreamClientInteraction) manager.lookup(ActivityStreamClientInteraction.ROLE); 112 _languagesManager = (LanguagesManager) manager.lookup(LanguagesManager.ROLE); 113 _currentUserProvider = (CurrentUserProvider) manager.lookup(CurrentUserProvider.ROLE); 114 _keywordsDAO = (KeywordsDAO) manager.lookup(KeywordsDAO.ROLE); 115 _workspaceHelper = (WorkspacesHelper) manager.lookup(WorkspacesHelper.ROLE); 116 _projectRightHelper = (ProjectRightHelper) manager.lookup(ProjectRightHelper.ROLE); 117 _projectRightsHelper = (ProjectRightHelper) manager.lookup(ProjectRightHelper.ROLE); 118 } 119 120 /** 121 * Returns the current project 122 * @return the current project 123 */ 124 public static String project() 125 { 126 Project project = _workspaceHelper.getProjectFromRequest(); 127 if (project == null && _logger.isWarnEnabled()) 128 { 129 String warnMsg = String.format("No project was found for current site"); 130 _logger.warn(warnMsg); 131 } 132 return project != null ? project.getName() : null; 133 } 134 135 /** 136 * Returns the information of current project 137 * @return the information of current project 138 */ 139 public static MapElement projectInfo() 140 { 141 return projectInfo(project()); 142 } 143 144 /** 145 * Returns the project information 146 * @param projectName The project name 147 * @return the project information 148 */ 149 public static MapElement projectInfo(String projectName) 150 { 151 Map<String, Object> projectInfo = new HashMap<>(); 152 153 Project project = _projectManager.getProject(projectName); 154 if (project != null) 155 { 156 projectInfo.put("name", project.getName()); 157 projectInfo.put("id", project.getId()); 158 projectInfo.put("title", project.getTitle()); 159 projectInfo.put("description", project.getDescription()); 160 projectInfo.put("creationDate", _projectCreationDate(project)); 161 projectInfo.put("lastActivityDate", projectLastActivityDate(project.getName())); 162 projectInfo.put("categoryLabel", _projectCategoryLabel(project)); 163 projectInfo.put("categoryColor", _projectCategoryColor(project)); 164 projectInfo.put("keyword", _projectKeywords(project)); 165 } 166 167 return new MapElement("project", projectInfo); 168 } 169 170 /** 171 * Returns the title of the current project 172 * @return the title of the current project 173 */ 174 public static String projectTitle() 175 { 176 return projectTitle(project()); 177 } 178 179 /** 180 * Returns the title of the given project 181 * @param projectName The project to consider 182 * @return the title of the given project or empty otherwise 183 */ 184 public static String projectTitle(String projectName) 185 { 186 String title = ""; 187 188 Project project = _projectManager.getProject(projectName); 189 if (project != null) 190 { 191 title = project.getTitle(); 192 } 193 194 return title; 195 } 196 197 /** 198 * Returns the description of the current project 199 * @return the description of the current project 200 */ 201 public static String projectDescription() 202 { 203 return projectDescription(project()); 204 } 205 206 /** 207 * Returns the description of the given project 208 * @param projectName The project to consider 209 * @return the description of the given project or empty otherwise 210 */ 211 public static String projectDescription(String projectName) 212 { 213 String title = ""; 214 215 Project project = _projectManager.getProject(projectName); 216 if (project != null) 217 { 218 title = project.getDescription(); 219 } 220 221 return title; 222 } 223 224 /** 225 * Returns the keywords of the current project 226 * @return keywords of the current project 227 */ 228 public static NodeList projectKeywords() 229 { 230 return projectKeywords(project()); 231 } 232 233 /** 234 * Returns the project keywords 235 * @param projectName the project name 236 * @return the project keywords 237 */ 238 public static NodeList projectKeywords(String projectName) 239 { 240 List<MapElement> keywordsElmts = new ArrayList<>(); 241 242 Project project = _projectManager.getProject(projectName); 243 if (project != null) 244 { 245 List<Map<String, String>> keywords = _projectKeywords(project); 246 for (Map<String, String> keyword : keywords) 247 { 248 keywordsElmts.add(new MapElement("keyword", keyword)); 249 } 250 } 251 252 return new AmetysNodeList(keywordsElmts); 253 } 254 255 private static List<Map<String, String>> _projectKeywords(Project project) 256 { 257 List<Map<String, String>> keywords = new ArrayList<>(); 258 259 for (String keyword : project.getKeywords()) 260 { 261 Tag tag = _keywordsDAO.getTag(keyword, Collections.emptyMap()); 262 if (tag != null) 263 { 264 String title = _i18nUtils.translate(tag.getTitle(), AmetysXSLTHelper.lang()); 265 keywords.add(Map.of("title", title, "name", keyword)); 266 } 267 } 268 269 return keywords; 270 } 271 272 /** 273 * Returns the creation date of the current project 274 * @return the creation date of the current project at the ISO format or empty otherwise 275 */ 276 public static String projectCreationDate() 277 { 278 return projectCreationDate(project()); 279 } 280 281 /** 282 * Returns the creation date of the given project 283 * @param projectName The project to consider 284 * @return the creation date of the given project at the ISO format or empty otherwise 285 */ 286 public static String projectCreationDate(String projectName) 287 { 288 String creationDate = ""; 289 290 Project project = _projectManager.getProject(projectName); 291 if (project != null) 292 { 293 creationDate = _projectCreationDate(project); 294 } 295 296 return creationDate; 297 } 298 299 private static String _projectCreationDate(Project project) 300 { 301 ZonedDateTime date = project.getCreationDate(); 302 return date.format(DateUtils.getISODateTimeFormatter()); 303 } 304 305 /** 306 * Return the color associated to the first category associated to the current project 307 * @return the hexa code color or the default color if no color is associated to the first category or if not category is associated. 308 * <color> 309 * <main>#FFFFFF</main> 310 * <text>#000000</text> 311 * </color> 312 */ 313 public static MapElement projectCategoryColor() 314 { 315 return projectCategoryColor(project()); 316 } 317 318 /** 319 * Return the color associated to the first category associated to the given project 320 * @param projectName The project to consider 321 * @return the hexa code color or the default color if no color is associated to the first category or if not category is associated 322 * <color> 323 * <main>#FFFFFF</main> 324 * <text>#000000</text> 325 * </color> 326 */ 327 public static MapElement projectCategoryColor(String projectName) 328 { 329 Project project = _projectManager.getProject(projectName); 330 Map<String, String> color = _projectCategoryColor(project); 331 return new MapElement("color", color); 332 } 333 334 private static Map<String, String> _projectCategoryColor(Project project) 335 { 336 Category category = _getFirstCategory(project); 337 return _categoryColor(category); 338 } 339 340 /** 341 * Return the color associated to the category 342 * @param categoryId category id 343 * @return the hexa code color or the default color if no color is associated to the category or if null 344 * <color> 345 * <main>#FFFFFF</main> 346 * <text>#000000</text> 347 * </color> 348 */ 349 public static MapElement categoryColor(String categoryId) 350 { 351 Category category = _categoryProviderEP.getTag(categoryId, null); 352 return categoryColor(category); 353 } 354 355 /** 356 * Return the color associated to the category 357 * @param category category 358 * @return the hexa code color or the default color if no color is associated to the category or if null 359 * <color> 360 * <main>#FFFFFF</main> 361 * <text>#000000</text> 362 * </color> 363 */ 364 public static MapElement categoryColor(Category category) 365 { 366 Map<String, String> color = _categoryColor(category); 367 return new MapElement("color", color); 368 } 369 370 private static Map<String, String> _categoryColor(Category category) 371 { 372 return _categoryHelper.getCategoryColor(category); 373 } 374 375 private static Category _getFirstCategory(Project project) 376 { 377 if (project != null) 378 { 379 for (String categoryId : project.getCategories()) 380 { 381 Category category = _categoryProviderEP.getTag(categoryId, null); 382 if (category != null) 383 { 384 return category; 385 } 386 } 387 } 388 return null; 389 } 390 391 392 /** 393 * Return the color associated to the first category associated to the current project 394 * @return the hexa code color or the default color if no color is associated to the first category or if not category is associated 395 */ 396 public static String projectCategoryLabel() 397 { 398 return projectCategoryLabel(project()); 399 } 400 401 /** 402 * Return the color associated to the first category associated to the given project 403 * @param projectName The project to consider 404 * @return the label or empty if no category 405 */ 406 public static String projectCategoryLabel(String projectName) 407 { 408 Project project = _projectManager.getProject(projectName); 409 if (project != null) 410 { 411 return _projectCategoryLabel(project); 412 } 413 return null; 414 } 415 416 private static String _projectCategoryLabel(Project project) 417 { 418 Category category = _getFirstCategory(project); 419 if (category != null) 420 { 421 return _i18nUtils.translate(category.getTitle(), AmetysXSLTHelper.lang()); 422 } 423 return null; 424 } 425 426 /** 427 * Get the date of last activity for current project 428 * @return the formatted date of last activity or creation date if there is no activity yet 429 */ 430 public static String projectLastActivityDate() 431 { 432 return projectLastActivityDate(project()); 433 } 434 435 /** 436 * Get the date of last activity for a given project 437 * @param projectName the project's name 438 * @return the formatted date of last activity or creation date if there is no activity yet 439 */ 440 public static String projectLastActivityDate(String projectName) 441 { 442 ZonedDateTime lastDate = _activityStream.getDateOfLastActivity(projectName, Arrays.asList(ObservationConstants.EVENT_MEMBER_ADDED, ObservationConstants.EVENT_MEMBER_DELETED)); 443 return lastDate != null ? lastDate.format(DateUtils.getISODateTimeFormatter()) : projectCreationDate(); 444 } 445 446 /** 447 * True if the resource comes from workspaces 448 * @param resourceId the resource id 449 * @return true if the resource comes from workspaces 450 */ 451 public static boolean isResourceFromWorkspace(String resourceId) 452 { 453 return _projectManager.getParentProject(resourceId) != null; 454 } 455 456 /** 457 * Get the site of a project's resource 458 * @param projectResourceId The resource id 459 * @return The site <site id="site://xxx" name="siteName"><title>Site's titleX</title><url>http://...</url>/site> 460 */ 461 public static Node resourceSite(String projectResourceId) 462 { 463 Project project = _projectManager.getParentProject(projectResourceId); 464 if (project != null) 465 { 466 Site site = project.getSite(); 467 468 Map<String, String> attributes = new HashMap<>(); 469 attributes.put("name", site.getName()); 470 attributes.put("id", site.getId()); 471 472 Map<String, String> values = new HashMap<>(); 473 values.put("title", site.getTitle()); 474 values.put("url", site.getUrl()); 475 476 return new MapElement("site", attributes, values); 477 } 478 479 return null; 480 } 481 482 /** 483 * Get the id of news root's page 484 * @return the id of news root's page or null if not exists 485 */ 486 public static String getNewsRootPageId() 487 { 488 Request request = ContextHelper.getRequest(_context); 489 490 // Retrieve the current workspace. 491 String currentWsp = RequestAttributeWorkspaceSelector.getForcedWorkspace(request); 492 493 try 494 { 495 // Force default workspace (news root's page can not be publish yet as it is a node page) 496 RequestAttributeWorkspaceSelector.setForcedWorkspace(request, RepositoryConstants.DEFAULT_WORKSPACE); 497 498 return getModulePage(NewsWorkspaceModule.NEWS_MODULE_ID); 499 } 500 finally 501 { 502 // Restore workspace 503 RequestAttributeWorkspaceSelector.setForcedWorkspace(request, currentWsp); 504 } 505 } 506 507 /** 508 * Get the id of alerts root's page 509 * @return the id of alerts root's page or null if not exists 510 */ 511 public static String getAlertsRootPageId() 512 { 513 Request request = ContextHelper.getRequest(_context); 514 515 // Retrieve the current workspace. 516 String currentWsp = RequestAttributeWorkspaceSelector.getForcedWorkspace(request); 517 518 try 519 { 520 // Force default workspace (news root's page can not be publish yet as it is a node page) 521 RequestAttributeWorkspaceSelector.setForcedWorkspace(request, RepositoryConstants.DEFAULT_WORKSPACE); 522 523 return getModulePage(AlertWorkspaceModule.ALERT_MODULE_ID); 524 } 525 finally 526 { 527 // Restore workspace 528 RequestAttributeWorkspaceSelector.setForcedWorkspace(request, currentWsp); 529 } 530 } 531 532 /** 533 * Get the module page. 534 * Be careful, the read access is not check ! 535 * @param moduleId the module id 536 * @return the module page 537 */ 538 public static String getModulePage(String moduleId) 539 { 540 String projectName = project(); 541 Project project = _projectManager.getProject(projectName); 542 543 Set<Page> newsRootPages = _projectManager.getModulePages(project, moduleId); 544 Iterator<Page> it = newsRootPages.iterator(); 545 if (it.hasNext()) 546 { 547 return it.next().getId(); 548 } 549 550 return null; 551 } 552 553 /** 554 * Get the translated label associated to the code 555 * @param code The language code 556 * @param language The language in which translate the label 557 * @return The label associated to the code 558 */ 559 public static String getLanguageLabel(String code, String language) 560 { 561 I18nizableText label = _languagesManager.getLanguage(code).getLabel(); 562 return _i18nUtils.translate(label, language); 563 } 564 565 /** 566 * Get the language that will be the default value of the available values. 567 * First choice will be the page language, 'en' otherwise, first available language otherwise. 568 * @param language The current page language 569 * @return A language code 570 */ 571 public static String computeDefaultLanguage(String language) 572 { 573 Request request = ContextHelper.getRequest(_context); 574 ZoneItem zoneItem = (ZoneItem) request.getAttribute(WebConstants.REQUEST_ATTR_ZONEITEM); 575 ModelAwareDataHolder dataHolder = zoneItem.getServiceParameters(); 576 577 String[] availableLanguages = dataHolder.getValue("availableLanguages"); 578 if (Arrays.stream(availableLanguages).anyMatch(l -> l.equals(language))) 579 { 580 return language; 581 } 582 else if (Arrays.stream(availableLanguages).anyMatch(l -> l.equals("en"))) 583 { 584 return "en"; 585 } 586 else 587 { 588 return availableLanguages[0]; 589 } 590 } 591 592 /** 593 * Determines if the current user is a manager of current project 594 * @return true if the current user is manager 595 */ 596 public static boolean isManager() 597 { 598 UserIdentity user = _currentUserProvider.getUser(); 599 String projectName = project(); 600 return _projectManager.isManager(projectName, user); 601 } 602 603 /** 604 * Determines if a user is a manager 605 * @param login the user's login 606 * @param populationId the user's population 607 * @return true if the user is manager 608 */ 609 public static boolean isManager(String login, String populationId) 610 { 611 String projectName = project(); 612 UserIdentity user = new UserIdentity(login, populationId); 613 return _projectManager.isManager(projectName, user); 614 } 615 616 /** 617 * Determines if the current user is a manager for the project 618 * @param projectName the project's name 619 * @return true if the current user is manager 620 */ 621 public static boolean isManager(String projectName) 622 { 623 UserIdentity user = _currentUserProvider.getUser(); 624 return _projectManager.isManager(projectName, user); 625 } 626 627 /** 628 * Determines if the given user is a manager for the project 629 * @param projectName the project's name 630 * @param login the user's login 631 * @param populationId the user's population 632 * @return true if the user is manager 633 */ 634 public static boolean isManager(String projectName, String login, String populationId) 635 { 636 UserIdentity user = new UserIdentity(login, populationId); 637 return _projectManager.isManager(projectName, user); 638 } 639 640 641 /** 642 * Returns true if the current user has the specified right on the specified workspace module 643 * @param rightId Right Id 644 * @param moduleId Module Id 645 * @return true if the current user has the specified right on the specified module 646 */ 647 public static boolean hasRightOnModule(String rightId, String moduleId) 648 { 649 return _projectRightHelper.hasRightOnModule(rightId, moduleId); 650 } 651 652 /** 653 * Returns true if current user can access to the back-office 654 * @return true if current user can access to the back-office 655 */ 656 public static boolean canAccessBO() 657 { 658 String projectName = project(); 659 Project project = _projectManager.getProject(projectName); 660 return project != null ? _projectManager.canAccessBO(project) : false; 661 } 662 663 /** 664 * Returns true if current user can leave the project 665 * @return true if current user can leave the project 666 */ 667 public static boolean canLeaveProject() 668 { 669 String projectName = project(); 670 Project project = _projectManager.getProject(projectName); 671 return project != null ? _projectManager.canLeaveProject(project) : false; 672 } 673 674 /** 675 * Get the status of the user in the current project 676 * "not-allowed" if the user is not allowed in the module in the current project 677 * "current-user" if the user is the current user and in the project 678 * "allowed" if the user is allowed in the module in the current project and not the current user 679 * @param userAsString the user as string 680 * @return the status of the user in the current project 681 */ 682 public String getMemberStatus(String userAsString) 683 { 684 UserIdentity user = UserIdentity.stringToUserIdentity(userAsString); 685 String projectName = project(); 686 Project project = _projectManager.getProject(projectName); 687 if (user == null || !_projectRightsHelper.hasReadAccessOnModule(project, ForumWorkspaceModule.FORUM_MODULE_ID, user)) 688 { 689 return "not-allowed"; 690 } 691 692 // check if user is currentUser 693 if (user.equals(_currentUserProvider.getUser())) 694 { 695 return "current-user"; 696 } 697 return "allowed"; 698 } 699}