001/* 002 * Copyright 2015 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 */ 016package org.ametys.odf.orgunit; 017 018import java.util.ArrayList; 019import java.util.HashMap; 020import java.util.List; 021import java.util.Map; 022 023import org.apache.avalon.framework.component.Component; 024import org.apache.avalon.framework.service.ServiceException; 025import org.apache.avalon.framework.service.ServiceManager; 026import org.apache.avalon.framework.service.Serviceable; 027 028import org.ametys.core.right.RightManager; 029import org.ametys.core.ui.Callable; 030import org.ametys.odf.ODFHelper; 031import org.ametys.plugins.repository.AmetysObjectResolver; 032import org.ametys.plugins.repository.UnknownAmetysObjectException; 033 034/** 035 * DAO for manipulating OrgUnits 036 */ 037public class OrgUnitDAO implements Serviceable, Component 038{ 039 /** The Avalon role */ 040 public static final String ROLE = OrgUnitDAO.class.getName(); 041 042 /** The Ametys Object Resolver */ 043 private AmetysObjectResolver _resolver; 044 045 /** The ODF helper */ 046 private ODFHelper _odfHelper; 047 048 private RightManager _rightManager; 049 050 public void service(ServiceManager manager) throws ServiceException 051 { 052 _resolver = (AmetysObjectResolver) manager.lookup(AmetysObjectResolver.ROLE); 053 _odfHelper = (ODFHelper) manager.lookup(ODFHelper.ROLE); 054 _rightManager = (RightManager) manager.lookup(RightManager.ROLE); 055 } 056 057 /** 058 * Gets the information about the given orgunits 059 * @param orgUnitIds The orgunits ids 060 * @return A map of information 061 */ 062 @Callable (rights = Callable.NO_CHECK_REQUIRED) // Used by select orgunits widget 063 public Map<String, Object> getOrgUnitsInfos (List<String> orgUnitIds) 064 { 065 return getOrgUnitsInfos(orgUnitIds, null); 066 } 067 068 /** 069 * Gets the information about the given orgunits 070 * @param orgUnitIds The orgunits ids 071 * @param rootOrgUnitId the root orgUnitId 072 * @return A map of information 073 */ 074 @Callable (rights = Callable.CHECKED_BY_IMPLEMENTATION) // Used by select orgunits widget 075 public Map<String, Object> getOrgUnitsInfos (List<String> orgUnitIds, String rootOrgUnitId) 076 { 077 Map<String, Object> result = new HashMap<>(); 078 079 List<Map<String, Object>> orgUnits = new ArrayList<>(); 080 List<String> notFound = new ArrayList<>(); 081 List<String> noRight = new ArrayList<>(); 082 083 for (String id : orgUnitIds) 084 { 085 try 086 { 087 OrgUnit orgUnit = _resolver.resolveById(id); 088 if (_rightManager.currentUserHasReadAccess(orgUnit)) 089 { 090 orgUnits.add(getOrgUnitInfos(orgUnit, rootOrgUnitId)); 091 } 092 else 093 { 094 noRight.add(id); 095 } 096 } 097 catch (UnknownAmetysObjectException e) 098 { 099 notFound.add(id); 100 } 101 } 102 103 result.put("orgUnits", orgUnits); 104 result.put("orgUnitsNotFound", notFound); 105 result.put("orgUnitsNoRight", noRight); 106 107 return result; 108 } 109 110 /** 111 * Gets the information about the given orgunit 112 * @param orgUnitId The orgunit id 113 * @return A map of information 114 */ 115 public Map<String, Object> getOrgUnitInfos(String orgUnitId) 116 { 117 return getOrgUnitInfos(orgUnitId, null); 118 } 119 120 /** 121 * Gets the information about the given orgunit 122 * @param orgUnitId The orgunit id 123 * @param rootOrgUnitId the root orgUnit id 124 * @return A map of information 125 */ 126 public Map<String, Object> getOrgUnitInfos(String orgUnitId, String rootOrgUnitId) 127 { 128 OrgUnit orgUnit = _resolver.resolveById(orgUnitId); 129 return getOrgUnitInfos(orgUnit, rootOrgUnitId); 130 } 131 132 /** 133 * Gets the information about the given orgunit 134 * @param orgUnit The orgunit 135 * @param rootOrgUnitId the root orgUnit id 136 * @return A map of information 137 */ 138 public Map<String, Object> getOrgUnitInfos(OrgUnit orgUnit, String rootOrgUnitId) 139 { 140 Map<String, Object> infos = new HashMap<>(); 141 142 infos.put("id", orgUnit.getId()); 143 infos.put("name", orgUnit.getName()); 144 infos.put("title", orgUnit.getTitle()); 145 infos.put("path", _odfHelper.getOrgUnitPath(orgUnit.getId(), rootOrgUnitId)); 146 147 return infos; 148 } 149}