Coverage Report - org.kuali.student.core.organization.service.impl.OrganizationServiceImpl
 
Classes in this File Line Coverage Branch Coverage Complexity
OrganizationServiceImpl
0%
0/280
0%
0/26
1.435
 
 1  
 /**
 2  
  * Copyright 2010 The Kuali Foundation Licensed under the Educational Community License, Version 2.0 (the "License"); you may
 3  
  * not use this file except in compliance with the License. You may obtain a copy of the License at
 4  
  * http://www.osedu.org/licenses/ECL-2.0 Unless required by applicable law or agreed to in writing, software distributed
 5  
  * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
 6  
  * implied. See the License for the specific language governing permissions and limitations under the License.
 7  
  */
 8  
 
 9  
 package org.kuali.student.core.organization.service.impl;
 10  
 
 11  
 import java.util.ArrayList;
 12  
 import java.util.HashMap;
 13  
 import java.util.HashSet;
 14  
 import java.util.List;
 15  
 import java.util.Map;
 16  
 import java.util.Set;
 17  
 
 18  
 import javax.jws.WebService;
 19  
 import javax.persistence.NoResultException;
 20  
 
 21  
 import org.apache.log4j.Logger;
 22  
 import org.kuali.student.common.dictionary.old.dto.ObjectStructure;
 23  
 import org.kuali.student.common.dictionary.service.old.DictionaryService;
 24  
 import org.kuali.student.common.dto.StatusInfo;
 25  
 import org.kuali.student.common.exceptions.AlreadyExistsException;
 26  
 import org.kuali.student.common.exceptions.DataValidationErrorException;
 27  
 import org.kuali.student.common.exceptions.DoesNotExistException;
 28  
 import org.kuali.student.common.exceptions.InvalidParameterException;
 29  
 import org.kuali.student.common.exceptions.MissingParameterException;
 30  
 import org.kuali.student.common.exceptions.OperationFailedException;
 31  
 import org.kuali.student.common.exceptions.PermissionDeniedException;
 32  
 import org.kuali.student.common.exceptions.VersionMismatchException;
 33  
 import org.kuali.student.common.search.dto.SearchCriteriaTypeInfo;
 34  
 import org.kuali.student.common.search.dto.SearchRequest;
 35  
 import org.kuali.student.common.search.dto.SearchResult;
 36  
 import org.kuali.student.common.search.dto.SearchResultTypeInfo;
 37  
 import org.kuali.student.common.search.dto.SearchTypeInfo;
 38  
 import org.kuali.student.common.search.service.SearchManager;
 39  
 import org.kuali.student.common.validation.dto.ValidationResultInfo;
 40  
 import org.kuali.student.common.validator.old.Validator;
 41  
 import org.kuali.student.core.organization.dao.OrganizationDao;
 42  
 import org.kuali.student.core.organization.dto.OrgHierarchyInfo;
 43  
 import org.kuali.student.core.organization.dto.OrgInfo;
 44  
 import org.kuali.student.core.organization.dto.OrgOrgRelationInfo;
 45  
 import org.kuali.student.core.organization.dto.OrgOrgRelationTypeInfo;
 46  
 import org.kuali.student.core.organization.dto.OrgPersonRelationInfo;
 47  
 import org.kuali.student.core.organization.dto.OrgPersonRelationTypeInfo;
 48  
 import org.kuali.student.core.organization.dto.OrgPositionRestrictionInfo;
 49  
 import org.kuali.student.core.organization.dto.OrgTreeInfo;
 50  
 import org.kuali.student.core.organization.dto.OrgTypeInfo;
 51  
 import org.kuali.student.core.organization.entity.Org;
 52  
 import org.kuali.student.core.organization.entity.OrgHierarchy;
 53  
 import org.kuali.student.core.organization.entity.OrgOrgRelation;
 54  
 import org.kuali.student.core.organization.entity.OrgOrgRelationType;
 55  
 import org.kuali.student.core.organization.entity.OrgPersonRelation;
 56  
 import org.kuali.student.core.organization.entity.OrgPersonRelationType;
 57  
 import org.kuali.student.core.organization.entity.OrgPositionRestriction;
 58  
 import org.kuali.student.core.organization.entity.OrgType;
 59  
 import org.kuali.student.core.organization.service.OrganizationService;
 60  
 import org.kuali.student.core.organizationsearch.service.impl.OrganizationSearch;
 61  
 import org.springframework.transaction.annotation.Transactional;
 62  
 
 63  
 @WebService(endpointInterface = "org.kuali.student.core.organization.service.OrganizationService", serviceName = "OrganizationService", portName = "OrganizationService", targetNamespace = "http://student.kuali.org/wsdl/organization")
 64  
 @Transactional(readOnly = true, noRollbackFor = {DoesNotExistException.class}, rollbackFor = {Throwable.class})
 65  0
 public class OrganizationServiceImpl implements OrganizationService {
 66  
 
 67  0
     final Logger logger = Logger.getLogger(OrganizationServiceImpl.class);
 68  
 
 69  
     private OrganizationDao organizationDao;
 70  
     private DictionaryService dictionaryServiceDelegate;
 71  
     private SearchManager searchManager;
 72  
     private Validator validator;
 73  
 
 74  
     private Map<String, OrganizationSearch> searchOperations;
 75  
 
 76  
     public void setSearchOperations(Map<String, OrganizationSearch> searchOperations) {
 77  0
         this.searchOperations = searchOperations;
 78  0
     }
 79  
 
 80  
     @Override
 81  
     @Transactional(readOnly = false)
 82  
     public OrgPositionRestrictionInfo addPositionRestrictionToOrg(String orgId, String orgPersonRelationTypeKey, OrgPositionRestrictionInfo orgPositionRestrictionInfo) throws AlreadyExistsException, DataValidationErrorException, DoesNotExistException, InvalidParameterException, MissingParameterException, OperationFailedException, PermissionDeniedException {
 83  
 
 84  
         // Check Missing params
 85  0
         checkForMissingParameter(orgId, "orgId");
 86  0
         checkForMissingParameter(orgPersonRelationTypeKey, "orgPersonRelationTypeKey");
 87  0
         checkForMissingParameter(orgPositionRestrictionInfo, "orgPositionRestrictionInfo");
 88  
 
 89  
         // Set all the values on OrgOrgRelationInfo
 90  0
         orgPositionRestrictionInfo.setOrgId(orgId);
 91  0
         orgPositionRestrictionInfo.setOrgPersonRelationTypeKey(orgPersonRelationTypeKey);
 92  
 
 93  0
         OrgPositionRestriction orgPositionRestriction = null;
 94  
 
 95  
         // Create a new persistence entity from the Info
 96  
         try {
 97  0
             orgPositionRestriction = OrganizationAssembler.toOrgPositionRestriction(false, orgPositionRestrictionInfo, organizationDao);
 98  0
         } catch (VersionMismatchException e) {}
 99  
 
 100  
         // Persist the positionRestriction
 101  0
         organizationDao.create(orgPositionRestriction);
 102  
 
 103  
         // Copy back to an OrgOrgRelationInfo and return
 104  0
         OrgPositionRestrictionInfo createdOrgPositionRestrictionInfo = OrganizationAssembler.toOrgPositionRestrictionInfo(orgPositionRestriction);
 105  0
         return createdOrgPositionRestrictionInfo;
 106  
     }
 107  
 
 108  
     @Override
 109  
     @Transactional(readOnly = false)
 110  
     public OrgOrgRelationInfo createOrgOrgRelation(String orgId, String relatedOrgId, String orgOrgRelationTypeKey, OrgOrgRelationInfo orgOrgRelationInfo) throws AlreadyExistsException, DataValidationErrorException, DoesNotExistException, InvalidParameterException, MissingParameterException, PermissionDeniedException, OperationFailedException {
 111  
 
 112  
         // Check Missing params
 113  0
         checkForMissingParameter(orgId, "orgId");
 114  0
         checkForMissingParameter(relatedOrgId, "relatedOrgId");
 115  0
         checkForMissingParameter(orgOrgRelationTypeKey, "orgOrgRelationTypeKey");
 116  0
         checkForMissingParameter(orgOrgRelationInfo, "orgOrgRelationInfo");
 117  
 
 118  
         // Set all the values on OrgOrgRelationInfo
 119  0
         orgOrgRelationInfo.setOrgId(orgId);
 120  0
         orgOrgRelationInfo.setRelatedOrgId(relatedOrgId);
 121  0
         orgOrgRelationInfo.setType(orgOrgRelationTypeKey);
 122  
 
 123  0
         OrgOrgRelation orgOrgRelation = null;
 124  
 
 125  
         // Create a new persistence entity from the orgInfo
 126  
         try {
 127  0
             orgOrgRelation = OrganizationAssembler.toOrgOrgRelation(false, orgOrgRelationInfo, organizationDao);
 128  0
         } catch (VersionMismatchException e) {}
 129  
 
 130  
         // Persist the orgOrgRelation
 131  0
         organizationDao.create(orgOrgRelation);
 132  
 
 133  
         // Copy back to an OrgOrgRelationInfo and return
 134  0
         OrgOrgRelationInfo createdOrgOrgRelationInfo = OrganizationAssembler.toOrgOrgRelationInfo(orgOrgRelation);
 135  0
         return createdOrgOrgRelationInfo;
 136  
     }
 137  
 
 138  
     @Override
 139  
     @Transactional(readOnly = false)
 140  
     public OrgPersonRelationInfo createOrgPersonRelation(String orgId, String personId, String orgPersonRelationTypeKey, OrgPersonRelationInfo orgPersonRelationInfo) throws AlreadyExistsException, DataValidationErrorException, DoesNotExistException, InvalidParameterException, MissingParameterException, PermissionDeniedException, OperationFailedException {
 141  
 
 142  
         // Check Missing params
 143  0
         checkForMissingParameter(orgId, "orgId");
 144  0
         checkForMissingParameter(personId, "personId");
 145  0
         checkForMissingParameter(orgPersonRelationTypeKey, "orgPersonRelationTypeKey");
 146  0
         checkForMissingParameter(orgPersonRelationTypeKey, "orgPersonRelationTypeKey");
 147  
 
 148  
         // Make sure that only valid org person relations are done
 149  0
         if (!organizationDao.validatePositionRestriction(orgId, orgPersonRelationTypeKey)) {
 150  0
             throw new InvalidParameterException("There is no Position for this relationship");
 151  
         }
 152  
 
 153  
         // Set all the values on OrgOrgRelationInfo
 154  0
         orgPersonRelationInfo.setOrgId(orgId);
 155  0
         orgPersonRelationInfo.setPersonId(personId);
 156  0
         orgPersonRelationInfo.setType(orgPersonRelationTypeKey);
 157  
 
 158  0
         OrgPersonRelation orgPersonRelation = null;
 159  
 
 160  
         // Create a new persistence entity from the orgInfo
 161  
         try {
 162  0
             orgPersonRelation = OrganizationAssembler.toOrgPersonRelation(false, orgPersonRelationInfo, organizationDao);
 163  0
         } catch (VersionMismatchException e) {}
 164  
 
 165  
         // Persist the orgPersonRelation
 166  0
         organizationDao.create(orgPersonRelation);
 167  
 
 168  
         // Copy back to an orgPersonRelationInfo and return
 169  0
         OrgPersonRelationInfo createdOrgPersonRelationInfo = OrganizationAssembler.toOrgPersonRelationInfo(orgPersonRelation);
 170  0
         return createdOrgPersonRelationInfo;
 171  
     }
 172  
 
 173  
     @Override
 174  
     @Transactional(readOnly = false)
 175  
     public OrgInfo createOrganization(String orgTypeKey, OrgInfo orgInfo) throws AlreadyExistsException, DataValidationErrorException, InvalidParameterException, MissingParameterException, OperationFailedException, PermissionDeniedException {
 176  
 
 177  
         // Check Missing params
 178  0
         checkForMissingParameter(orgTypeKey, "orgTypeKey");
 179  0
         checkForMissingParameter(orgInfo, "orgInfo");
 180  
 
 181  
         // Set all the values on orgInfo
 182  0
         orgInfo.setType(orgTypeKey);
 183  
 
 184  
         try {
 185  0
             List<ValidationResultInfo> validations = validateOrg("", orgInfo);
 186  0
             for (ValidationResultInfo validationResult : validations) {
 187  0
                 if (validationResult.isError())
 188  0
                     throw new DataValidationErrorException(validationResult.toString());
 189  
             }
 190  0
         } catch (DoesNotExistException e1) {
 191  0
             logger.error("Exception occured: ", e1);
 192  0
         }
 193  
 
 194  0
         Org org = null;
 195  
 
 196  
         // Create a new persistence entity from the orgInfo
 197  
         try {
 198  0
             org = OrganizationAssembler.toOrg(false, orgInfo, organizationDao);
 199  0
         } catch (DoesNotExistException e) {
 200  
             // OrgAssembler should not be throwing this exception for create!
 201  0
             logger.info(e.getMessage(), e);
 202  0
             throw new OperationFailedException(e.getMessage(), e);
 203  0
         } catch (VersionMismatchException e) {
 204  
             // OrgAssembler should not be throwing this exception for create!
 205  0
             logger.info(e.getMessage(), e);
 206  0
             throw new OperationFailedException(e.getMessage(), e);
 207  0
         }
 208  
 
 209  
         // Persist the org
 210  0
         organizationDao.create(org);
 211  
 
 212  
         // Copy back to an orgInfo and return
 213  0
         OrgInfo createdOrgInfo = OrganizationAssembler.toOrgInfo(org);
 214  0
         return createdOrgInfo;
 215  
 
 216  
     }
 217  
 
 218  
     @Override
 219  
     @Transactional(readOnly = false)
 220  
     public StatusInfo deleteOrganization(String orgId) throws DoesNotExistException, InvalidParameterException, MissingParameterException, OperationFailedException, PermissionDeniedException {
 221  
 
 222  0
         checkForMissingParameter(orgId, "orgId");
 223  
 
 224  0
         Org org = organizationDao.fetch(Org.class, orgId);
 225  
 
 226  0
         if (org == null) {
 227  0
             throw new DoesNotExistException("Org does not exist for id: " + orgId);
 228  
         }
 229  
 
 230  0
         organizationDao.delete(org);
 231  
 
 232  0
         StatusInfo statusInfo = new StatusInfo();
 233  0
         statusInfo.setSuccess(true);
 234  0
         return statusInfo;
 235  
     }
 236  
 
 237  
     @Override
 238  
     public List<String> getAllDescendants(String orgId, String orgHierarchy) throws InvalidParameterException, MissingParameterException, OperationFailedException, PermissionDeniedException {
 239  
 
 240  0
         checkForMissingParameter(orgId, "orgId");
 241  0
         checkForMissingParameter(orgId, "orgHierarchy");
 242  
 
 243  0
         List<String> descendants = this.organizationDao.getAllDescendants(orgId, orgHierarchy);
 244  0
         return descendants;
 245  
     }
 246  
 
 247  
     @Override
 248  
     public List<OrgPersonRelationInfo> getAllOrgPersonRelationsByOrg(String orgId) throws DoesNotExistException, InvalidParameterException, MissingParameterException, OperationFailedException, PermissionDeniedException {
 249  0
         checkForMissingParameter(orgId, "orgId");
 250  
 
 251  0
         return OrganizationAssembler.toOrgPersonRelationInfos(organizationDao.getAllOrgPersonRelationsByOrg(orgId));
 252  
     }
 253  
 
 254  
     @Override
 255  
     public List<OrgPersonRelationInfo> getAllOrgPersonRelationsByPerson(String personId) throws DoesNotExistException, InvalidParameterException, MissingParameterException, OperationFailedException, PermissionDeniedException {
 256  0
         checkForMissingParameter(personId, "personId");
 257  
 
 258  0
         List<OrgPersonRelation> orgPersonRelationInfo = organizationDao.getAllOrgPersonRelationsByPerson(personId);
 259  0
         return OrganizationAssembler.toOrgPersonRelationInfos(orgPersonRelationInfo);
 260  
     }
 261  
 
 262  
     @Override
 263  
     public List<String> getAllAncestors(String orgId, String orgHierarchy) throws InvalidParameterException, MissingParameterException, OperationFailedException, PermissionDeniedException {
 264  0
         checkForMissingParameter(orgId, "orgId");
 265  0
         checkForMissingParameter(orgHierarchy, "orgHierarchy");
 266  
 
 267  0
         List<String> ancestors = this.organizationDao.getAllAncestors(orgId, orgHierarchy);
 268  0
         return ancestors;
 269  
     }
 270  
 
 271  
     @Override
 272  
     public List<OrgHierarchyInfo> getOrgHierarchies() throws OperationFailedException {
 273  0
         return OrganizationAssembler.toOrgHierarchyInfos(organizationDao.find(OrgHierarchy.class));
 274  
 
 275  
     }
 276  
 
 277  
     @Override
 278  
     public OrgHierarchyInfo getOrgHierarchy(String orgHierarchyKey) throws DoesNotExistException, InvalidParameterException, MissingParameterException, OperationFailedException {
 279  0
         checkForMissingParameter(orgHierarchyKey, "orgHierarchyKey");
 280  
 
 281  0
         return OrganizationAssembler.toOrgHierarchyInfo(organizationDao.fetch(OrgHierarchy.class, orgHierarchyKey));
 282  
     }
 283  
 
 284  
     @Override
 285  
     public OrgOrgRelationInfo getOrgOrgRelation(String orgOrgRelationId) throws DoesNotExistException, InvalidParameterException, MissingParameterException, OperationFailedException, PermissionDeniedException {
 286  0
         checkForMissingParameter(orgOrgRelationId, "orgOrgRelationId");
 287  
 
 288  0
         return OrganizationAssembler.toOrgOrgRelationInfo(organizationDao.fetch(OrgOrgRelation.class, orgOrgRelationId));
 289  
 
 290  
     }
 291  
 
 292  
     @Override
 293  
     public OrgOrgRelationTypeInfo getOrgOrgRelationType(String orgOrgRelationTypeKey) throws DoesNotExistException, InvalidParameterException, MissingParameterException, OperationFailedException {
 294  0
         checkForMissingParameter(orgOrgRelationTypeKey, "orgOrgRelationTypeKey");
 295  
 
 296  0
         return OrganizationAssembler.toOrgOrgRelationTypeInfo(organizationDao.fetch(OrgOrgRelationType.class, orgOrgRelationTypeKey));
 297  
     }
 298  
 
 299  
     @Override
 300  
     public List<OrgOrgRelationTypeInfo> getOrgOrgRelationTypes() throws OperationFailedException {
 301  0
         List<OrgOrgRelationType> orgOrgRelationTypes = organizationDao.find(OrgOrgRelationType.class);
 302  0
         return OrganizationAssembler.toOrgOrgRelationTypeInfos(orgOrgRelationTypes);
 303  
     }
 304  
 
 305  
     @Override
 306  
     public List<OrgOrgRelationTypeInfo> getOrgOrgRelationTypesForOrgHierarchy(String orgHierarchyKey) throws DoesNotExistException, InvalidParameterException, MissingParameterException, OperationFailedException {
 307  0
         checkForMissingParameter(orgHierarchyKey, "orgHierarchyKey");
 308  
 
 309  0
         List<OrgOrgRelationType> orgOrgRelationTypes = organizationDao.getOrgOrgRelationTypesForOrgHierarchy(orgHierarchyKey);
 310  0
         return OrganizationAssembler.toOrgOrgRelationTypeInfos(orgOrgRelationTypes);
 311  
     }
 312  
 
 313  
     @Override
 314  
     public List<OrgOrgRelationTypeInfo> getOrgOrgRelationTypesForOrgType(String orgTypeKey) throws DoesNotExistException, InvalidParameterException, MissingParameterException, OperationFailedException {
 315  0
         checkForMissingParameter(orgTypeKey, "orgTypeKey");
 316  
 
 317  0
         List<OrgOrgRelationType> orgOrgRelationTypes = organizationDao.getOrgOrgRelationTypesForOrgType(orgTypeKey);
 318  0
         return OrganizationAssembler.toOrgOrgRelationTypeInfos(orgOrgRelationTypes);
 319  
     }
 320  
 
 321  
     @Override
 322  
     public List<OrgOrgRelationInfo> getOrgOrgRelationsByIdList(List<String> orgOrgRelationIdList) throws DoesNotExistException, InvalidParameterException, MissingParameterException, OperationFailedException, PermissionDeniedException {
 323  0
         checkForMissingParameter(orgOrgRelationIdList, "orgOrgRelationIdList");
 324  
 
 325  0
         List<OrgOrgRelation> orgOrgRelations = organizationDao.getOrgOrgRelationsByIdList(orgOrgRelationIdList);
 326  0
         return OrganizationAssembler.toOrgOrgRelationInfos(orgOrgRelations);
 327  
     }
 328  
 
 329  
     @Override
 330  
     public List<OrgOrgRelationInfo> getOrgOrgRelationsByOrg(String orgId) throws DoesNotExistException, InvalidParameterException, MissingParameterException, OperationFailedException, PermissionDeniedException {
 331  
         // TODO Flush out exceptions
 332  0
         checkForMissingParameter(orgId, "orgId");
 333  
 
 334  0
         List<OrgOrgRelation> orgOrgRelations = organizationDao.getOrgOrgRelationsByOrg(orgId);
 335  0
         return OrganizationAssembler.toOrgOrgRelationInfos(orgOrgRelations);
 336  
     }
 337  
 
 338  
     @Override
 339  
     public List<OrgOrgRelationInfo> getOrgOrgRelationsByRelatedOrg(String relatedOrgId) throws DoesNotExistException, InvalidParameterException, MissingParameterException, OperationFailedException, PermissionDeniedException {
 340  0
         checkForMissingParameter(relatedOrgId, "relatedOrgId");
 341  
 
 342  0
         List<OrgOrgRelation> orgOrgRelations = organizationDao.getOrgOrgRelationsByRelatedOrg(relatedOrgId);
 343  0
         return OrganizationAssembler.toOrgOrgRelationInfos(orgOrgRelations);
 344  
     }
 345  
 
 346  
     @Override
 347  
     public OrgPersonRelationInfo getOrgPersonRelation(String orgPersonRelationId) throws DoesNotExistException, InvalidParameterException, MissingParameterException, OperationFailedException, PermissionDeniedException {
 348  0
         checkForMissingParameter(orgPersonRelationId, "orgPersonRelationId");
 349  
 
 350  0
         OrgPersonRelation opr = organizationDao.fetch(OrgPersonRelation.class, orgPersonRelationId);
 351  0
         return OrganizationAssembler.toOrgPersonRelationInfo(opr);
 352  
     }
 353  
 
 354  
     @Override
 355  
     public OrgPersonRelationTypeInfo getOrgPersonRelationType(String orgPersonRelationTypeKey) throws DoesNotExistException, InvalidParameterException, MissingParameterException, OperationFailedException {
 356  0
         checkForMissingParameter(orgPersonRelationTypeKey, "orgPersonRelationTypeKey");
 357  
 
 358  0
         OrgPersonRelationType oprt = organizationDao.fetch(OrgPersonRelationType.class, orgPersonRelationTypeKey);
 359  0
         return OrganizationAssembler.toOrgPersonRelationTypeInfo(oprt);
 360  
     }
 361  
 
 362  
     @Override
 363  
     public List<OrgPersonRelationTypeInfo> getOrgPersonRelationTypes() throws OperationFailedException {
 364  0
         List<OrgPersonRelationType> oprts = organizationDao.find(OrgPersonRelationType.class);
 365  0
         return OrganizationAssembler.toOrgPersonRelationTypeInfos(oprts);
 366  
     }
 367  
 
 368  
     @Override
 369  
     public List<OrgPersonRelationTypeInfo> getOrgPersonRelationTypesForOrgType(String orgTypeKey) throws DoesNotExistException, InvalidParameterException, MissingParameterException, OperationFailedException {
 370  0
         checkForMissingParameter(orgTypeKey, "orgTypeKey");
 371  
 
 372  0
         List<OrgPersonRelationType> oprts = organizationDao.getOrgPersonRelationTypesForOrgType(orgTypeKey);
 373  0
         return OrganizationAssembler.toOrgPersonRelationTypeInfos(oprts);
 374  
     }
 375  
 
 376  
     @Override
 377  
     public List<OrgPersonRelationInfo> getOrgPersonRelationsByIdList(List<String> orgPersonRelationIdList) throws DoesNotExistException, InvalidParameterException, MissingParameterException, OperationFailedException, PermissionDeniedException {
 378  0
         checkForMissingParameter(orgPersonRelationIdList, "orgPersonRelationIdList");
 379  
 
 380  0
         List<OrgPersonRelation> oprts = organizationDao.getOrgPersonRelationsByIdList(orgPersonRelationIdList);
 381  0
         return OrganizationAssembler.toOrgPersonRelationInfos(oprts);
 382  
     }
 383  
 
 384  
     @Override
 385  
     public List<OrgPersonRelationInfo> getOrgPersonRelationsByOrg(String orgId) throws DoesNotExistException, InvalidParameterException, MissingParameterException, OperationFailedException, PermissionDeniedException {
 386  0
         checkForMissingParameter(orgId, "orgId");
 387  
 
 388  0
         List<OrgPersonRelation> relations = organizationDao.getAllOrgPersonRelationsByOrg(orgId);
 389  0
         return OrganizationAssembler.toOrgPersonRelationInfos(relations);
 390  
     }
 391  
 
 392  
     @Override
 393  
     public List<OrgPersonRelationInfo> getOrgPersonRelationsByPerson(String personId, String orgId) throws DoesNotExistException, InvalidParameterException, MissingParameterException, OperationFailedException, PermissionDeniedException {
 394  0
         checkForMissingParameter(personId, "personId");
 395  0
         checkForMissingParameter(orgId, "orgId");
 396  
 
 397  0
         List<OrgPersonRelation> oprts = organizationDao.getOrgPersonRelationsByPerson(personId, orgId);
 398  0
         return OrganizationAssembler.toOrgPersonRelationInfos(oprts);
 399  
     }
 400  
 
 401  
     @Override
 402  
     public OrgTypeInfo getOrgType(String orgTypeKey) throws DoesNotExistException, InvalidParameterException, MissingParameterException, OperationFailedException {
 403  0
         checkForMissingParameter(orgTypeKey, "orgTypeKey");
 404  
 
 405  0
         return OrganizationAssembler.toOrgTypeInfo(organizationDao.fetch(OrgType.class, orgTypeKey));
 406  
     }
 407  
 
 408  
     @Override
 409  
     public List<OrgTypeInfo> getOrgTypes() throws OperationFailedException {
 410  0
         return OrganizationAssembler.toOrgTypeInfos(organizationDao.find(OrgType.class));
 411  
     }
 412  
 
 413  
     @Override
 414  
     public OrgInfo getOrganization(String orgId) throws DoesNotExistException, InvalidParameterException, MissingParameterException, OperationFailedException, PermissionDeniedException {
 415  0
         checkForMissingParameter(orgId, "orgId");
 416  
 
 417  0
         return OrganizationAssembler.toOrgInfo(organizationDao.fetch(Org.class, orgId));
 418  
     }
 419  
 
 420  
     @Override
 421  
     public List<OrgInfo> getOrganizationsByIdList(List<String> orgIdList) throws DoesNotExistException, InvalidParameterException, MissingParameterException, OperationFailedException, PermissionDeniedException {
 422  0
         checkForMissingParameter(orgIdList, "orgIdList");
 423  
 
 424  0
         List<Org> orgs = this.organizationDao.getOrganizationsByIdList(orgIdList);
 425  0
         return OrganizationAssembler.toOrgInfos(orgs);
 426  
     }
 427  
 
 428  
     @Override
 429  
     public List<String> getPersonIdsForOrgByRelationType(String orgId, String orgPersonRelationTypeKey) throws DoesNotExistException, InvalidParameterException, MissingParameterException, OperationFailedException, PermissionDeniedException {
 430  0
         checkForMissingParameter(orgId, "orgId");
 431  0
         checkForMissingParameter(orgPersonRelationTypeKey, "orgPersonRelationTypeKey");
 432  
 
 433  0
         return organizationDao.getPersonIdsForOrgByRelationType(orgId, orgPersonRelationTypeKey);
 434  
     }
 435  
 
 436  
     @Override
 437  
     public List<OrgPositionRestrictionInfo> getPositionRestrictionsByOrg(String orgId) throws DataValidationErrorException, DoesNotExistException, InvalidParameterException, MissingParameterException, PermissionDeniedException, OperationFailedException {
 438  0
         checkForMissingParameter(orgId, "orgId");
 439  
 
 440  0
         List<OrgPositionRestriction> restrictions = organizationDao.getPositionRestrictionsByOrg(orgId);
 441  0
         return OrganizationAssembler.toOrgPositionRestrictionInfos(restrictions);
 442  
     }
 443  
 
 444  
     @Override
 445  
     public Boolean hasOrgOrgRelation(String orgId, String comparisonOrgId, String orgOrgRelationTypeKey) throws InvalidParameterException, MissingParameterException, OperationFailedException, PermissionDeniedException {
 446  
         // Check Missing params
 447  0
         checkForMissingParameter(orgId, "orgId");
 448  0
         checkForMissingParameter(comparisonOrgId, "comparisonOrgId");
 449  0
         checkForMissingParameter(orgOrgRelationTypeKey, "orgOrgRelationTypeKey");
 450  
 
 451  0
         boolean result = organizationDao.hasOrgOrgRelation(orgId, comparisonOrgId, orgOrgRelationTypeKey);
 452  0
         return Boolean.valueOf(result);
 453  
     }
 454  
 
 455  
     @Override
 456  
     public Boolean hasOrgPersonRelation(String orgId, String personId, String orgPersonRelationTypeKey) throws InvalidParameterException, MissingParameterException, OperationFailedException, PermissionDeniedException {
 457  0
         checkForMissingParameter(orgId, "orgId");
 458  0
         checkForMissingParameter(personId, "personId");
 459  0
         checkForMissingParameter(orgPersonRelationTypeKey, "orgPersonRelationTypeKey");
 460  
 
 461  0
         return organizationDao.hasOrgPersonRelation(orgId, personId, orgPersonRelationTypeKey);
 462  
     }
 463  
 
 464  
     @Override
 465  
     public Boolean isDescendant(String orgId, String descendantOrgId, String orgHierarchy) throws InvalidParameterException, MissingParameterException, OperationFailedException, PermissionDeniedException {
 466  0
         checkForMissingParameter(orgId, "orgId");
 467  0
         checkForMissingParameter(descendantOrgId, "descendantOrgId");
 468  0
         checkForMissingParameter(orgHierarchy, "orgHierarchy");
 469  
 
 470  
         // get ancestors of the descendant, as it will be more efficient in most cases
 471  0
         List<String> ancestors = organizationDao.getAllAncestors(descendantOrgId, orgHierarchy);
 472  0
         boolean result = ancestors.contains(orgId);
 473  0
         return Boolean.valueOf(result);
 474  
     }
 475  
 
 476  
     @Override
 477  
     @Transactional(readOnly = false)
 478  
     public StatusInfo removeOrgOrgRelation(String orgOrgRelationId) throws DoesNotExistException, InvalidParameterException, MissingParameterException, OperationFailedException, PermissionDeniedException {
 479  0
         checkForMissingParameter(orgOrgRelationId, "orgOrgRelationId");
 480  
 
 481  0
         organizationDao.delete(OrgOrgRelation.class, orgOrgRelationId);
 482  0
         return new StatusInfo();
 483  
     }
 484  
 
 485  
     @Override
 486  
     @Transactional(readOnly = false)
 487  
     public StatusInfo removeOrgPersonRelation(String orgPersonRelationId) throws DoesNotExistException, InvalidParameterException, MissingParameterException, OperationFailedException, PermissionDeniedException {
 488  0
         checkForMissingParameter(orgPersonRelationId, "orgPersonRelationId");
 489  
 
 490  0
         organizationDao.delete(OrgPersonRelation.class, orgPersonRelationId);
 491  0
         return new StatusInfo();
 492  
     }
 493  
 
 494  
     @Override
 495  
     @Transactional(readOnly = false)
 496  
     public StatusInfo removePositionRestrictionFromOrg(String orgId, String orgPersonRelationTypeKey) throws DoesNotExistException, InvalidParameterException, MissingParameterException, OperationFailedException, PermissionDeniedException {
 497  
 
 498  0
         checkForMissingParameter(orgId, "orgId");
 499  0
         checkForMissingParameter(orgPersonRelationTypeKey, "orgPersonRelationTypeKey");
 500  0
         OrgPositionRestriction opr = null;
 501  
         try {
 502  0
             opr = organizationDao.getPositionRestrictionByOrgAndPersonRelationTypeKey(orgId, orgPersonRelationTypeKey);
 503  0
             if (opr == null) {
 504  0
                 throw new DoesNotExistException();
 505  
             }
 506  0
         } catch (NoResultException e) {
 507  0
             throw new DoesNotExistException();
 508  0
         }
 509  0
         organizationDao.delete(opr);
 510  0
         return new StatusInfo();
 511  
     }
 512  
 
 513  
     @Override
 514  
     @Transactional(readOnly = false)
 515  
     public OrgOrgRelationInfo updateOrgOrgRelation(String orgOrgRelationId, OrgOrgRelationInfo orgOrgRelationInfo) throws DataValidationErrorException, DoesNotExistException, InvalidParameterException, MissingParameterException, OperationFailedException, PermissionDeniedException, VersionMismatchException {
 516  
         // Check Missing params
 517  0
         checkForMissingParameter(orgOrgRelationId, "orgOrgRelationId");
 518  0
         checkForMissingParameter(orgOrgRelationId, "orgOrgRelationId");
 519  
 
 520  
         // Set all the values on OrgOrgRelationInfo
 521  0
         orgOrgRelationInfo.setId(orgOrgRelationId);
 522  
 
 523  0
         OrgOrgRelation orgOrgRelation = null;
 524  
 
 525  
         // Update the persistence entity from the Info
 526  0
         orgOrgRelation = OrganizationAssembler.toOrgOrgRelation(true, orgOrgRelationInfo, organizationDao);
 527  
 
 528  
         // Update the orgOrgRelation
 529  0
         OrgOrgRelation updatedOrgOrgRelation = organizationDao.update(orgOrgRelation);
 530  
 
 531  
         // Copy back to an OrgOrgRelationInfo and return
 532  0
         OrgOrgRelationInfo updatedOrgOrgRelationInfo = OrganizationAssembler.toOrgOrgRelationInfo(updatedOrgOrgRelation);
 533  0
         return updatedOrgOrgRelationInfo;
 534  
 
 535  
     }
 536  
 
 537  
     @Override
 538  
     @Transactional(readOnly = false)
 539  
     public OrgPersonRelationInfo updateOrgPersonRelation(String orgPersonRelationId, OrgPersonRelationInfo orgPersonRelationInfo) throws DataValidationErrorException, DoesNotExistException, InvalidParameterException, MissingParameterException, OperationFailedException, PermissionDeniedException, VersionMismatchException {
 540  
         // Check Missing params
 541  0
         checkForMissingParameter(orgPersonRelationId, "orgPersonRelationId");
 542  0
         checkForMissingParameter(orgPersonRelationInfo, "orgPersonRelationInfo");
 543  
 
 544  
         // Make sure that only valid org person relations are done
 545  0
         if (!organizationDao.validatePositionRestriction(orgPersonRelationInfo.getOrgId(), orgPersonRelationInfo.getType())) {
 546  0
             throw new InvalidParameterException("There is no Position for this relationship");
 547  
         }
 548  
 
 549  
         // Set all the values on OrgPersonRelationInfo
 550  0
         orgPersonRelationInfo.setId(orgPersonRelationId);
 551  
 
 552  0
         OrgPersonRelation orgPersonRelation = null;
 553  
 
 554  
         // Update persistence entity from the orgInfo
 555  0
         orgPersonRelation = OrganizationAssembler.toOrgPersonRelation(true, orgPersonRelationInfo, organizationDao);
 556  
 
 557  
         // Update the orgPersonRelation
 558  0
         orgPersonRelation = organizationDao.update(orgPersonRelation);
 559  
 
 560  
         // Copy back to an orgPersonRelationInfo and return
 561  0
         OrgPersonRelationInfo createdOrgPersonRelationInfo = OrganizationAssembler.toOrgPersonRelationInfo(orgPersonRelation);
 562  0
         return createdOrgPersonRelationInfo;
 563  
     }
 564  
 
 565  
     @Override
 566  
     @Transactional(readOnly = false)
 567  
     public OrgInfo updateOrganization(String orgId, OrgInfo orgInfo) throws DataValidationErrorException, DoesNotExistException, InvalidParameterException, MissingParameterException, OperationFailedException, PermissionDeniedException, VersionMismatchException {
 568  
 
 569  
         // Check Missing params
 570  0
         checkForMissingParameter(orgId, "orgId");
 571  0
         checkForMissingParameter(orgInfo, "orgInfo");
 572  
 
 573  
         // Set all the values on orgInfo
 574  0
         orgInfo.setId(orgId);
 575  
 
 576  0
         Org org = null;
 577  
 
 578  
         // Update persistence entity from the orgInfo
 579  0
         org = OrganizationAssembler.toOrg(true, orgInfo, organizationDao);
 580  
 
 581  
         // Update the org
 582  0
         Org updatedOrg = organizationDao.update(org);
 583  
 
 584  
         // Copy back to an orgInfo and return
 585  0
         OrgInfo updatedOrgInfo = OrganizationAssembler.toOrgInfo(updatedOrg);
 586  0
         return updatedOrgInfo;
 587  
 
 588  
     }
 589  
 
 590  
     @Override
 591  
     @Transactional(readOnly = false)
 592  
     public OrgPositionRestrictionInfo updatePositionRestrictionForOrg(String orgId, String orgPersonRelationTypeKey, OrgPositionRestrictionInfo orgPositionRestrictionInfo) throws DataValidationErrorException, DoesNotExistException, InvalidParameterException, MissingParameterException, OperationFailedException, PermissionDeniedException, VersionMismatchException {
 593  
 
 594  
         // Check Missing params
 595  0
         checkForMissingParameter(orgId, "orgId");
 596  0
         checkForMissingParameter(orgPersonRelationTypeKey, "orgPersonRelationTypeKey");
 597  0
         checkForMissingParameter(orgPositionRestrictionInfo, "orgPositionRestrictionInfo");
 598  
 
 599  
         // Set all the values on OrgOrgRelationInfo
 600  0
         orgPositionRestrictionInfo.setOrgId(orgId);
 601  0
         orgPositionRestrictionInfo.setOrgPersonRelationTypeKey(orgPersonRelationTypeKey);
 602  
 
 603  0
         OrgPositionRestriction orgPositionRestriction = null;
 604  
 
 605  
         // Update persistence entity from the Info
 606  0
         orgPositionRestriction = OrganizationAssembler.toOrgPositionRestriction(true, orgPositionRestrictionInfo, organizationDao);
 607  
 
 608  
         // Update the positionRestriction
 609  0
         OrgPositionRestriction updated = organizationDao.update(orgPositionRestriction);
 610  
 
 611  
         // Copy back to an OrgOrgRelationInfo and return
 612  0
         OrgPositionRestrictionInfo updatedOrgPositionRestrictionInfo = OrganizationAssembler.toOrgPositionRestrictionInfo(updated);
 613  0
         return updatedOrgPositionRestrictionInfo;
 614  
     }
 615  
 
 616  
     @Override
 617  
     public List<ValidationResultInfo> validateOrg(String validationType, OrgInfo orgInfo) throws DoesNotExistException, InvalidParameterException, MissingParameterException, OperationFailedException {
 618  
 
 619  0
         checkForMissingParameter(validationType, "validationType");
 620  0
         checkForMissingParameter(orgInfo, "orgInfo");
 621  
 
 622  
         // FIXME redo validation here and for all calls to create/update
 623  
         // return validator.validateTypeStateObject(orgInfo, getObjectStructure("orgInfo"));
 624  
 
 625  0
         return new ArrayList<ValidationResultInfo>(0);
 626  
     }
 627  
 
 628  
     @Override
 629  
     public List<ValidationResultInfo> validateOrgOrgRelation(String validationType, OrgOrgRelationInfo orgOrgRelationInfo) throws DoesNotExistException, InvalidParameterException, MissingParameterException, OperationFailedException {
 630  0
         checkForMissingParameter(validationType, "validationType");
 631  0
         checkForMissingParameter(orgOrgRelationInfo, "orgOrgRelationInfo");
 632  
 
 633  0
         List<ValidationResultInfo> valResults = validator.validateTypeStateObject(orgOrgRelationInfo, getObjectStructure("orgOrgRelationInfo"));
 634  0
         return valResults;
 635  
     }
 636  
 
 637  
     @Override
 638  
     public List<ValidationResultInfo> validateOrgPersonRelation(String validationType, OrgPersonRelationInfo orgPersonRelationInfo) throws DoesNotExistException, InvalidParameterException, MissingParameterException, OperationFailedException {
 639  0
         checkForMissingParameter(validationType, "validationType");
 640  
 
 641  0
         List<ValidationResultInfo> valResults = validator.validateTypeStateObject(orgPersonRelationInfo, getObjectStructure("orgPersonRelationInfo"));
 642  0
         return valResults;
 643  
 
 644  
     }
 645  
 
 646  
     @Override
 647  
     public List<ValidationResultInfo> validateOrgPositionRestriction(String validationType, OrgPositionRestrictionInfo orgPositionRestrictionInfo) throws DoesNotExistException, InvalidParameterException, MissingParameterException, OperationFailedException {
 648  0
         checkForMissingParameter(validationType, "validationType");
 649  0
         checkForMissingParameter(orgPositionRestrictionInfo, "orgPositionRestrictionInfo");
 650  
 
 651  0
         return null;
 652  
     }
 653  
 
 654  
     @Override
 655  
     public SearchCriteriaTypeInfo getSearchCriteriaType(String searchCriteriaTypeKey) throws DoesNotExistException, InvalidParameterException, MissingParameterException, OperationFailedException {
 656  
 
 657  0
         return searchManager.getSearchCriteriaType(searchCriteriaTypeKey);
 658  
     }
 659  
 
 660  
     @Override
 661  
     public List<SearchCriteriaTypeInfo> getSearchCriteriaTypes() throws OperationFailedException {
 662  0
         return searchManager.getSearchCriteriaTypes();
 663  
     }
 664  
 
 665  
     @Override
 666  
     public SearchResultTypeInfo getSearchResultType(String searchResultTypeKey) throws DoesNotExistException, InvalidParameterException, MissingParameterException, OperationFailedException {
 667  0
         checkForMissingParameter(searchResultTypeKey, "searchResultTypeKey");
 668  0
         return searchManager.getSearchResultType(searchResultTypeKey);
 669  
     }
 670  
 
 671  
     @Override
 672  
     public List<SearchResultTypeInfo> getSearchResultTypes() throws OperationFailedException {
 673  0
         return searchManager.getSearchResultTypes();
 674  
     }
 675  
 
 676  
     @Override
 677  
     public SearchTypeInfo getSearchType(String searchTypeKey) throws DoesNotExistException, InvalidParameterException, MissingParameterException, OperationFailedException {
 678  0
         checkForMissingParameter(searchTypeKey, "searchTypeKey");
 679  0
         return searchManager.getSearchType(searchTypeKey);
 680  
     }
 681  
 
 682  
     @Override
 683  
     public List<SearchTypeInfo> getSearchTypes() throws OperationFailedException {
 684  0
         return searchManager.getSearchTypes();
 685  
     }
 686  
 
 687  
     @Override
 688  
     public List<SearchTypeInfo> getSearchTypesByCriteria(String searchCriteriaTypeKey) throws DoesNotExistException, InvalidParameterException, MissingParameterException, OperationFailedException {
 689  0
         checkForMissingParameter(searchCriteriaTypeKey, "searchCriteriaTypeKey");
 690  0
         return searchManager.getSearchTypesByCriteria(searchCriteriaTypeKey);
 691  
     }
 692  
 
 693  
     @Override
 694  
     public List<SearchTypeInfo> getSearchTypesByResult(String searchResultTypeKey) throws DoesNotExistException, InvalidParameterException, MissingParameterException, OperationFailedException {
 695  0
         checkForMissingParameter(searchResultTypeKey, "searchResultTypeKey");
 696  0
         return searchManager.getSearchTypesByResult(searchResultTypeKey);
 697  
     }
 698  
 
 699  
     public OrganizationDao getOrganizationDao() {
 700  0
         return organizationDao;
 701  
     }
 702  
 
 703  
     public void setOrganizationDao(OrganizationDao organizationDao) {
 704  0
         this.organizationDao = organizationDao;
 705  0
     }
 706  
 
 707  
     @Override
 708  
     public ObjectStructure getObjectStructure(String objectTypeKey) {
 709  0
         return dictionaryServiceDelegate.getObjectStructure(objectTypeKey);
 710  
     }
 711  
 
 712  
     @Override
 713  
     public List<String> getObjectTypes() {
 714  0
         return dictionaryServiceDelegate.getObjectTypes();
 715  
     }
 716  
 
 717  
     @Override
 718  
     public List<OrgTreeInfo> getOrgTree(String rootOrgId, String orgHierarchyId, int maxLevels) throws DoesNotExistException, InvalidParameterException, MissingParameterException, OperationFailedException, PermissionDeniedException {
 719  0
         checkForMissingParameter(rootOrgId, "rootOrgId");
 720  0
         checkForMissingParameter(orgHierarchyId, "orgHierarchyId");
 721  
 
 722  0
         Set<OrgTreeInfo> results = new HashSet<OrgTreeInfo>();
 723  0
         Org rootOrg = organizationDao.fetch(Org.class, rootOrgId);
 724  0
         OrgTreeInfo root = new OrgTreeInfo(rootOrgId, null, rootOrg.getLongName());
 725  0
         root.setPositions(this.organizationDao.getOrgMemebershipCount(root.getOrgId()));
 726  0
         root.setOrgHierarchyId(orgHierarchyId);
 727  0
         results.add(root);
 728  0
         if (maxLevels >= 0) {
 729  0
             results.addAll(parseOrgTree(rootOrgId, orgHierarchyId, maxLevels, 0));
 730  
         }
 731  0
         return new ArrayList<OrgTreeInfo>(results);
 732  
     }
 733  
 
 734  
     private List<OrgTreeInfo> parseOrgTree(String rootOrgId, String orgHierarchyId, int maxLevels, int currentLevel) {
 735  0
         List<OrgTreeInfo> results = new ArrayList<OrgTreeInfo>();
 736  0
         if (maxLevels == 0 || currentLevel < maxLevels) {
 737  0
             List<OrgTreeInfo> orgTreeInfos = this.organizationDao.getOrgTreeInfo(rootOrgId, orgHierarchyId);
 738  0
             for (OrgTreeInfo orgTreeInfo : orgTreeInfos) {
 739  0
                 orgTreeInfo.setPositions(this.organizationDao.getOrgMemebershipCount(orgTreeInfo.getOrgId()));
 740  0
                 results.addAll(parseOrgTree(orgTreeInfo.getOrgId(), orgHierarchyId, maxLevels, currentLevel + 1));
 741  
             }
 742  0
             results.addAll(orgTreeInfos);
 743  
         }
 744  0
         return results;
 745  
     }
 746  
 
 747  
     /**
 748  
      * Check for missing parameter and throw localized exception if missing
 749  
      * 
 750  
      * @param param
 751  
      * @param parameter
 752  
      *            name
 753  
      * @throws MissingParameterException
 754  
      */
 755  
     private void checkForMissingParameter(Object param, String paramName) throws MissingParameterException {
 756  0
         if (param == null) {
 757  0
             throw new MissingParameterException(paramName + " can not be null");
 758  
         }
 759  0
     }
 760  
 
 761  
     public SearchManager getSearchManager() {
 762  0
         return searchManager;
 763  
     }
 764  
 
 765  
     public void setSearchManager(SearchManager searchManager) {
 766  0
         this.searchManager = searchManager;
 767  0
     }
 768  
 
 769  
     public DictionaryService getDictionaryServiceDelegate() {
 770  0
         return dictionaryServiceDelegate;
 771  
     }
 772  
 
 773  
     public void setDictionaryServiceDelegate(DictionaryService dictionaryServiceDelegate) {
 774  0
         this.dictionaryServiceDelegate = dictionaryServiceDelegate;
 775  0
     }
 776  
 
 777  
     public Validator getValidator() {
 778  0
         return validator;
 779  
     }
 780  
 
 781  
     public void setValidator(Validator validator) {
 782  0
         this.validator = validator;
 783  0
     }
 784  
 
 785  
     @Override
 786  
     public SearchResult search(SearchRequest searchRequest) throws MissingParameterException {
 787  0
         checkForMissingParameter(searchRequest, "searchRequest");
 788  
 
 789  
         // Look for a Organization Search instance.
 790  0
         if (searchOperations != null) {
 791  0
             OrganizationSearch search = searchOperations.get(searchRequest.getSearchKey());
 792  0
             if (search != null) {
 793  0
                 return search.search(searchRequest);
 794  
             }
 795  
         }
 796  
 
 797  0
         return searchManager.search(searchRequest, organizationDao);
 798  
     }
 799  
 
 800  
 }