Coverage Report - org.kuali.student.lum.lu.service.impl.LuServiceImpl
 
Classes in this File Line Coverage Branch Coverage Complexity
LuServiceImpl
0%
0/1585
0%
0/654
3.828
LuServiceImpl$SearchResultRowComparator
0%
0/22
0%
0/14
3.828
 
 1  
 /**
 2  
  * Copyright 2010 The Kuali Foundation Licensed under the
 3  
  * Educational Community License, Version 2.0 (the "License"); you may
 4  
  * not use this file except in compliance with the License. You may
 5  
  * obtain a copy of the License at
 6  
  *
 7  
  * http://www.osedu.org/licenses/ECL-2.0
 8  
  *
 9  
  * Unless required by applicable law or agreed to in writing,
 10  
  * software distributed under the License is distributed on an "AS IS"
 11  
  * BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 12  
  * or implied. See the License for the specific language governing
 13  
  * permissions and limitations under the License.
 14  
  */
 15  
 
 16  
 package org.kuali.student.lum.lu.service.impl;
 17  
 
 18  
 import java.util.ArrayList;
 19  
 import java.util.Collections;
 20  
 import java.util.Comparator;
 21  
 import java.util.Date;
 22  
 import java.util.HashMap;
 23  
 import java.util.HashSet;
 24  
 import java.util.Iterator;
 25  
 import java.util.List;
 26  
 import java.util.Map;
 27  
 import java.util.Map.Entry;
 28  
 import java.util.Set;
 29  
 
 30  
 import javax.jws.WebService;
 31  
 import javax.persistence.NoResultException;
 32  
 
 33  
 import org.apache.log4j.Logger;
 34  
 import org.kuali.student.common.dictionary.dto.ObjectStructureDefinition;
 35  
 import org.kuali.student.common.dictionary.service.DictionaryService;
 36  
 import org.kuali.student.common.dto.CurrencyAmountInfo;
 37  
 import org.kuali.student.common.dto.DtoConstants;
 38  
 import org.kuali.student.common.dto.StatusInfo;
 39  
 import org.kuali.student.common.entity.Amount;
 40  
 import org.kuali.student.common.entity.TimeAmount;
 41  
 import org.kuali.student.common.entity.Version;
 42  
 import org.kuali.student.common.entity.VersionEntity;
 43  
 import org.kuali.student.common.exceptions.AlreadyExistsException;
 44  
 import org.kuali.student.common.exceptions.CircularRelationshipException;
 45  
 import org.kuali.student.common.exceptions.DataValidationErrorException;
 46  
 import org.kuali.student.common.exceptions.DependentObjectsExistException;
 47  
 import org.kuali.student.common.exceptions.DoesNotExistException;
 48  
 import org.kuali.student.common.exceptions.IllegalVersionSequencingException;
 49  
 import org.kuali.student.common.exceptions.InvalidParameterException;
 50  
 import org.kuali.student.common.exceptions.MissingParameterException;
 51  
 import org.kuali.student.common.exceptions.OperationFailedException;
 52  
 import org.kuali.student.common.exceptions.PermissionDeniedException;
 53  
 import org.kuali.student.common.exceptions.UnsupportedActionException;
 54  
 import org.kuali.student.common.exceptions.VersionMismatchException;
 55  
 import org.kuali.student.common.search.dto.SearchCriteriaTypeInfo;
 56  
 import org.kuali.student.common.search.dto.SearchParam;
 57  
 import org.kuali.student.common.search.dto.SearchRequest;
 58  
 import org.kuali.student.common.search.dto.SearchResult;
 59  
 import org.kuali.student.common.search.dto.SearchResultCell;
 60  
 import org.kuali.student.common.search.dto.SearchResultRow;
 61  
 import org.kuali.student.common.search.dto.SearchResultTypeInfo;
 62  
 import org.kuali.student.common.search.dto.SearchTypeInfo;
 63  
 import org.kuali.student.common.search.service.SearchDispatcher;
 64  
 import org.kuali.student.common.search.service.SearchManager;
 65  
 import org.kuali.student.common.validation.dto.ValidationResultInfo;
 66  
 import org.kuali.student.common.validator.Validator;
 67  
 import org.kuali.student.common.validator.ValidatorFactory;
 68  
 import org.kuali.student.common.versionmanagement.dto.VersionDisplayInfo;
 69  
 import org.kuali.student.lum.lu.dao.LuDao;
 70  
 import org.kuali.student.lum.lu.dto.AccreditationInfo;
 71  
 import org.kuali.student.lum.lu.dto.AdminOrgInfo;
 72  
 import org.kuali.student.lum.lu.dto.AffiliatedOrgInfo;
 73  
 import org.kuali.student.lum.lu.dto.CluCluRelationInfo;
 74  
 import org.kuali.student.lum.lu.dto.CluFeeRecordInfo;
 75  
 import org.kuali.student.lum.lu.dto.CluIdentifierInfo;
 76  
 import org.kuali.student.lum.lu.dto.CluInfo;
 77  
 import org.kuali.student.lum.lu.dto.CluInstructorInfo;
 78  
 import org.kuali.student.lum.lu.dto.CluLoRelationInfo;
 79  
 import org.kuali.student.lum.lu.dto.CluLoRelationTypeInfo;
 80  
 import org.kuali.student.lum.lu.dto.CluPublicationInfo;
 81  
 import org.kuali.student.lum.lu.dto.CluResultInfo;
 82  
 import org.kuali.student.lum.lu.dto.CluResultTypeInfo;
 83  
 import org.kuali.student.lum.lu.dto.CluSetInfo;
 84  
 import org.kuali.student.lum.lu.dto.CluSetTreeViewInfo;
 85  
 import org.kuali.student.lum.lu.dto.CluSetTypeInfo;
 86  
 import org.kuali.student.lum.lu.dto.DeliveryMethodTypeInfo;
 87  
 import org.kuali.student.lum.lu.dto.FieldInfo;
 88  
 import org.kuali.student.lum.lu.dto.InstructionalFormatTypeInfo;
 89  
 import org.kuali.student.lum.lu.dto.LuCodeInfo;
 90  
 import org.kuali.student.lum.lu.dto.LuCodeTypeInfo;
 91  
 import org.kuali.student.lum.lu.dto.LuLuRelationTypeInfo;
 92  
 import org.kuali.student.lum.lu.dto.LuPublicationTypeInfo;
 93  
 import org.kuali.student.lum.lu.dto.LuTypeInfo;
 94  
 import org.kuali.student.lum.lu.dto.LuiInfo;
 95  
 import org.kuali.student.lum.lu.dto.LuiLuiRelationInfo;
 96  
 import org.kuali.student.lum.lu.dto.MembershipQueryInfo;
 97  
 import org.kuali.student.lum.lu.dto.ResultOptionInfo;
 98  
 import org.kuali.student.lum.lu.dto.ResultUsageTypeInfo;
 99  
 import org.kuali.student.lum.lu.entity.Clu;
 100  
 import org.kuali.student.lum.lu.entity.CluAccounting;
 101  
 import org.kuali.student.lum.lu.entity.CluAccountingAttribute;
 102  
 import org.kuali.student.lum.lu.entity.CluAccreditation;
 103  
 import org.kuali.student.lum.lu.entity.CluAccreditationAttribute;
 104  
 import org.kuali.student.lum.lu.entity.CluAdminOrg;
 105  
 import org.kuali.student.lum.lu.entity.CluAdminOrgAttribute;
 106  
 import org.kuali.student.lum.lu.entity.CluAtpTypeKey;
 107  
 import org.kuali.student.lum.lu.entity.CluAttribute;
 108  
 import org.kuali.student.lum.lu.entity.CluCampusLocation;
 109  
 import org.kuali.student.lum.lu.entity.CluCluRelation;
 110  
 import org.kuali.student.lum.lu.entity.CluCluRelationAttribute;
 111  
 import org.kuali.student.lum.lu.entity.CluFee;
 112  
 import org.kuali.student.lum.lu.entity.CluIdentifier;
 113  
 import org.kuali.student.lum.lu.entity.CluInstructor;
 114  
 import org.kuali.student.lum.lu.entity.CluInstructorAttribute;
 115  
 import org.kuali.student.lum.lu.entity.CluLoRelation;
 116  
 import org.kuali.student.lum.lu.entity.CluLoRelationAttribute;
 117  
 import org.kuali.student.lum.lu.entity.CluLoRelationType;
 118  
 import org.kuali.student.lum.lu.entity.CluPublication;
 119  
 import org.kuali.student.lum.lu.entity.CluPublicationAttribute;
 120  
 import org.kuali.student.lum.lu.entity.CluPublicationType;
 121  
 import org.kuali.student.lum.lu.entity.CluPublicationVariant;
 122  
 import org.kuali.student.lum.lu.entity.CluResult;
 123  
 import org.kuali.student.lum.lu.entity.CluResultType;
 124  
 import org.kuali.student.lum.lu.entity.CluSet;
 125  
 import org.kuali.student.lum.lu.entity.CluSetAttribute;
 126  
 import org.kuali.student.lum.lu.entity.CluSetJoinVersionIndClu;
 127  
 import org.kuali.student.lum.lu.entity.CluSetType;
 128  
 import org.kuali.student.lum.lu.entity.DeliveryMethodType;
 129  
 import org.kuali.student.lum.lu.entity.InstructionalFormatType;
 130  
 import org.kuali.student.lum.lu.entity.LuCode;
 131  
 import org.kuali.student.lum.lu.entity.LuCodeAttribute;
 132  
 import org.kuali.student.lum.lu.entity.LuCodeType;
 133  
 import org.kuali.student.lum.lu.entity.LuLuRelationType;
 134  
 import org.kuali.student.lum.lu.entity.LuPublicationType;
 135  
 import org.kuali.student.lum.lu.entity.LuRichText;
 136  
 import org.kuali.student.lum.lu.entity.LuType;
 137  
 import org.kuali.student.lum.lu.entity.Lui;
 138  
 import org.kuali.student.lum.lu.entity.LuiAttribute;
 139  
 import org.kuali.student.lum.lu.entity.LuiLuiRelation;
 140  
 import org.kuali.student.lum.lu.entity.LuiLuiRelationAttribute;
 141  
 import org.kuali.student.lum.lu.entity.MembershipQuery;
 142  
 import org.kuali.student.lum.lu.entity.ResultOption;
 143  
 import org.kuali.student.lum.lu.entity.ResultUsageType;
 144  
 import org.kuali.student.lum.lu.service.LuService;
 145  
 import org.kuali.student.lum.lu.service.LuServiceConstants;
 146  
 import org.springframework.beans.BeanUtils;
 147  
 import org.springframework.transaction.annotation.Transactional;
 148  
 
 149  
 @WebService(endpointInterface = "org.kuali.student.lum.lu.service.LuService", serviceName = "LuService", portName = "LuService", targetNamespace = "http://student.kuali.org/wsdl/lu")
 150  
 @Transactional(readOnly=true,noRollbackFor={DoesNotExistException.class},rollbackFor={Throwable.class})
 151  0
 public class LuServiceImpl implements LuService {
 152  
 
 153  
         private static final String SEARCH_KEY_DEPENDENCY_ANALYSIS = "lu.search.dependencyAnalysis";
 154  
         private static final String SEARCH_KEY_BROWSE_PROGRAM = "lu.search.browseProgram";
 155  
         private static final String SEARCH_KEY_BROWSE_VARIATIONS = "lu.search.browseVariations";
 156  
         private static final String SEARCH_KEY_RESULT_COMPONENT = "lrc.search.resultComponent";
 157  
         
 158  0
         final Logger logger = Logger.getLogger(LuServiceImpl.class);
 159  
 
 160  
         private LuDao luDao;
 161  
         private SearchManager searchManager;
 162  
         private SearchDispatcher searchDispatcher;
 163  
         private DictionaryService dictionaryServiceDelegate;
 164  
         private ValidatorFactory validatorFactory;
 165  
 
 166  
         public void setSearchManager(SearchManager searchManager) {
 167  0
                 this.searchManager = searchManager;
 168  0
         }
 169  
 
 170  
         public void setDictionaryServiceDelegate(
 171  
                         DictionaryService dictionaryServiceDelegate) {
 172  0
                 this.dictionaryServiceDelegate = dictionaryServiceDelegate;
 173  0
         }
 174  
 
 175  
         public DictionaryService getDictionaryServiceDelegate() {
 176  0
                 return dictionaryServiceDelegate;
 177  
         }
 178  
 
 179  
 
 180  
         /**************************************************************************
 181  
          * SETUP OPERATION *
 182  
          **************************************************************************/
 183  
 
 184  
         @Override
 185  
         public List<DeliveryMethodTypeInfo> getDeliveryMethodTypes()
 186  
                         throws OperationFailedException {
 187  0
                 return LuServiceAssembler.toDeliveryMethodTypeInfos(luDao
 188  
                                 .find(DeliveryMethodType.class));
 189  
         }
 190  
 
 191  
         @Override
 192  
         public DeliveryMethodTypeInfo getDeliveryMethodType(
 193  
                         String deliveryMethodTypeKey) throws DoesNotExistException,
 194  
                         InvalidParameterException, MissingParameterException,
 195  
                         OperationFailedException {
 196  
 
 197  0
                 checkForMissingParameter(deliveryMethodTypeKey, "deliveryMethodTypeKey");
 198  
 
 199  0
                 return LuServiceAssembler.toDeliveryMethodTypeInfo(luDao.fetch(
 200  
                                 DeliveryMethodType.class, deliveryMethodTypeKey));
 201  
         }
 202  
 
 203  
         @Override
 204  
         public List<InstructionalFormatTypeInfo> getInstructionalFormatTypes()
 205  
                         throws OperationFailedException {
 206  0
                 return LuServiceAssembler.toInstructionalFormatTypeInfos(luDao
 207  
                                 .find(InstructionalFormatType.class));
 208  
         }
 209  
 
 210  
         @Override
 211  
         public InstructionalFormatTypeInfo getInstructionalFormatType(
 212  
                         String instructionalFormatTypeKey) throws DoesNotExistException,
 213  
                         InvalidParameterException, MissingParameterException,
 214  
                         OperationFailedException {
 215  0
                 checkForMissingParameter(instructionalFormatTypeKey,
 216  
                                 "instructionalFormatTypeKey");
 217  
 
 218  0
                 return LuServiceAssembler.toInstructionalFormatTypeInfo(luDao.fetch(
 219  
                                 InstructionalFormatType.class, instructionalFormatTypeKey));
 220  
         }
 221  
 
 222  
         @Override
 223  
         public List<LuTypeInfo> getLuTypes() throws OperationFailedException {
 224  0
                 return LuServiceAssembler.toLuTypeInfos(luDao.find(LuType.class));
 225  
         }
 226  
 
 227  
         @Override
 228  
         public LuTypeInfo getLuType(String luTypeKey) throws DoesNotExistException,
 229  
                         InvalidParameterException, MissingParameterException,
 230  
                         OperationFailedException {
 231  0
                 checkForMissingParameter(luTypeKey, "luTypeKey");
 232  
 
 233  0
                 return LuServiceAssembler.toLuTypeInfo(luDao.fetch(LuType.class,
 234  
                                 luTypeKey));
 235  
         }
 236  
 
 237  
         @Override
 238  
         public LuCodeTypeInfo getLuCodeType(String luCodeTypeKey)
 239  
                         throws DoesNotExistException, InvalidParameterException,
 240  
                         MissingParameterException, OperationFailedException {
 241  0
                 checkForMissingParameter(luCodeTypeKey, "luCodeTypeKey");
 242  0
                 return LuServiceAssembler.toLuCodeTypeInfo(luDao.fetch(
 243  
                                 LuCodeType.class, luCodeTypeKey));
 244  
         }
 245  
 
 246  
         @Override
 247  
         public List<LuCodeTypeInfo> getLuCodeTypes()
 248  
                         throws OperationFailedException {
 249  0
                 return LuServiceAssembler.toLuCodeTypeInfos(luDao
 250  
                                 .find(LuCodeType.class));
 251  
         }
 252  
 
 253  
         @Override
 254  
         public List<LuLuRelationTypeInfo> getLuLuRelationTypes()
 255  
                         throws OperationFailedException {
 256  0
                 return LuServiceAssembler.toLuLuRelationTypeInfos(luDao
 257  
                                 .find(LuLuRelationType.class));
 258  
         }
 259  
 
 260  
         @Override
 261  
         public LuLuRelationTypeInfo getLuLuRelationType(String luLuRelationTypeKey)
 262  
                         throws OperationFailedException, MissingParameterException,
 263  
                         DoesNotExistException {
 264  0
                 checkForMissingParameter(luLuRelationTypeKey, "luLuRelationTypeKey");
 265  
 
 266  0
                 LuLuRelationType luLuRelationType = luDao.fetch(LuLuRelationType.class,
 267  
                                 luLuRelationTypeKey);
 268  0
                 return LuServiceAssembler.toLuLuRelationTypeInfo(luLuRelationType);
 269  
         }
 270  
 
 271  
         @Override
 272  
         public List<String> getAllowedLuLuRelationTypesForLuType(String luTypeKey,
 273  
                         String relatedLuTypeKey) throws DoesNotExistException,
 274  
                         InvalidParameterException, MissingParameterException,
 275  
                         OperationFailedException {
 276  0
                 checkForMissingParameter(luTypeKey, "luTypeKey");
 277  0
                 checkForMissingParameter(relatedLuTypeKey, "relatedLuTypeKey");
 278  
 
 279  0
                 return luDao.getAllowedLuLuRelationTypesForLuType(luTypeKey,
 280  
                                 relatedLuTypeKey);
 281  
         }
 282  
 
 283  
         @Override
 284  
         public List<LuPublicationTypeInfo> getLuPublicationTypes()
 285  
                         throws OperationFailedException {
 286  0
                 return LuServiceAssembler.toLuPublicationTypeInfos(luDao
 287  
                                 .find(LuPublicationType.class));
 288  
         }
 289  
 
 290  
         @Override
 291  
         public LuPublicationTypeInfo getLuPublicationType(
 292  
                         String luPublicationTypeKey) throws DoesNotExistException,
 293  
                         InvalidParameterException, MissingParameterException,
 294  
                         OperationFailedException {
 295  0
                 checkForMissingParameter(luPublicationTypeKey, "luPublicationTypeKey");
 296  
 
 297  0
                 return LuServiceAssembler.toLuPublicationTypeInfo(luDao.fetch(
 298  
                                 LuPublicationType.class, luPublicationTypeKey));
 299  
         }
 300  
 
 301  
         @Override
 302  
         public List<String> getLuPublicationTypesForLuType(String luTypeKey)
 303  
                         throws DoesNotExistException, InvalidParameterException,
 304  
                         MissingParameterException, OperationFailedException {
 305  0
                 throw new UnsupportedOperationException("getLuPublicationTypesForLuType");
 306  
         }
 307  
 
 308  
         @Override
 309  
         public List<CluResultTypeInfo> getCluResultTypes()
 310  
                         throws OperationFailedException {
 311  0
                 return LuServiceAssembler.toCluResultTypeInfos(luDao
 312  
                                 .find(CluResultType.class));
 313  
         }
 314  
 
 315  
         @Override
 316  
         public CluResultTypeInfo getCluResultType(String cluResultTypeKey)
 317  
                         throws DoesNotExistException, InvalidParameterException,
 318  
                         MissingParameterException, OperationFailedException {
 319  0
                 return LuServiceAssembler.toCluResultTypeInfo(luDao.fetch(
 320  
                                 CluResultType.class, cluResultTypeKey));
 321  
         }
 322  
 
 323  
         @Override
 324  
         public List<CluResultTypeInfo> getCluResultTypesForLuType(String luTypeKey)
 325  
                         throws DoesNotExistException, InvalidParameterException,
 326  
                         MissingParameterException, OperationFailedException {
 327  0
                 checkForMissingParameter(luTypeKey, "luTypeKey");
 328  0
                 return LuServiceAssembler.toCluResultTypeInfos((luDao
 329  
                                 .getAllowedCluResultTypesForLuType(luTypeKey)));
 330  
         }
 331  
 
 332  
         @Override
 333  
         public List<ResultUsageTypeInfo> getResultUsageTypes()
 334  
                         throws OperationFailedException {
 335  0
                 return LuServiceAssembler.toResultUsageTypeInfos(luDao
 336  
                                 .find(ResultUsageType.class));
 337  
         }
 338  
 
 339  
         @Override
 340  
         public ResultUsageTypeInfo getResultUsageType(String resultUsageTypeKey)
 341  
                         throws DoesNotExistException, InvalidParameterException,
 342  
                         MissingParameterException, OperationFailedException {
 343  0
                 checkForMissingParameter(resultUsageTypeKey, "resultUsageTypeKey");
 344  0
                 return LuServiceAssembler.toResultUsageTypeInfo(luDao.fetch(
 345  
                                 ResultUsageType.class, resultUsageTypeKey));
 346  
         }
 347  
 
 348  
         @Override
 349  
         public List<String> getAllowedResultUsageTypesForLuType(String luTypeKey)
 350  
                         throws DoesNotExistException, InvalidParameterException,
 351  
                         MissingParameterException, OperationFailedException {
 352  0
                 checkForMissingParameter(luTypeKey, "luTypeKey");
 353  
 
 354  0
                 return luDao.getAllowedResultUsageTypesForLuType(luTypeKey);
 355  
         }
 356  
 
 357  
         @Override
 358  
         public List<String> getAllowedResultComponentTypesForResultUsageType(
 359  
                         String resultUsageTypeKey) throws DoesNotExistException,
 360  
                         InvalidParameterException, MissingParameterException,
 361  
                         OperationFailedException {
 362  
 
 363  0
                 checkForMissingParameter(resultUsageTypeKey, "resultUsageTypeKey");
 364  
 
 365  0
                 return luDao
 366  
                                 .getAllowedResultComponentTypesForResultUsageType(resultUsageTypeKey);
 367  
         }
 368  
 
 369  
         @Override
 370  
         public CluLoRelationTypeInfo getCluLoRelationType(
 371  
                         String cluLoRelationTypeKey) throws DoesNotExistException,
 372  
                         InvalidParameterException, MissingParameterException,
 373  
                         OperationFailedException {
 374  0
                 checkForMissingParameter(cluLoRelationTypeKey, "cluLoRelationTypeKey");
 375  
 
 376  0
                 CluLoRelationType cluLoRelationType = luDao.fetch(
 377  
                                 CluLoRelationType.class, cluLoRelationTypeKey);
 378  0
                 return LuServiceAssembler.toCluLoRelationTypeInfo(cluLoRelationType);
 379  
         }
 380  
 
 381  
         @Override
 382  
         public List<CluLoRelationTypeInfo> getCluLoRelationTypes()
 383  
                         throws OperationFailedException {
 384  0
                 return LuServiceAssembler.toCluLoRelationTypeInfos(luDao
 385  
                                 .find(CluLoRelationType.class));
 386  
         }
 387  
 
 388  
         @Override
 389  
         public List<String> getAllowedCluLoRelationTypesForLuType(String luTypeKey)
 390  
                         throws DoesNotExistException, InvalidParameterException,
 391  
                         MissingParameterException, OperationFailedException {
 392  
 
 393  0
                 checkForMissingParameter(luTypeKey, luTypeKey);
 394  
 
 395  0
                 return luDao.getAllowedCluLoRelationTypesForLuType(luTypeKey);
 396  
         }
 397  
 
 398  
         @Override
 399  
         public List<CluSetTypeInfo> getCluSetTypes()
 400  
                         throws OperationFailedException {
 401  0
                 return LuServiceAssembler.toCluSetTypeInfos(luDao
 402  
                                 .find(CluSetType.class));
 403  
         }
 404  
 
 405  
         @Override
 406  
         public CluSetTypeInfo getCluSetType(String cluSetTypeKey)
 407  
                         throws DoesNotExistException, InvalidParameterException,
 408  
                         MissingParameterException, OperationFailedException {
 409  0
                 checkForMissingParameter(cluSetTypeKey, "cluSetTypeKey");
 410  0
                 return LuServiceAssembler.toCluSetTypeInfo(luDao.fetch(
 411  
                                 CluSetType.class, cluSetTypeKey));
 412  
         }
 413  
 
 414  
         /**************************************************************************
 415  
          * READ OPERATION *
 416  
          **************************************************************************/
 417  
 
 418  
         // **** Core **********
 419  
         @Override
 420  
         public CluInfo getClu(String cluId) throws DoesNotExistException,
 421  
                         InvalidParameterException, MissingParameterException,
 422  
                         OperationFailedException {
 423  
 
 424  0
                 checkForMissingParameter(cluId, "cluId");
 425  
 
 426  0
                 Clu clu = luDao.fetch(Clu.class, cluId);
 427  0
                 return LuServiceAssembler.toCluInfo(clu);
 428  
         }
 429  
 
 430  
         @Override
 431  
         public List<CluInfo> getClusByIdList(List<String> cluIdList)
 432  
                         throws DoesNotExistException, InvalidParameterException,
 433  
                         MissingParameterException, OperationFailedException {
 434  0
                 checkForMissingParameter(cluIdList, "cluIdList");
 435  0
                 checkForEmptyList(cluIdList, "cluIdList");
 436  0
                 List<Clu> clus = luDao.getClusByIdList(cluIdList);
 437  0
                 return LuServiceAssembler.toCluInfos(clus);
 438  
         }
 439  
 
 440  
         @Override
 441  
         public List<CluInfo> getClusByLuType(String luTypeKey, String luState)
 442  
                         throws DoesNotExistException, InvalidParameterException,
 443  
                         MissingParameterException, OperationFailedException {
 444  0
                 checkForMissingParameter(luTypeKey, "luTypeKey");
 445  0
                 checkForMissingParameter(luState, "lustate");
 446  0
                 List<Clu> clus = luDao.getClusByLuType(luTypeKey, luState);
 447  0
                 return LuServiceAssembler.toCluInfos(clus);
 448  
         }
 449  
 
 450  
         @Override
 451  
         public List<String> getCluIdsByLuType(String luTypeKey, String luState)
 452  
                         throws DoesNotExistException, InvalidParameterException,
 453  
                         MissingParameterException, OperationFailedException {
 454  0
                 checkForMissingParameter(luTypeKey, "luTypeKey");
 455  0
                 checkForMissingParameter(luState, "luState");
 456  0
                 List<Clu> clus = luDao.getClusByLuType(luTypeKey, luState);
 457  0
                 List<String> ids = new ArrayList<String>(clus.size());
 458  0
                 for (Clu clu : clus) {
 459  0
                         ids.add(clu.getId());
 460  
                 }
 461  0
                 return ids;
 462  
         }
 463  
 
 464  
         // ****** Relations
 465  
 
 466  
         @Override
 467  
         public List<String> getAllowedLuLuRelationTypesByCluId(String cluId,
 468  
                         String relatedCluId) throws DoesNotExistException,
 469  
                         InvalidParameterException, MissingParameterException,
 470  
                         OperationFailedException {
 471  0
                 checkForMissingParameter(cluId, "cluId");
 472  0
                 checkForMissingParameter(relatedCluId, "relatedCluId");
 473  
 
 474  0
                 return luDao.getAllowedLuLuRelationTypesByCluId(cluId, relatedCluId);
 475  
         }
 476  
 
 477  
         @Override
 478  
         public List<CluInfo> getClusByRelation(String relatedCluId,
 479  
                         String luLuRelationTypeKey) throws DoesNotExistException,
 480  
                         InvalidParameterException, MissingParameterException,
 481  
                         OperationFailedException {
 482  0
                 checkForMissingParameter(relatedCluId, "relatedCluId");
 483  0
                 checkForMissingParameter(luLuRelationTypeKey, "luLuRelationTypeKey");
 484  
 
 485  0
                 List<Clu> clus = luDao.getClusByRelation(relatedCluId,
 486  
                                 luLuRelationTypeKey);
 487  0
                 List<CluInfo> result = LuServiceAssembler.toCluInfos(clus);
 488  0
                 return result;
 489  
 
 490  
         }
 491  
 
 492  
         @Override
 493  
         public List<String> getCluIdsByRelation(String relatedCluId,
 494  
                         String luLuRelationTypeKey) throws DoesNotExistException,
 495  
                         InvalidParameterException, MissingParameterException,
 496  
                         OperationFailedException {
 497  0
                 checkForMissingParameter(relatedCluId, "relatedCluId");
 498  0
                 checkForMissingParameter(luLuRelationTypeKey, "luLuRelationTypeKey");
 499  
 
 500  0
         List<String> cluIds = luDao.getCluIdsByRelatedCluId(relatedCluId, luLuRelationTypeKey);
 501  0
         return cluIds;
 502  
         }
 503  
 
 504  
         @Override
 505  
         public List<CluInfo> getRelatedClusByCluId(String cluId,
 506  
                         String luLuRelationTypeKey) throws DoesNotExistException,
 507  
                         InvalidParameterException, MissingParameterException,
 508  
                         OperationFailedException {
 509  0
                 checkForMissingParameter(cluId, "cluId");
 510  0
                 checkForMissingParameter(luLuRelationTypeKey, "luLuRelationTypeKey");
 511  0
                 List<Clu> relatedClus = luDao.getRelatedClusByCluId(cluId,
 512  
                                 luLuRelationTypeKey);
 513  0
                 return LuServiceAssembler.toCluInfos(relatedClus);
 514  
         }
 515  
 
 516  
         @Override
 517  
         public List<String> getRelatedCluIdsByCluId(String cluId,
 518  
                         String luLuRelationTypeKey) throws DoesNotExistException,
 519  
                         InvalidParameterException, MissingParameterException,
 520  
                         OperationFailedException {
 521  0
                 checkForMissingParameter(cluId, "cluId");
 522  0
                 checkForMissingParameter(luLuRelationTypeKey, "luLuRelationTypeKey");
 523  0
                 List<String> relatedCluIds = luDao.getRelatedCluIdsByCluId(cluId,
 524  
                                 luLuRelationTypeKey);
 525  0
                 return relatedCluIds;
 526  
         }
 527  
 
 528  
         @Override
 529  
         public CluCluRelationInfo getCluCluRelation(String cluCluRelationId)
 530  
                         throws DoesNotExistException, InvalidParameterException,
 531  
                         MissingParameterException, OperationFailedException {
 532  0
                 checkForMissingParameter(cluCluRelationId, "cluCluRelationId");
 533  0
                 return LuServiceAssembler.toCluCluRelationInfo(luDao.fetch(
 534  
                                 CluCluRelation.class, cluCluRelationId));
 535  
         }
 536  
 
 537  
         @Override
 538  
         public List<CluCluRelationInfo> getCluCluRelationsByClu(String cluId)
 539  
                         throws DoesNotExistException, InvalidParameterException,
 540  
                         MissingParameterException, OperationFailedException {
 541  0
                 checkForMissingParameter(cluId, "cluId");
 542  0
                 List<CluCluRelation> cluCluRelations = luDao
 543  
                                 .getCluCluRelationsByClu(cluId);
 544  0
                 return LuServiceAssembler.toCluCluRelationInfos(cluCluRelations);
 545  
         }
 546  
 
 547  
         // **** Publication
 548  
         @Override
 549  
         public List<CluPublicationInfo> getCluPublicationsByCluId(String cluId)
 550  
                         throws DoesNotExistException, InvalidParameterException,
 551  
                         MissingParameterException, OperationFailedException {
 552  0
               checkForMissingParameter(cluId, "cluId");
 553  0
               List<CluPublication> cluPublications = luDao.getCluPublicationsByCluId(cluId);
 554  0
               return LuServiceAssembler.toCluPublicationInfos(cluPublications);
 555  
         }
 556  
 
 557  
         @Override
 558  
         public List<CluPublicationInfo> getCluPublicationsByType(
 559  
                         String luPublicationTypeKey) throws DoesNotExistException,
 560  
                         InvalidParameterException, MissingParameterException,
 561  
                         OperationFailedException {
 562  0
               checkForMissingParameter(luPublicationTypeKey, "luPublicationTypeKey");
 563  0
               List<CluPublication> cluPublications = luDao.getCluPublicationsByType(luPublicationTypeKey);
 564  0
               return LuServiceAssembler.toCluPublicationInfos(cluPublications);
 565  
         }
 566  
 
 567  
         @Override
 568  
         public CluPublicationInfo getCluPublication(String cluPublicationId)
 569  
                         throws DoesNotExistException, InvalidParameterException,
 570  
                         MissingParameterException, OperationFailedException {
 571  0
               checkForMissingParameter(cluPublicationId, "cluPublicationId");
 572  0
               CluPublication cluPublication = luDao.fetch(CluPublication.class, cluPublicationId);
 573  0
               return LuServiceAssembler.toCluPublicationInfo(cluPublication);
 574  
         }
 575  
 
 576  
         // **** Results
 577  
 
 578  
         @Override
 579  
         public CluResultInfo getCluResult(String cluResultId)
 580  
                         throws DoesNotExistException, InvalidParameterException,
 581  
                         MissingParameterException, OperationFailedException {
 582  
 
 583  0
                 checkForMissingParameter(cluResultId, "cluResultId");
 584  
 
 585  0
                 CluResult cluResult = luDao.fetch(CluResult.class, cluResultId);
 586  0
                 return LuServiceAssembler.toCluResultInfo(cluResult);
 587  
         }
 588  
 
 589  
         @Override
 590  
         public List<CluResultInfo> getCluResultByClu(String cluId)
 591  
                         throws DoesNotExistException, InvalidParameterException,
 592  
                         MissingParameterException, OperationFailedException {
 593  
 
 594  0
                 checkForMissingParameter(cluId, "cluId");
 595  
 
 596  0
                 return LuServiceAssembler.toCluResultInfos(luDao
 597  
                                 .getCluResultByClu(cluId));
 598  
         }
 599  
 
 600  
         @Override
 601  
         public List<String> getCluIdsByResultUsageType(String resultUsageTypeKey)
 602  
                         throws DoesNotExistException, InvalidParameterException,
 603  
                         MissingParameterException, OperationFailedException {
 604  0
                 return luDao.getCluIdsByResultUsageType(resultUsageTypeKey);
 605  
         }
 606  
 
 607  
         @Override
 608  
         public List<String> getCluIdsByResultComponent(String resultComponentId)
 609  
                         throws DoesNotExistException, InvalidParameterException,
 610  
                         MissingParameterException, OperationFailedException {
 611  0
                 return luDao.getCluIdsByResultComponentId(resultComponentId);
 612  
         }
 613  
 
 614  
         // **** Learning Objectives
 615  
 
 616  
         @Override
 617  
         public CluLoRelationInfo getCluLoRelation(String cluLoRelationId)
 618  
                         throws DoesNotExistException, InvalidParameterException,
 619  
                         MissingParameterException, OperationFailedException,
 620  
                         PermissionDeniedException {
 621  
 
 622  0
                 checkForMissingParameter(cluLoRelationId, "cluLoRelationId");
 623  
 
 624  0
                 CluLoRelation reltn = luDao.fetch(CluLoRelation.class, cluLoRelationId);
 625  0
                 return LuServiceAssembler.toCluLoRelationInfo(reltn);
 626  
 
 627  
         }
 628  
 
 629  
         @Override
 630  
         public List<CluLoRelationInfo> getCluLoRelationsByClu(String cluId)
 631  
                         throws DoesNotExistException, InvalidParameterException,
 632  
                         MissingParameterException, OperationFailedException {
 633  
 
 634  0
                 checkForMissingParameter(cluId, "cluId");
 635  0
                 List<CluLoRelation> cluLoRelations = luDao
 636  
                                 .getCluLoRelationsByClu(cluId);
 637  0
                 return LuServiceAssembler.toCluLoRelationInfos(cluLoRelations);
 638  
 
 639  
         }
 640  
 
 641  
         @Override
 642  
         public List<CluLoRelationInfo> getCluLoRelationsByLo(String loId)
 643  
                         throws DoesNotExistException, InvalidParameterException,
 644  
                         MissingParameterException, OperationFailedException {
 645  0
                 checkForMissingParameter(loId, "loId");
 646  0
                 List<CluLoRelation> cluLoRelations = luDao.getCluLoRelationsByLo(loId);
 647  0
                 return LuServiceAssembler.toCluLoRelationInfos(cluLoRelations);
 648  
         }
 649  
 
 650  
         // *** Resources
 651  
 
 652  
         @Override
 653  
         public List<String> getResourceRequirementsForCluId(String cluId)
 654  
                         throws DoesNotExistException, InvalidParameterException,
 655  
                         MissingParameterException, OperationFailedException {
 656  0
               throw new UnsupportedOperationException("Method not yet implemented!");
 657  
         }
 658  
 
 659  
         // *** Sets
 660  
 
 661  
         @Override
 662  
         public CluSetInfo getCluSetInfo(String cluSetId)
 663  
                         throws DoesNotExistException, InvalidParameterException,
 664  
                         MissingParameterException, OperationFailedException,
 665  
                         PermissionDeniedException {
 666  0
                 checkForMissingParameter(cluSetId, "cluSetId");
 667  0
                 CluSet cluSet = luDao.fetch(CluSet.class, cluSetId);
 668  0
                 CluSetInfo cluSetInfo = LuServiceAssembler.toCluSetInfo(cluSet);
 669  0
                 setMembershipQuerySearchResult(cluSetInfo);
 670  0
                 return cluSetInfo;
 671  
         }
 672  
 
 673  
         @Override
 674  
         public CluSetTreeViewInfo getCluSetTreeView(String cluSetId)
 675  
                         throws DoesNotExistException, InvalidParameterException,
 676  
                         MissingParameterException, OperationFailedException,
 677  
                         PermissionDeniedException {
 678  
 
 679  0
                 checkForMissingParameter(cluSetId, "cluSetId");
 680  0
                 CluSetInfo cluSet = getCluSetInfo(cluSetId);
 681  0
                 if (cluSet == null) {
 682  0
                         return null;
 683  
                 }
 684  
 
 685  0
                 CluSetTreeViewInfo cluSetTreeView = new CluSetTreeViewInfo();
 686  0
                 getCluSetTreeViewHelper(cluSet, cluSetTreeView);
 687  0
                 return cluSetTreeView;
 688  
         }
 689  
 
 690  
         /**
 691  
          * Go through the list of CluSets and retrieve all the information regarding child
 692  
          * Clu Sets and associated Clus
 693  
          *
 694  
          * @param cluSetInfo
 695  
          * @param cluSetTreeViewInfo
 696  
          * @throws DoesNotExistException
 697  
          * @throws InvalidParameterException
 698  
          * @throws MissingParameterException
 699  
          * @throws OperationFailedException
 700  
          * @throws PermissionDeniedException
 701  
          */
 702  
         private void getCluSetTreeViewHelper(CluSetInfo cluSetInfo,
 703  
                         CluSetTreeViewInfo cluSetTreeViewInfo) throws DoesNotExistException, InvalidParameterException, MissingParameterException, OperationFailedException, PermissionDeniedException {
 704  0
                 cluSetTreeViewInfo.setName(cluSetInfo.getName());
 705  0
                 cluSetTreeViewInfo.setDescr(cluSetInfo.getDescr());
 706  0
                 cluSetTreeViewInfo.setEffectiveDate(cluSetInfo.getEffectiveDate());
 707  0
                 cluSetTreeViewInfo.setExpirationDate(cluSetInfo.getExpirationDate());
 708  0
                 cluSetTreeViewInfo.setAdminOrg(cluSetInfo.getAdminOrg());
 709  0
                 cluSetTreeViewInfo.setIsReusable(cluSetInfo.getIsReusable());
 710  0
                 cluSetTreeViewInfo.setIsReferenceable(cluSetInfo.getIsReferenceable());
 711  0
                 cluSetTreeViewInfo.setMetaInfo(cluSetInfo.getMetaInfo());
 712  0
                 cluSetTreeViewInfo.setAttributes(cluSetInfo.getAttributes());
 713  0
                 cluSetTreeViewInfo.setType(cluSetInfo.getType());
 714  0
                 cluSetTreeViewInfo.setState(cluSetInfo.getState());
 715  0
                 cluSetTreeViewInfo.setId(cluSetInfo.getId());
 716  
 
 717  0
                 if (!cluSetInfo.getCluSetIds().isEmpty()) {
 718  0
                         for (String cluSetId : cluSetInfo.getCluSetIds()) {
 719  0
                                 CluSetInfo subCluSet = getCluSetInfo(cluSetId);
 720  0
                                 List<CluSetTreeViewInfo> cluSets =
 721  
                     cluSetTreeViewInfo.getCluSets() == null ?
 722  
                             new ArrayList<CluSetTreeViewInfo>(0) : cluSetTreeViewInfo.getCluSets();
 723  
 
 724  0
                 CluSetTreeViewInfo subCluSetTreeViewInfo = new CluSetTreeViewInfo();
 725  0
                 getCluSetTreeViewHelper(subCluSet, subCluSetTreeViewInfo);
 726  0
                 cluSets.add(subCluSetTreeViewInfo);
 727  
 
 728  0
                 cluSetTreeViewInfo.setCluSets(cluSets);
 729  0
                         }
 730  
                 }
 731  0
                 List<CluInfo> clus = new ArrayList<CluInfo>();
 732  0
                 for (String cluId : cluSetInfo.getCluIds()) {
 733  0
                         if(cluId!=null){
 734  
                 //Optimized version of clu translation. It seems like for now we only need the following information.
 735  
                 //If more information is needed, then appropriate method in assembler has to be used.
 736  0
                 Clu clu = luDao.getCurrentCluVersion(cluId);
 737  0
                 CluInfo cluInfo = new CluInfo();
 738  0
                 cluInfo.setId(clu.getId());
 739  0
                 cluInfo.setType(clu.getLuType().getId());
 740  0
                 cluInfo.setOfficialIdentifier(LuServiceAssembler.toCluIdentifierInfo(clu.getOfficialIdentifier()));
 741  0
                                 clus.add(cluInfo);
 742  0
                         }
 743  
                 }
 744  0
                 cluSetTreeViewInfo.setClus(clus);
 745  0
         }
 746  
 
 747  
         @Override
 748  
         public List<CluSetInfo> getCluSetInfoByIdList(List<String> cluSetIdList)
 749  
                         throws DoesNotExistException, InvalidParameterException,
 750  
                         MissingParameterException, OperationFailedException,
 751  
                         PermissionDeniedException {
 752  0
                 checkForMissingParameter(cluSetIdList, "cluSetIdList");
 753  0
                 checkForEmptyList(cluSetIdList, "cluSetIdList");
 754  0
                 List<CluSet> cluSets = luDao.getCluSetInfoByIdList(cluSetIdList);
 755  0
                 return LuServiceAssembler.toCluSetInfos(cluSets);
 756  
         }
 757  
 
 758  
         @Override
 759  
         public List<String> getCluSetIdsFromCluSet(String cluSetId)
 760  
                         throws DoesNotExistException, InvalidParameterException,
 761  
                         MissingParameterException, OperationFailedException,
 762  
                         PermissionDeniedException {
 763  0
                 checkForMissingParameter(cluSetId, "cluSetId");
 764  0
                 CluSet cluSet = luDao.fetch(CluSet.class, cluSetId);
 765  0
                 List<String> ids = new ArrayList<String>(cluSet.getCluVerIndIds().size());
 766  0
                 if(cluSet.getCluSets()!=null){
 767  0
                         for (CluSet cluSet2 : cluSet.getCluSets()) {
 768  0
                                 ids.add(cluSet2.getId());
 769  
                         }
 770  
                 }
 771  0
                 return ids;
 772  
         }
 773  
 
 774  
         @Override
 775  
         public Boolean isCluSetDynamic(String cluSetId)
 776  
                         throws DoesNotExistException, InvalidParameterException,
 777  
                         MissingParameterException, OperationFailedException,
 778  
                         PermissionDeniedException {
 779  0
               throw new UnsupportedOperationException("Method not yet implemented!");
 780  
         }
 781  
 
 782  
         @Override
 783  
         public List<CluInfo> getClusFromCluSet(String cluSetId)
 784  
                         throws DoesNotExistException, InvalidParameterException,
 785  
                         MissingParameterException, OperationFailedException,
 786  
                         PermissionDeniedException {
 787  0
                 checkForMissingParameter(cluSetId, "cluSetId");
 788  0
                 CluSet cluSet = luDao.fetch(CluSet.class, cluSetId);
 789  0
                 List<CluInfo> clus = new ArrayList<CluInfo>(cluSet.getCluVerIndIds().size());
 790  0
                 for (CluSetJoinVersionIndClu cluSetJnClu : cluSet.getCluVerIndIds()) {
 791  0
                         clus.add(LuServiceAssembler.toCluInfo(luDao.getCurrentCluVersion(cluSetJnClu.getCluVersionIndId())));
 792  
                 }
 793  0
                 return clus;
 794  
         }
 795  
 
 796  
         @Override
 797  
         public List<String> getCluIdsFromCluSet(String cluSetId)
 798  
                         throws DoesNotExistException, InvalidParameterException,
 799  
                         MissingParameterException, OperationFailedException,
 800  
                         PermissionDeniedException {
 801  0
                 checkForMissingParameter(cluSetId, "cluSetId");
 802  0
                 CluSet cluSet = luDao.fetch(CluSet.class, cluSetId);
 803  0
                 List<String> ids = new ArrayList<String>(cluSet.getCluVerIndIds().size());
 804  0
                 for (CluSetJoinVersionIndClu cluSetJnClu : cluSet.getCluVerIndIds()) {
 805  0
                         ids.add(cluSetJnClu.getCluVersionIndId());
 806  
                 }
 807  0
                 return ids;
 808  
         }
 809  
 
 810  
         @Override
 811  
         public List<CluInfo> getAllClusInCluSet(String cluSetId)
 812  
                         throws DoesNotExistException, InvalidParameterException,
 813  
                         MissingParameterException, OperationFailedException,
 814  
                         PermissionDeniedException {
 815  0
                 checkForMissingParameter(cluSetId, "cluSetId");
 816  0
                 List<String> cluIndIds = new ArrayList<String>();
 817  0
                 CluSet cluSet = luDao.fetch(CluSet.class, cluSetId);
 818  0
                 findClusInCluSet(cluIndIds, cluSet);
 819  0
                 List<CluInfo> infos = new ArrayList<CluInfo>(cluIndIds.size());
 820  0
                 for (String cluIndId : cluIndIds) {
 821  0
                         infos.add(LuServiceAssembler.toCluInfo(luDao.getCurrentCluVersion(cluIndId)));
 822  
                 }
 823  0
                 return infos;
 824  
         }
 825  
 
 826  
         @Override
 827  
         public List<String> getAllCluIdsInCluSet(String cluSetId)
 828  
                         throws DoesNotExistException, InvalidParameterException,
 829  
                         MissingParameterException, OperationFailedException,
 830  
                         PermissionDeniedException {
 831  0
                 checkForMissingParameter(cluSetId, "cluSetId");
 832  0
                 List<String> ids = new ArrayList<String>();
 833  0
                 CluSet cluSet = luDao.fetch(CluSet.class, cluSetId);
 834  0
                 findClusInCluSet(ids, cluSet);
 835  0
                 return ids;
 836  
         }
 837  
 
 838  
         @Override
 839  
         public Boolean isCluInCluSet(String cluId, String cluSetId)
 840  
                         throws DoesNotExistException, InvalidParameterException,
 841  
                         MissingParameterException, OperationFailedException,
 842  
                         PermissionDeniedException {
 843  0
                 checkForMissingParameter(cluId, "cluId");
 844  0
                 checkForMissingParameter(cluSetId, "cluSetId");
 845  0
                 return luDao.isCluInCluSet(cluId, cluSetId);
 846  
         }
 847  
 
 848  
         // ******** LUI OPERATIONS
 849  
         // *** Core
 850  
 
 851  
         @Override
 852  
         public LuiInfo getLui(String luiId) throws DoesNotExistException,
 853  
                         InvalidParameterException, MissingParameterException,
 854  
                         OperationFailedException {
 855  
 
 856  0
                 checkForMissingParameter(luiId, "luiId");
 857  
 
 858  0
                 Lui lui = luDao.fetch(Lui.class, luiId);
 859  0
                 return LuServiceAssembler.toLuiInfo(lui);
 860  
         }
 861  
 
 862  
         @Override
 863  
         public List<LuiInfo> getLuisByIdList(List<String> luiIdList)
 864  
                         throws DoesNotExistException, InvalidParameterException,
 865  
                         MissingParameterException, OperationFailedException {
 866  0
                 checkForMissingParameter(luiIdList, "luiIdList");
 867  0
                 checkForEmptyList(luiIdList, "luiIdList");
 868  0
                 List<Lui> luis = luDao.getLuisByIdList(luiIdList);
 869  0
                 return LuServiceAssembler.toLuiInfos(luis);
 870  
         }
 871  
 
 872  
         @Override
 873  
         public List<LuiInfo> getLuisInAtpByCluId(String cluId, String atpKey)
 874  
                         throws DoesNotExistException, InvalidParameterException,
 875  
                         MissingParameterException, OperationFailedException {
 876  0
               throw new UnsupportedOperationException("Method not yet implemented!");
 877  
         }
 878  
 
 879  
         @Override
 880  
         public List<String> getLuiIdsByCluId(String cluId)
 881  
                         throws DoesNotExistException, InvalidParameterException,
 882  
                         MissingParameterException, OperationFailedException {
 883  
 
 884  0
                 checkForMissingParameter(cluId, "cluId");
 885  
 
 886  0
                 return luDao.getLuiIdsByCluId(cluId);
 887  
         }
 888  
 
 889  
         @Override
 890  
         public List<String> getLuiIdsInAtpByCluId(String cluId, String atpKey)
 891  
                         throws DoesNotExistException, InvalidParameterException,
 892  
                         MissingParameterException, OperationFailedException {
 893  
 
 894  0
                 checkForMissingParameter(cluId, "cluId");
 895  0
                 checkForMissingParameter(atpKey, "atpKey");
 896  0
                 return luDao.getLuiIdsInAtpByCluId(cluId, atpKey);
 897  
         }
 898  
 
 899  
         // *** Relations
 900  
 
 901  
         @Override
 902  
         public List<String> getAllowedLuLuRelationTypesByLuiId(String luiId,
 903  
                         String relatedLuiId) throws DoesNotExistException,
 904  
                         InvalidParameterException, MissingParameterException,
 905  
                         OperationFailedException {
 906  
 
 907  0
                 checkForMissingParameter(luiId, "luiId");
 908  0
                 checkForMissingParameter(relatedLuiId, "relatedLuiId");
 909  
 
 910  0
                 return luDao.getAllowedLuLuRelationTypesByLuiId(luiId, relatedLuiId);
 911  
         }
 912  
 
 913  
         @Override
 914  
         public List<LuiInfo> getLuisByRelation(String luiId,
 915  
                         String luLuRelationTypeKey) throws DoesNotExistException,
 916  
                         InvalidParameterException, MissingParameterException,
 917  
                         OperationFailedException {
 918  0
                 checkForMissingParameter(luiId, "luiId");
 919  0
                 checkForMissingParameter(luLuRelationTypeKey, "luLuRelationTypeKey");
 920  
 
 921  0
                 return LuServiceAssembler.toLuiInfos(luDao.getLuisByRelationType(luiId,
 922  
                                 luLuRelationTypeKey));
 923  
         }
 924  
 
 925  
         @Override
 926  
         public List<String> getLuiIdsByRelation(String luiId,
 927  
                         String luLuRelationTypeKey) throws DoesNotExistException,
 928  
                         InvalidParameterException, MissingParameterException,
 929  
                         OperationFailedException {
 930  0
                 checkForMissingParameter(luiId, "luiId");
 931  0
                 checkForMissingParameter(luLuRelationTypeKey, "luLuRelationTypeKey");
 932  
 
 933  0
                 return luDao.getLuiIdsByRelationType(luiId, luLuRelationTypeKey);
 934  
         }
 935  
 
 936  
         @Override
 937  
         public List<LuiInfo> getRelatedLuisByLuiId(String luiId,
 938  
                         String luLuRelationTypeKey) throws DoesNotExistException,
 939  
                         InvalidParameterException, MissingParameterException,
 940  
                         OperationFailedException {
 941  0
                 checkForMissingParameter(luiId, "luiId");
 942  0
                 checkForMissingParameter(luLuRelationTypeKey, "luLuRelationTypeKey");
 943  0
                 List<Lui> relatedLuis = luDao.getRelatedLuisByLuiId(luiId,
 944  
                                 luLuRelationTypeKey);
 945  0
                 return LuServiceAssembler.toLuiInfos(relatedLuis);
 946  
         }
 947  
 
 948  
         @Override
 949  
         public List<String> getRelatedLuiIdsByLuiId(String luiId,
 950  
                         String luLuRelationTypeKey) throws DoesNotExistException,
 951  
                         InvalidParameterException, MissingParameterException,
 952  
                         OperationFailedException {
 953  0
                 checkForMissingParameter(luiId, "luiId");
 954  0
                 checkForMissingParameter(luLuRelationTypeKey, "luLuRelationTypeKey");
 955  0
                 List<String> relatedLuiIds = luDao.getRelatedLuiIdsByLuiId(luiId,
 956  
                                 luLuRelationTypeKey);
 957  0
                 return relatedLuiIds;
 958  
         }
 959  
 
 960  
         @Override
 961  
         public LuiLuiRelationInfo getLuiLuiRelation(String luiLuiRelationId)
 962  
                         throws DoesNotExistException, InvalidParameterException,
 963  
                         MissingParameterException, OperationFailedException {
 964  0
                 checkForMissingParameter(luiLuiRelationId, "luiLuiRelationId");
 965  0
                 LuiLuiRelation luiLuiRelation = luDao.fetch(LuiLuiRelation.class,
 966  
                                 luiLuiRelationId);
 967  0
                 return LuServiceAssembler.toLuiLuiRelationInfo(luiLuiRelation);
 968  
         }
 969  
 
 970  
         @Override
 971  
         public List<LuiLuiRelationInfo> getLuiLuiRelationsByLui(String luiId)
 972  
                         throws DoesNotExistException, InvalidParameterException,
 973  
                         MissingParameterException, OperationFailedException {
 974  0
                 checkForMissingParameter(luiId, "luiId");
 975  0
                 List<LuiLuiRelation> entities = luDao.getLuiLuiRelations(luiId);
 976  0
                 return LuServiceAssembler.toLuiLuiRelationInfos(entities);
 977  
         }
 978  
 
 979  
         /**************************************************************************
 980  
          * MAINTENANCE OPERATIONS *
 981  
          **************************************************************************/
 982  
 
 983  
         @Override
 984  
         public List<ValidationResultInfo> validateClu(String validationType,
 985  
                         CluInfo cluInfo) throws DoesNotExistException,
 986  
                         InvalidParameterException, MissingParameterException,
 987  
                         OperationFailedException {
 988  0
                 checkForMissingParameter(validationType, "validationType");
 989  0
                 checkForMissingParameter(cluInfo, "cluInfo");
 990  
 
 991  0
         ObjectStructureDefinition objStructure = this.getObjectStructure(CluInfo.class.getName());
 992  0
         Validator defaultValidator = validatorFactory.getValidator();
 993  0
         List<ValidationResultInfo> validationResults = defaultValidator.validateObject(cluInfo, objStructure);
 994  
         
 995  0
         return validationResults;
 996  
         }
 997  
 
 998  
         @Override
 999  
         @Transactional(readOnly=false)
 1000  
         public CluInfo createClu(String luTypeKey, CluInfo cluInfo)
 1001  
                         throws AlreadyExistsException, DataValidationErrorException,
 1002  
                         DoesNotExistException, InvalidParameterException,
 1003  
                         MissingParameterException, OperationFailedException,
 1004  
                         PermissionDeniedException {
 1005  0
                 Clu clu = toCluForCreate(luTypeKey,cluInfo);
 1006  
                 //Set current (since this is brand new and every verIndId needs one current)
 1007  0
                 if(clu.getVersion() == null){
 1008  0
                         clu.setVersion(new Version());
 1009  
                 }
 1010  0
                 clu.getVersion().setCurrentVersionStart(new Date());
 1011  0
                 luDao.create(clu);
 1012  0
                 return LuServiceAssembler.toCluInfo(clu);
 1013  
         }
 1014  
         
 1015  
         public Clu toCluForCreate(String luTypeKey, CluInfo cluInfo)
 1016  
                         throws AlreadyExistsException, DataValidationErrorException,
 1017  
                         DoesNotExistException, InvalidParameterException,
 1018  
                         MissingParameterException, OperationFailedException,
 1019  
                         PermissionDeniedException {
 1020  0
                 checkForMissingParameter(luTypeKey, "luTypeKey");
 1021  0
                 checkForMissingParameter(cluInfo, "cluInfo");
 1022  
 
 1023  
                 // Validate CLU
 1024  0
                 List<ValidationResultInfo> val = validateClu("SYSTEM", cluInfo);
 1025  0
                 if(null != val && val.size() > 0) {
 1026  0
                         throw new DataValidationErrorException("Validation error!", val);
 1027  
                 }
 1028  
 
 1029  0
                 Clu clu = new Clu();
 1030  
 
 1031  0
                 LuType luType = luDao.fetch(LuType.class, luTypeKey);
 1032  0
                 clu.setLuType(luType);
 1033  
 
 1034  0
                 if (cluInfo.getOfficialIdentifier() != null) {
 1035  0
                         clu.setOfficialIdentifier(LuServiceAssembler.createOfficialIdentifier(cluInfo, luDao));
 1036  
                 }
 1037  0
                 clu.setAlternateIdentifiers(LuServiceAssembler.createAlternateIdentifiers(cluInfo, luDao));
 1038  0
                 if (cluInfo.getDescr() != null) {
 1039  0
                     LuRichText descr = LuServiceAssembler.toRichText(LuRichText.class, cluInfo.getDescr());
 1040  0
                     if (descr.getPlain() != null || descr.getFormatted() != null) {
 1041  0
                         clu.setDescr(descr);
 1042  
                     }
 1043  
                 }
 1044  
 
 1045  0
                 if (clu.getAdminOrgs() == null) {
 1046  0
                         clu.setAdminOrgs(new ArrayList<CluAdminOrg>(0));
 1047  
                 }
 1048  0
                 List<CluAdminOrg> adminOrgs = clu.getAdminOrgs();
 1049  0
                 for (AdminOrgInfo orgInfo : cluInfo.getAdminOrgs()) {
 1050  0
                         CluAdminOrg instructor = new CluAdminOrg();
 1051  0
                         BeanUtils.copyProperties(orgInfo, instructor,
 1052  
                                         new String[] { "attributes" });
 1053  0
                         instructor.setAttributes(LuServiceAssembler.toGenericAttributes(
 1054  
                                         CluAdminOrgAttribute.class, orgInfo.getAttributes(),
 1055  
                                         instructor, luDao));
 1056  0
                         instructor.setClu(clu);
 1057  0
                         adminOrgs.add(instructor);
 1058  0
                 }
 1059  
 
 1060  0
                 if (cluInfo.getPrimaryInstructor() != null) {
 1061  0
                         CluInstructor primaryInstructor = new CluInstructor();
 1062  0
                         BeanUtils.copyProperties(cluInfo.getPrimaryInstructor(),
 1063  
                                         primaryInstructor, new String[] { "attributes" });
 1064  0
                         primaryInstructor.setAttributes(LuServiceAssembler
 1065  
                                         .toGenericAttributes(CluInstructorAttribute.class, cluInfo
 1066  
                                                         .getPrimaryInstructor().getAttributes(),
 1067  
                                                         primaryInstructor, luDao));
 1068  0
                         clu.setPrimaryInstructor(primaryInstructor);
 1069  
                 }
 1070  
 
 1071  0
                 if (clu.getInstructors() == null) {
 1072  0
                         clu.setInstructors(new ArrayList<CluInstructor>(0));
 1073  
                 }
 1074  0
                 List<CluInstructor> instructors = clu.getInstructors();
 1075  0
                 for (CluInstructorInfo instructorInfo : cluInfo.getInstructors()) {
 1076  0
                         CluInstructor instructor = new CluInstructor();
 1077  0
                         BeanUtils.copyProperties(instructorInfo, instructor,
 1078  
                                         new String[] { "attributes" });
 1079  0
                         instructor.setAttributes(LuServiceAssembler.toGenericAttributes(
 1080  
                                         CluInstructorAttribute.class, instructorInfo
 1081  
                                                         .getAttributes(), instructor, luDao));
 1082  0
                         instructors.add(instructor);
 1083  0
                 }
 1084  
 
 1085  0
                 if (cluInfo.getStdDuration() != null) {
 1086  0
                         clu.setStdDuration(LuServiceAssembler.toTimeAmount(cluInfo
 1087  
                                         .getStdDuration()));
 1088  
                 }
 1089  
 
 1090  0
                 if (clu.getLuCodes() == null) {
 1091  0
                         clu.setLuCodes(new ArrayList<LuCode>(0));
 1092  
                 }
 1093  0
                 List<LuCode> luCodes = clu.getLuCodes();
 1094  0
                 for (LuCodeInfo luCodeInfo : cluInfo.getLuCodes()) {
 1095  0
                         LuCode luCode = new LuCode();
 1096  0
                         luCode.setAttributes(LuServiceAssembler.toGenericAttributes(
 1097  
                                         LuCodeAttribute.class, luCodeInfo.getAttributes(), luCode,
 1098  
                                         luDao));
 1099  0
                         BeanUtils.copyProperties(luCodeInfo, luCode, new String[] {
 1100  
                                         "attributes", "metaInfo" });
 1101  0
                         luCode.setDescr(luCodeInfo.getDescr());
 1102  0
                         luCode.setClu(clu);
 1103  0
                         luCodes.add(luCode);
 1104  0
                 }
 1105  
 
 1106  0
                 if (clu.getOfferedAtpTypes() == null) {
 1107  0
                         clu.setOfferedAtpTypes(new ArrayList<CluAtpTypeKey>(0));
 1108  
                 }
 1109  0
                 List<CluAtpTypeKey> offeredAtpTypes = clu.getOfferedAtpTypes();
 1110  0
                 for (String atpTypeKey : cluInfo.getOfferedAtpTypes()) {
 1111  0
                         CluAtpTypeKey cluAtpTypeKey = new CluAtpTypeKey();
 1112  0
                         cluAtpTypeKey.setAtpTypeKey(atpTypeKey);
 1113  0
                         cluAtpTypeKey.setClu(clu);
 1114  0
                         offeredAtpTypes.add(cluAtpTypeKey);
 1115  0
                 }
 1116  
 
 1117  
                 // FEE INFO
 1118  0
                 if (cluInfo.getFeeInfo() != null) {
 1119  0
                         CluFee cluFee = null;
 1120  
                         try {
 1121  0
                                 cluFee = LuServiceAssembler.toCluFee(clu, false, cluInfo
 1122  
                                                 .getFeeInfo(), luDao);
 1123  0
                         } catch (VersionMismatchException e) {
 1124  
                                 // Version Mismatch Should Happen only for updates
 1125  0
                         }
 1126  0
                         clu.setFee(cluFee);
 1127  
                 }
 1128  
 
 1129  0
                 if (cluInfo.getAccountingInfo() != null) {
 1130  0
                         CluAccounting cluAccounting = new CluAccounting();
 1131  0
                         cluAccounting.setAttributes(LuServiceAssembler.toGenericAttributes(
 1132  
                                         CluAccountingAttribute.class, cluInfo.getAccountingInfo()
 1133  
                                                         .getAttributes(), cluAccounting, luDao));
 1134  0
                         cluAccounting.setAffiliatedOrgs(LuServiceAssembler
 1135  
                                         .toAffiliatedOrgs(false, cluAccounting.getAffiliatedOrgs(),
 1136  
                                                         cluInfo.getAccountingInfo().getAffiliatedOrgs(),
 1137  
                                                         luDao));
 1138  0
                         clu.setAccounting(cluAccounting);
 1139  
                 }
 1140  
 
 1141  0
                 clu.setAttributes(LuServiceAssembler.toGenericAttributes(
 1142  
                                 CluAttribute.class, cluInfo.getAttributes(), clu, luDao));
 1143  
 
 1144  
 
 1145  0
                 if (cluInfo.getIntensity() != null) {
 1146  0
                         clu.setIntensity(LuServiceAssembler
 1147  
                                         .toAmount(cluInfo.getIntensity()));
 1148  
                 }
 1149  
 
 1150  0
                 if (clu.getCampusLocations() == null) {
 1151  0
                         clu.setCampusLocations(new ArrayList<CluCampusLocation>(0));
 1152  
                 }
 1153  0
                 List<CluCampusLocation> locations = clu.getCampusLocations();
 1154  0
                 for (String locationName : cluInfo.getCampusLocations()) {
 1155  0
                         CluCampusLocation location = new CluCampusLocation();
 1156  0
                         location.setCampusLocation(locationName);
 1157  0
                         location.setClu(clu);
 1158  0
                         locations.add(location);
 1159  0
                 }
 1160  
 
 1161  0
                 if (clu.getAccreditations() == null) {
 1162  0
                         clu.setAccreditations(new ArrayList<CluAccreditation>(0));
 1163  
                 }
 1164  0
                 List<CluAccreditation> accreditations = clu.getAccreditations();
 1165  0
                 for (AccreditationInfo accreditationInfo : cluInfo.getAccreditations()) {
 1166  0
                         CluAccreditation accreditation = new CluAccreditation();
 1167  0
                         BeanUtils.copyProperties(accreditationInfo, accreditation,
 1168  
                                         new String[] { "attributes" });
 1169  0
                         accreditation.setAttributes(LuServiceAssembler.toGenericAttributes(
 1170  
                                         CluAccreditationAttribute.class, accreditationInfo
 1171  
                                                         .getAttributes(), accreditation, luDao));
 1172  0
                         accreditations.add(accreditation);
 1173  0
                 }
 1174  
                 
 1175  
                 // Now copy all not standard properties
 1176  0
                 BeanUtils.copyProperties(cluInfo, clu, new String[] { "luType",
 1177  
                                 "officialIdentifier", "alternateIdentifiers", "descr",
 1178  
                                 "luCodes", "primaryInstructor", "instructors", "stdDuration",
 1179  
                                 "offeredAtpTypes", "feeInfo", "accountingInfo", "attributes",
 1180  
                                 "metaInfo", "versionInfo", "intensity",
 1181  
                                 "campusLocations", "accreditations",
 1182  
                                 "adminOrgs" });
 1183  
 
 1184  0
                 return clu;
 1185  
         }
 1186  
 
 1187  
         @Override
 1188  
         @Transactional(readOnly=false)
 1189  
         public CluInfo updateClu(String cluId, CluInfo cluInfo)
 1190  
                         throws DataValidationErrorException, DoesNotExistException,
 1191  
                         InvalidParameterException, MissingParameterException,
 1192  
                         OperationFailedException, PermissionDeniedException,
 1193  
                         VersionMismatchException {
 1194  
 
 1195  0
                 checkForMissingParameter(cluId, "cluId");
 1196  0
                 checkForMissingParameter(cluInfo, "cluInfo");
 1197  
 
 1198  
                 // Validate CLU
 1199  0
                 List<ValidationResultInfo> val = validateClu("SYSTEM", cluInfo);
 1200  0
                 if(null != val && val.size() > 0) {
 1201  0
                         throw new DataValidationErrorException("Validation error!", val);
 1202  
                 }
 1203  
 
 1204  0
                 Clu clu = luDao.fetch(Clu.class, cluId);
 1205  
 
 1206  0
                 if (!String.valueOf(clu.getVersionNumber()).equals(
 1207  
                                 cluInfo.getMetaInfo().getVersionInd())) {
 1208  0
                         throw new VersionMismatchException(
 1209  
                                         "Clu to be updated is not the current version");
 1210  
                 }
 1211  
 
 1212  0
                 LuType luType = luDao.fetch(LuType.class, cluInfo.getType());
 1213  0
                 clu.setLuType(luType);
 1214  
 
 1215  0
                 if (cluInfo.getOfficialIdentifier() != null) {
 1216  0
                     LuServiceAssembler.updateOfficialIdentifier(clu, cluInfo, luDao);
 1217  0
                 } else if (clu.getOfficialIdentifier() != null) {
 1218  0
                         luDao.delete(clu.getOfficialIdentifier());
 1219  
                 }
 1220  
 
 1221  
                 // Update the list of Alternate Identifiers
 1222  
                 // Get a map of Id->object of all the currently persisted objects in the
 1223  
                 // list
 1224  0
                 Map<String, CluIdentifier> oldAltIdMap = new HashMap<String, CluIdentifier>();
 1225  0
                 LuServiceAssembler.updateAlternateIdentifier(oldAltIdMap, clu, cluInfo, luDao);
 1226  
                 // Now delete anything left over
 1227  0
                 for (Entry<String, CluIdentifier> entry : oldAltIdMap.entrySet()) {
 1228  0
                         luDao.delete(entry.getValue());
 1229  
                 }
 1230  
 
 1231  0
                 if (cluInfo.getDescr() != null && (cluInfo.getDescr().getPlain() != null || cluInfo.getDescr().getFormatted() != null)) {
 1232  0
                         if (clu.getDescr() == null) {
 1233  0
                                 clu.setDescr(new LuRichText());
 1234  
                         }
 1235  0
                         BeanUtils.copyProperties(cluInfo.getDescr(), clu.getDescr());
 1236  0
                 } else if (clu.getDescr() != null) {
 1237  0
                         luDao.delete(clu.getDescr());
 1238  0
                         clu.setDescr(null);//TODO is the is the best method of doing this? what if the user passes in a new made up id, does that mean we have orphaned richtexts?
 1239  
                 }
 1240  
 
 1241  0
                 if (cluInfo.getPrimaryInstructor() != null) {
 1242  0
                         if (clu.getPrimaryInstructor() == null) {
 1243  0
                                 clu.setPrimaryInstructor(new CluInstructor());
 1244  
                         }
 1245  0
                         BeanUtils.copyProperties(cluInfo.getPrimaryInstructor(), clu
 1246  
                                         .getPrimaryInstructor(), new String[] { "attributes" });
 1247  0
                         clu.getPrimaryInstructor().setAttributes(
 1248  
                                         LuServiceAssembler.toGenericAttributes(
 1249  
                                                         CluInstructorAttribute.class, cluInfo
 1250  
                                                                         .getPrimaryInstructor().getAttributes(),
 1251  
                                                         clu.getPrimaryInstructor(), luDao));
 1252  0
                 } else if (clu.getPrimaryInstructor() != null) {
 1253  0
                         luDao.delete(clu.getPrimaryInstructor());
 1254  
                 }
 1255  
 
 1256  
                 // Update the List of instructors
 1257  
                 // Get a map of Id->object of all the currently persisted objects in the
 1258  
                 // list
 1259  0
                 Map<String, CluInstructor> oldInstructorMap = new HashMap<String, CluInstructor>();
 1260  0
                 for (CluInstructor cluInstructor : clu.getInstructors()) {
 1261  0
                         oldInstructorMap.put(cluInstructor.getOrgId() + "_"
 1262  
                                         + cluInstructor.getPersonId(), cluInstructor);
 1263  
                 }
 1264  0
                 clu.getInstructors().clear();
 1265  
 
 1266  
                 // Loop through the new list, if the item exists already update and
 1267  
                 // remove from the list
 1268  
                 // otherwise create a new entry
 1269  0
                 for (CluInstructorInfo instructorInfo : cluInfo.getInstructors()) {
 1270  0
                         CluInstructor cluInstructor = oldInstructorMap
 1271  
                                         .remove(instructorInfo.getOrgId() + "_"
 1272  
                                                         + instructorInfo.getPersonId());
 1273  0
                         if (cluInstructor == null) {
 1274  0
                                 cluInstructor = new CluInstructor();
 1275  
                         }
 1276  
                         // Do Copy
 1277  0
                         BeanUtils.copyProperties(instructorInfo, cluInstructor,
 1278  
                                         new String[] { "attributes" });
 1279  0
                         cluInstructor.setAttributes(LuServiceAssembler.toGenericAttributes(
 1280  
                                         CluInstructorAttribute.class, instructorInfo
 1281  
                                                         .getAttributes(), cluInstructor, luDao));
 1282  0
                         clu.getInstructors().add(cluInstructor);
 1283  0
                 }
 1284  
 
 1285  
                 // Now delete anything left over
 1286  0
                 for (Entry<String, CluInstructor> entry : oldInstructorMap.entrySet()) {
 1287  0
                         luDao.delete(entry.getValue());
 1288  
                 }
 1289  
 
 1290  0
                 if (cluInfo.getStdDuration() != null) {
 1291  0
                         if (clu.getStdDuration() == null) {
 1292  0
                                 clu.setStdDuration(new TimeAmount());
 1293  
                         }
 1294  0
                         BeanUtils.copyProperties(cluInfo.getStdDuration(), clu
 1295  
                                         .getStdDuration());
 1296  0
                 } else if (clu.getStdDuration() != null) {
 1297  0
                         luDao.delete(clu.getStdDuration());
 1298  
                 }
 1299  
 
 1300  
                 // Update the LuCodes
 1301  
                 // Get a map of Id->object of all the currently persisted objects in the
 1302  
                 // list
 1303  0
                 Map<String, LuCode> oldLuCodeMap = new HashMap<String, LuCode>();
 1304  0
                 for (LuCode luCode : clu.getLuCodes()) {
 1305  0
                         oldLuCodeMap.put(luCode.getId(), luCode);
 1306  
                 }
 1307  0
                 clu.getLuCodes().clear();
 1308  
 
 1309  
                 // Loop through the new list, if the item exists already update and
 1310  
                 // remove from the list
 1311  
                 // otherwise create a new entry
 1312  0
                 for (LuCodeInfo luCodeInfo : cluInfo.getLuCodes()) {
 1313  0
                         LuCode luCode = oldLuCodeMap.remove(luCodeInfo.getId());
 1314  0
                         if (luCode == null) {
 1315  0
                                 luCode = new LuCode();
 1316  
                         } else {
 1317  0
                                 if (!String.valueOf(luCode.getVersionNumber()).equals(
 1318  
                                                 luCodeInfo.getMetaInfo().getVersionInd())) {
 1319  0
                                         throw new VersionMismatchException(
 1320  
                                                         "LuCode to be updated is not the current version");
 1321  
                                 }
 1322  
                         }
 1323  
                         // Do Copy
 1324  0
                         luCode.setAttributes(LuServiceAssembler.toGenericAttributes(
 1325  
                                         LuCodeAttribute.class, luCodeInfo.getAttributes(), luCode,
 1326  
                                         luDao));
 1327  0
                         BeanUtils.copyProperties(luCodeInfo, luCode, new String[] {
 1328  
                                         "attributes", "metaInfo" });
 1329  0
                         luCode.setDescr(luCodeInfo.getDescr());
 1330  0
                         luCode.setClu(clu);
 1331  0
                         clu.getLuCodes().add(luCode);
 1332  0
                 }
 1333  
 
 1334  
                 // Now delete anything left over
 1335  0
                 for (Entry<String, LuCode> entry : oldLuCodeMap.entrySet()) {
 1336  0
                         luDao.delete(entry.getValue());
 1337  
                 }
 1338  
 
 1339  
                 // Update the list of AtpTypeKeys
 1340  
                 // Get a map of Id->object of all the currently persisted objects in the
 1341  
                 // list
 1342  0
                 Map<String, CluAtpTypeKey> oldOfferedAtpTypesMap = new HashMap<String, CluAtpTypeKey>();
 1343  0
                 for (CluAtpTypeKey cluAtpTypeKey : clu.getOfferedAtpTypes()) {
 1344  0
                         oldOfferedAtpTypesMap.put(cluAtpTypeKey.getAtpTypeKey(),
 1345  
                                         cluAtpTypeKey);
 1346  
                 }
 1347  0
                 clu.getOfferedAtpTypes().clear();
 1348  
 
 1349  
                 // Loop through the new list, if the item exists already update and
 1350  
                 // remove from the list
 1351  
                 // otherwise create a new entry
 1352  0
                 for (String atpTypeKey : cluInfo.getOfferedAtpTypes()) {
 1353  0
                         CluAtpTypeKey cluAtpTypeKey = oldOfferedAtpTypesMap
 1354  
                                         .remove(atpTypeKey);
 1355  0
                         if (cluAtpTypeKey == null) {
 1356  0
                                 cluAtpTypeKey = new CluAtpTypeKey();
 1357  
                         }
 1358  
                         // Do Copy
 1359  0
                         cluAtpTypeKey.setAtpTypeKey(atpTypeKey);
 1360  0
                         cluAtpTypeKey.setClu(clu);
 1361  0
                         clu.getOfferedAtpTypes().add(cluAtpTypeKey);
 1362  0
                 }
 1363  
 
 1364  
                 // Now delete anything left over
 1365  0
                 for (Entry<String, CluAtpTypeKey> entry : oldOfferedAtpTypesMap
 1366  
                                 .entrySet()) {
 1367  0
                         luDao.delete(entry.getValue());
 1368  
                 }
 1369  
 
 1370  0
                 if (cluInfo.getFeeInfo() != null) {
 1371  0
                         if (clu.getFee() == null) {
 1372  0
                                 clu.setFee(LuServiceAssembler.toCluFee(clu, false, cluInfo
 1373  
                                                 .getFeeInfo(), luDao));
 1374  
                         } else {
 1375  0
                                 clu.setFee(LuServiceAssembler.toCluFee(clu, true, cluInfo
 1376  
                                                 .getFeeInfo(), luDao));
 1377  
                         }
 1378  0
                 } else if (clu.getFee() != null) {
 1379  0
                         luDao.delete(clu.getFee());
 1380  0
                         clu.setFee(null);
 1381  
                 }
 1382  
 
 1383  0
                 if (cluInfo.getAccountingInfo() != null) {
 1384  0
                         if (clu.getAccounting() == null) {
 1385  0
                                 clu.setAccounting(new CluAccounting());
 1386  
                         }
 1387  0
                         clu.getAccounting().setAttributes(
 1388  
                                         LuServiceAssembler.toGenericAttributes(
 1389  
                                                         CluAccountingAttribute.class, cluInfo
 1390  
                                                                         .getAccountingInfo().getAttributes(), clu
 1391  
                                                                         .getAccounting(), luDao));
 1392  0
                         clu.getAccounting().setAffiliatedOrgs(LuServiceAssembler
 1393  
                                         .toAffiliatedOrgs(true, clu.getAccounting().getAffiliatedOrgs(),
 1394  
                                                         cluInfo.getAccountingInfo().getAffiliatedOrgs(),
 1395  
                                                         luDao));
 1396  
 
 1397  0
                 } else if (clu.getAccounting() != null) {
 1398  0
                         clu.setAccounting(null);
 1399  
                 }
 1400  
 
 1401  0
                 clu.setAttributes(LuServiceAssembler.toGenericAttributes(
 1402  
                                 CluAttribute.class, cluInfo.getAttributes(), clu, luDao));
 1403  
 
 1404  0
                 if (cluInfo.getIntensity() != null) {
 1405  0
                         if (clu.getIntensity() == null) {
 1406  0
                                 clu.setIntensity(new Amount());
 1407  
                         }
 1408  0
                         BeanUtils
 1409  
                                         .copyProperties(cluInfo.getIntensity(), clu.getIntensity());
 1410  0
                 } else if (clu.getIntensity() != null) {
 1411  0
                         luDao.delete(clu.getIntensity());
 1412  
                 }
 1413  
 
 1414  
                 // Update the list of campusLocations
 1415  
                 // Get a map of Id->object of all the currently persisted objects in the
 1416  
                 // list
 1417  0
                 Map<String, CluCampusLocation> oldLocationsMap = new HashMap<String, CluCampusLocation>();
 1418  0
                 for (CluCampusLocation campus : clu.getCampusLocations()) {
 1419  0
                         oldLocationsMap.put(campus.getCampusLocation(), campus);
 1420  
                 }
 1421  0
                 clu.getCampusLocations().clear();
 1422  
 
 1423  
                 // Loop through the new list, if the item exists already update and
 1424  
                 // remove from the list
 1425  
                 // otherwise create a new entry
 1426  0
                 for (String locationName : cluInfo.getCampusLocations()) {
 1427  0
                         CluCampusLocation location = oldLocationsMap.remove(locationName);
 1428  0
                         if (location == null) {
 1429  0
                                 location = new CluCampusLocation();
 1430  
                         }
 1431  
                         // Do Copy
 1432  0
                         location.setCampusLocation(locationName);
 1433  0
                         location.setClu(clu);
 1434  0
                         clu.getCampusLocations().add(location);
 1435  0
                 }
 1436  
 
 1437  
                 // Now delete anything left over
 1438  0
                 for (Entry<String, CluCampusLocation> entry : oldLocationsMap
 1439  
                                 .entrySet()) {
 1440  0
                         luDao.delete(entry.getValue());
 1441  
                 }
 1442  
 
 1443  
                 // Update the List of accreditations
 1444  
                 // Get a map of Id->object of all the currently persisted objects in the
 1445  
                 // list
 1446  0
                 Map<String, CluAccreditation> oldAccreditationMap = new HashMap<String, CluAccreditation>();
 1447  0
                 for (CluAccreditation cluAccreditation : clu.getAccreditations()) {
 1448  0
                         oldAccreditationMap.put(cluAccreditation.getId(),
 1449  
                                         cluAccreditation);
 1450  
                 }
 1451  0
                 clu.getAccreditations().clear();
 1452  
 
 1453  
                 // Loop through the new list, if the item exists already update and
 1454  
                 // remove from the list
 1455  
                 // otherwise create a new entry
 1456  0
                 for (AccreditationInfo accreditationInfo : cluInfo.getAccreditations()) {
 1457  0
                         CluAccreditation cluAccreditation = null;
 1458  0
                         if(accreditationInfo.getId()!=null){
 1459  0
                                 cluAccreditation = oldAccreditationMap.remove(accreditationInfo.getId());
 1460  
                         }
 1461  
                                         
 1462  0
                         if (cluAccreditation == null) {
 1463  0
                                 cluAccreditation = new CluAccreditation();
 1464  
                         }
 1465  
                         // Do Copy
 1466  0
                         BeanUtils.copyProperties(accreditationInfo, cluAccreditation,
 1467  
                                         new String[] { "attributes" });
 1468  0
                         cluAccreditation.setAttributes(LuServiceAssembler
 1469  
                                         .toGenericAttributes(CluAccreditationAttribute.class,
 1470  
                                                         accreditationInfo.getAttributes(),
 1471  
                                                         cluAccreditation, luDao));
 1472  0
                         clu.getAccreditations().add(cluAccreditation);
 1473  0
                 }
 1474  
 
 1475  
                 // Now delete anything left over
 1476  0
                 for (Entry<String, CluAccreditation> entry : oldAccreditationMap
 1477  
                                 .entrySet()) {
 1478  0
                         luDao.delete(entry.getValue());
 1479  
                 }
 1480  
 
 1481  
                 // Update the List of alternate admin orgs
 1482  
                 // Get a map of Id->object of all the currently persisted objects in the
 1483  
                 // list
 1484  0
                 Map<String, CluAdminOrg> oldAdminOrgsMap = new HashMap<String, CluAdminOrg>();
 1485  0
                 if(clu.getAdminOrgs()!=null){
 1486  0
                         for (CluAdminOrg cluOrg : clu.getAdminOrgs()) {
 1487  0
                                 oldAdminOrgsMap.put(cluOrg.getId(), cluOrg);
 1488  
                         }
 1489  
                 }
 1490  0
                 clu.setAdminOrgs(new ArrayList<CluAdminOrg>());
 1491  
 
 1492  
                 // Loop through the new list, if the item exists already update and
 1493  
                 // remove from the list
 1494  
                 // otherwise create a new entry
 1495  0
                 for (AdminOrgInfo orgInfo : cluInfo.getAdminOrgs()) {
 1496  0
                         CluAdminOrg cluOrg = null;
 1497  0
                         if(orgInfo.getId() != null){
 1498  0
                                 cluOrg = oldAdminOrgsMap.remove(orgInfo.getId());
 1499  
                         }
 1500  
                         
 1501  0
                         if (cluOrg == null) {
 1502  0
                                 cluOrg = new CluAdminOrg();
 1503  
                         }
 1504  
                         
 1505  
                         // Do Copy
 1506  0
                         BeanUtils.copyProperties(orgInfo, cluOrg,
 1507  
                                         new String[] { "attributes","id" });
 1508  0
                         cluOrg.setAttributes(LuServiceAssembler.toGenericAttributes(
 1509  
                                         CluAdminOrgAttribute.class, orgInfo.getAttributes(),
 1510  
                                         cluOrg, luDao));
 1511  0
                         cluOrg.setClu(clu);
 1512  0
                         clu.getAdminOrgs().add(cluOrg);
 1513  0
                 }
 1514  
 
 1515  0
                 for (Entry<String, CluAdminOrg> entry : oldAdminOrgsMap.entrySet()) {
 1516  0
                         luDao.delete(entry.getValue());
 1517  
                 }
 1518  
 
 1519  
                 // Now copy all not standard properties
 1520  0
                 BeanUtils.copyProperties(cluInfo, clu, new String[] { "luType",
 1521  
                                 "officialIdentifier", "alternateIdentifiers", "descr",
 1522  
                                 "luCodes", "primaryInstructor", "instructors", "stdDuration",
 1523  
                                 "offeredAtpTypes", "feeInfo", "accountingInfo", "attributes",
 1524  
                                 "metaInfo","intensity",
 1525  
                                 "campusLocations", "accreditations",
 1526  
                                 "adminOrgs" });
 1527  0
                 Clu updated = null;
 1528  
                 try {
 1529  0
                         updated = luDao.update(clu);
 1530  0
                 } catch (Exception e) {
 1531  0
                         logger.error("Exception occured: ", e);
 1532  0
                 }
 1533  0
                 return LuServiceAssembler.toCluInfo(updated);
 1534  
         }
 1535  
 
 1536  
         @Override
 1537  
         @Transactional(readOnly=false)
 1538  
         public StatusInfo deleteClu(String cluId) throws DoesNotExistException,
 1539  
                         InvalidParameterException, MissingParameterException,
 1540  
                         DependentObjectsExistException, OperationFailedException,
 1541  
                         PermissionDeniedException {
 1542  0
                 checkForMissingParameter(cluId, "cluId");
 1543  
 
 1544  0
                 luDao.delete(Clu.class, cluId);
 1545  
 
 1546  0
                 StatusInfo statusInfo = new StatusInfo();
 1547  0
                 statusInfo.setSuccess(true);
 1548  
 
 1549  0
                 return statusInfo;
 1550  
         }
 1551  
 
 1552  
         @Override
 1553  
         @Transactional(readOnly=false)
 1554  
         public CluInfo updateCluState(String cluId, String luState)
 1555  
                         throws DataValidationErrorException, DoesNotExistException,
 1556  
                         InvalidParameterException, MissingParameterException,
 1557  
                         OperationFailedException, PermissionDeniedException {
 1558  
                 // Check Missing params
 1559  0
                 checkForMissingParameter(cluId, "cluId");
 1560  0
                 checkForMissingParameter(luState, "luState");
 1561  0
                 Clu clu = luDao.fetch(Clu.class, cluId);
 1562  0
                 clu.setState(luState);
 1563  0
                 Clu updated = luDao.update(clu);
 1564  0
                 return LuServiceAssembler.toCluInfo(updated);
 1565  
         }
 1566  
 
 1567  
         @Override
 1568  
         public List<ValidationResultInfo> validateCluCluRelation(
 1569  
                         String validationType, CluCluRelationInfo cluCluRelationInfo)
 1570  
                         throws DoesNotExistException, InvalidParameterException,
 1571  
                         MissingParameterException, OperationFailedException {
 1572  0
                 checkForMissingParameter(validationType, "validationType");
 1573  0
                 checkForMissingParameter(cluCluRelationInfo, "cluCluRelationInfo");
 1574  
 
 1575  0
         ObjectStructureDefinition objStructure = this.getObjectStructure(CluCluRelationInfo.class.getName());
 1576  0
         Validator defaultValidator = validatorFactory.getValidator();
 1577  0
         List<ValidationResultInfo> validationResults = defaultValidator.validateObject(cluCluRelationInfo, objStructure);
 1578  
         
 1579  0
         return validationResults;
 1580  
         }
 1581  
 
 1582  
         @Override
 1583  
         @Transactional(readOnly=false)
 1584  
         public CluCluRelationInfo createCluCluRelation(String cluId,
 1585  
                         String relatedCluId, String luLuRelationTypeKey,
 1586  
                         CluCluRelationInfo cluCluRelationInfo)
 1587  
                         throws AlreadyExistsException, DataValidationErrorException,
 1588  
                         DoesNotExistException, InvalidParameterException,
 1589  
                         MissingParameterException, OperationFailedException,
 1590  
                         PermissionDeniedException, CircularRelationshipException {
 1591  0
                 checkForMissingParameter(cluId, "cluId");
 1592  0
                 checkForMissingParameter(relatedCluId, "relatedCluId");
 1593  0
                 checkForMissingParameter(luLuRelationTypeKey, "luLuRelationTypeKey");
 1594  0
                 checkForMissingParameter(cluCluRelationInfo, "cluCluRelationInfo");
 1595  
 
 1596  0
                 if (cluId.equals(relatedCluId)) {
 1597  0
                         throw new CircularRelationshipException(
 1598  
                                         "Can not relate a Clu to itself");
 1599  
                 }
 1600  
 
 1601  
                 // Validate CluCluRelationInfo
 1602  0
                 List<ValidationResultInfo> val = validateCluCluRelation("SYSTEM", cluCluRelationInfo);
 1603  0
                 if(null != val && val.size() > 0) {
 1604  0
                         throw new DataValidationErrorException("Validation error!", val);
 1605  
                 }
 1606  
 
 1607  
 
 1608  0
                 Clu clu = luDao.fetch(Clu.class, cluId);
 1609  0
                 Clu relatedClu = luDao.fetch(Clu.class, relatedCluId);
 1610  
 
 1611  0
                 CluCluRelation cluCluRelation = new CluCluRelation();
 1612  0
                 BeanUtils.copyProperties(cluCluRelationInfo, cluCluRelation,
 1613  
                                 new String[] { "cluId", "relatedCluId",
 1614  
                                                 "isCluRelationRequired", "attributes", "metaInfo" });
 1615  
 
 1616  0
                 cluCluRelation.setClu(clu);
 1617  0
                 cluCluRelation.setRelatedClu(relatedClu);
 1618  0
                 cluCluRelation.setCluRelationRequired(cluCluRelationInfo
 1619  
                                 .getIsCluRelationRequired() == null ? true : cluCluRelationInfo
 1620  
                                 .getIsCluRelationRequired()); // TODO maybe this is unnecessary,
 1621  
                 // contract specifies not null
 1622  0
                 cluCluRelation.setAttributes(LuServiceAssembler.toGenericAttributes(
 1623  
                                 CluCluRelationAttribute.class, cluCluRelationInfo
 1624  
                                                 .getAttributes(), cluCluRelation, luDao));
 1625  
 
 1626  0
                 LuLuRelationType luLuRelationType = luDao.fetch(LuLuRelationType.class,
 1627  
                                 luLuRelationTypeKey);
 1628  
 
 1629  0
                 cluCluRelation.setLuLuRelationType(luLuRelationType);
 1630  
 
 1631  0
                 luDao.create(cluCluRelation);
 1632  
 
 1633  0
                 return LuServiceAssembler.toCluCluRelationInfo(cluCluRelation);
 1634  
         }
 1635  
 
 1636  
         @Override
 1637  
         @Transactional(readOnly=false)
 1638  
         public CluCluRelationInfo updateCluCluRelation(
 1639  
                         final String cluCluRelationId,
 1640  
                         final CluCluRelationInfo cluCluRelationInfo)
 1641  
                         throws DataValidationErrorException, DoesNotExistException,
 1642  
                         InvalidParameterException, MissingParameterException,
 1643  
                         OperationFailedException, PermissionDeniedException,
 1644  
                         VersionMismatchException {
 1645  0
                 checkForMissingParameter(cluCluRelationId, "cluCluRelationId");
 1646  0
                 checkForMissingParameter(cluCluRelationInfo, "cluCluRelationInfo");
 1647  
 
 1648  
                 // Validate CluCluRelationInfo
 1649  0
                 List<ValidationResultInfo> val = validateCluCluRelation("SYSTEM", cluCluRelationInfo);
 1650  0
                 if(null != val && val.size() > 0) {
 1651  0
                         throw new DataValidationErrorException("Validation error!", val);
 1652  
                 }
 1653  
 
 1654  0
                 final CluCluRelation cluCluRelation = luDao.fetch(CluCluRelation.class,
 1655  
                                 cluCluRelationId);
 1656  0
                 BeanUtils.copyProperties(cluCluRelationInfo, cluCluRelation,
 1657  
                                 new String[] { "cluId", "relatedCluId",
 1658  
                                                 "isCluRelationRequired", "attributes", "metaInfo" });
 1659  
 
 1660  0
                 cluCluRelation.setClu(luDao.fetch(Clu.class, cluCluRelationInfo
 1661  
                                 .getCluId()));
 1662  0
                 cluCluRelation.setRelatedClu(luDao.fetch(Clu.class, cluCluRelationInfo
 1663  
                                 .getRelatedCluId()));
 1664  0
                 cluCluRelation.setCluRelationRequired(cluCluRelationInfo
 1665  
                                 .getIsCluRelationRequired() == null ? true : cluCluRelationInfo
 1666  
                                 .getIsCluRelationRequired()); // TODO maybe this is unnecessary,
 1667  
                 // contract specifies not null
 1668  0
                 cluCluRelation.setAttributes(LuServiceAssembler.toGenericAttributes(
 1669  
                                 CluCluRelationAttribute.class, cluCluRelationInfo
 1670  
                                                 .getAttributes(), cluCluRelation, luDao));
 1671  
 
 1672  0
                 cluCluRelation.setLuLuRelationType(luDao.fetch(LuLuRelationType.class,
 1673  
                                 cluCluRelationInfo.getType()));
 1674  
 
 1675  0
                 final CluCluRelation update = luDao.update(cluCluRelation);
 1676  
 
 1677  0
                 return LuServiceAssembler.toCluCluRelationInfo(update);
 1678  
         }
 1679  
 
 1680  
         @Override
 1681  
         @Transactional(readOnly=false)
 1682  
         public StatusInfo deleteCluCluRelation(String cluCluRelationId)
 1683  
                         throws DoesNotExistException, InvalidParameterException,
 1684  
                         MissingParameterException, OperationFailedException,
 1685  
                         PermissionDeniedException {
 1686  0
                 checkForMissingParameter(cluCluRelationId, "cluCluRelationId");
 1687  
 
 1688  0
                 luDao.delete(CluCluRelation.class, cluCluRelationId);
 1689  
 
 1690  0
                 StatusInfo statusInfo = new StatusInfo();
 1691  0
                 statusInfo.setSuccess(true);
 1692  
 
 1693  0
                 return statusInfo;
 1694  
         }
 1695  
 
 1696  
         @Override
 1697  
         public List<ValidationResultInfo> validateCluPublication(
 1698  
                         String validationType, CluPublicationInfo cluPublicationInfo)
 1699  
                         throws DoesNotExistException, InvalidParameterException,
 1700  
                         MissingParameterException, OperationFailedException {
 1701  
 
 1702  0
                 checkForMissingParameter(validationType, "validationType");
 1703  0
                 checkForMissingParameter(cluPublicationInfo, "cluPublicationInfo");
 1704  
                 
 1705  0
         ObjectStructureDefinition objStructure = this.getObjectStructure(CluPublicationInfo.class.getName());
 1706  0
         Validator defaultValidator = validatorFactory.getValidator();
 1707  0
         List<ValidationResultInfo> validationResults = defaultValidator.validateObject(cluPublicationInfo, objStructure);
 1708  0
         return validationResults;
 1709  
         }
 1710  
 
 1711  
         @Override
 1712  
         @Transactional(readOnly=false)
 1713  
         public CluPublicationInfo createCluPublication(String cluId,
 1714  
                         String luPublicationType, CluPublicationInfo cluPublicationInfo)
 1715  
                         throws AlreadyExistsException, DataValidationErrorException,
 1716  
                         InvalidParameterException, MissingParameterException,
 1717  
                         OperationFailedException, PermissionDeniedException {
 1718  0
                 checkForMissingParameter(cluId, "cluId");
 1719  0
                 checkForMissingParameter(luPublicationType, "luPublicationType");
 1720  0
                 checkForMissingParameter(cluPublicationInfo, "cluPublicationInfo");
 1721  
                 
 1722  
                 // Validate CLU
 1723  
                 List<ValidationResultInfo> val;
 1724  
                 try {
 1725  0
                         val = validateCluPublication("SYSTEM", cluPublicationInfo);
 1726  0
                         if(null != val && val.size() > 0) {
 1727  0
                                 throw new DataValidationErrorException("Validation error!", val);
 1728  
                         }
 1729  0
                 } catch (DoesNotExistException e) {
 1730  0
                         throw new OperationFailedException("Error creating clu",e);
 1731  0
                 }
 1732  
 
 1733  
                 
 1734  0
                 CluPublication cluPub = new CluPublication();
 1735  
                 Clu clu;
 1736  
                 try {
 1737  0
                         clu = luDao.fetch(Clu.class, cluId);
 1738  0
                 } catch (DoesNotExistException e) {
 1739  0
                         throw new InvalidParameterException("Clu does not exist for id:"+cluId);
 1740  0
                 }
 1741  
                 
 1742  
                 CluPublicationType type;
 1743  
                 try{
 1744  0
                         type = luDao.fetch(CluPublicationType.class, luPublicationType);
 1745  0
                 } catch (DoesNotExistException e) {
 1746  0
                         throw new InvalidParameterException("CluPublication Type does not exist for id:" + luPublicationType);
 1747  0
                 }
 1748  
                 
 1749  0
                 cluPub.setClu(clu);
 1750  0
                 cluPub.setId(cluPublicationInfo.getId());
 1751  0
                 cluPub.setEndCycle(cluPublicationInfo.getEndCycle());
 1752  0
                 cluPub.setStartCycle(cluPublicationInfo.getStartCycle());
 1753  0
                 cluPub.setEffectiveDate(cluPublicationInfo.getEffectiveDate());
 1754  0
                 cluPub.setExpirationDate(cluPublicationInfo.getExpirationDate());
 1755  0
                 cluPub.setState(cluPublicationInfo.getState());
 1756  0
                 cluPub.setType(type);
 1757  0
                 cluPub.setAttributes(LuServiceAssembler.toGenericAttributes(CluPublicationAttribute.class, cluPublicationInfo.getAttributes(), cluPub, luDao));
 1758  0
                 cluPub.setVariants(LuServiceAssembler.toCluPublicationVariants(cluPublicationInfo.getVariants(), cluPub, luDao));
 1759  
 
 1760  0
         luDao.create(cluPub);
 1761  
 
 1762  0
                 return LuServiceAssembler.toCluPublicationInfo(cluPub);
 1763  
         }
 1764  
 
 1765  
         @Override
 1766  
         @Transactional(readOnly=false)
 1767  
         public CluPublicationInfo updateCluPublication(String cluPublicationId,
 1768  
                         CluPublicationInfo cluPublicationInfo)
 1769  
                         throws DataValidationErrorException, DoesNotExistException,
 1770  
                         InvalidParameterException, MissingParameterException,
 1771  
                         OperationFailedException, PermissionDeniedException,
 1772  
                         VersionMismatchException {
 1773  0
                 checkForMissingParameter(cluPublicationId, "cluPublicationId");
 1774  0
                 checkForMissingParameter(cluPublicationInfo, "cluPublicationInfo");
 1775  
                 
 1776  
                 // Validate CLU
 1777  
                 List<ValidationResultInfo> val;
 1778  
                 try {
 1779  0
                         val = validateCluPublication("SYSTEM", cluPublicationInfo);
 1780  0
                         if(null != val && val.size() > 0) {
 1781  0
                                 throw new DataValidationErrorException("Validation error!", val);
 1782  
                         }
 1783  0
                 } catch (DoesNotExistException e) {
 1784  0
                         throw new OperationFailedException("Error creating clu",e);
 1785  0
                 }
 1786  
                 
 1787  0
                 CluPublication cluPub = luDao.fetch(CluPublication.class, cluPublicationId);
 1788  
                 
 1789  0
                 if (!String.valueOf(cluPub.getVersionNumber()).equals(
 1790  
                                 cluPublicationInfo.getMetaInfo().getVersionInd())) {
 1791  0
                         throw new VersionMismatchException(
 1792  
                                         "CluPublication to be updated is not the current version");
 1793  
                 }
 1794  
                 
 1795  
                 Clu clu;
 1796  
                 try {
 1797  0
                         clu = luDao.fetch(Clu.class, cluPublicationInfo.getCluId());
 1798  0
                 } catch (DoesNotExistException e) {
 1799  0
                         throw new InvalidParameterException("Clu does not exist for id:"+cluPublicationInfo.getCluId());
 1800  0
                 }
 1801  
                 
 1802  
                 CluPublicationType type;
 1803  
                 try{
 1804  0
                         type = luDao.fetch(CluPublicationType.class, cluPublicationInfo.getType());
 1805  0
                 } catch (DoesNotExistException e) {
 1806  0
                         throw new InvalidParameterException("CluPublication Type does not exist for id:" + cluPublicationInfo.getType());
 1807  0
                 }
 1808  
 
 1809  
         // Update the list of variants
 1810  
         // Get a map of Id->object of all the currently persisted objects in the
 1811  
         // list
 1812  0
         Map<String, CluPublicationVariant> oldVariantMap = new HashMap<String, CluPublicationVariant>();
 1813  0
         for (CluPublicationVariant variant : cluPub.getVariants()) {
 1814  0
             oldVariantMap.put(variant.getKey(), variant);
 1815  
         }
 1816  0
         cluPub.getVariants().clear();
 1817  
 
 1818  
         // Loop through the new list, if the item exists already update and
 1819  
         // remove from the list otherwise create a new entry
 1820  0
         CluPublicationVariant variant = null;
 1821  0
         for (FieldInfo fieldInfo : cluPublicationInfo.getVariants()) {
 1822  0
             if (!oldVariantMap.containsKey(fieldInfo.getId())) {
 1823  
                 // New variant key
 1824  0
                 variant = new CluPublicationVariant();
 1825  0
                 variant.setKey(fieldInfo.getId());
 1826  0
                 variant.setValue(fieldInfo.getValue());
 1827  
             } else {
 1828  
                 // Update existing variant
 1829  0
                 variant = oldVariantMap.get(fieldInfo.getId());
 1830  0
                 variant.setValue(fieldInfo.getValue());
 1831  0
                 oldVariantMap.remove(fieldInfo.getId());
 1832  
             }
 1833  
 
 1834  0
             cluPub.getVariants().add(variant);
 1835  
         }
 1836  
 
 1837  
         // Now delete anything left over
 1838  0
         for (Entry<String, CluPublicationVariant> entry : oldVariantMap.entrySet()) {
 1839  0
             luDao.delete(entry.getValue());
 1840  
         }
 1841  
        
 1842  0
                 cluPub.setClu(clu);
 1843  0
                 cluPub.setEndCycle(cluPublicationInfo.getEndCycle());
 1844  0
                 cluPub.setStartCycle(cluPublicationInfo.getStartCycle());
 1845  0
                 cluPub.setEffectiveDate(cluPublicationInfo.getEffectiveDate());
 1846  0
                 cluPub.setExpirationDate(cluPublicationInfo.getExpirationDate());
 1847  0
                 cluPub.setState(cluPublicationInfo.getState());
 1848  0
                 cluPub.setType(type);
 1849  0
                 cluPub.setAttributes(LuServiceAssembler.toGenericAttributes(CluPublicationAttribute.class, cluPublicationInfo.getAttributes(), cluPub, luDao));
 1850  
 
 1851  0
         CluPublication updated = luDao.update(cluPub);
 1852  
 
 1853  0
                 return LuServiceAssembler.toCluPublicationInfo(updated);
 1854  
         }
 1855  
 
 1856  
         @Override
 1857  
         @Transactional(readOnly=false)
 1858  
         public StatusInfo deleteCluPublication(String cluPublicationId)
 1859  
                         throws DoesNotExistException, InvalidParameterException,
 1860  
                         MissingParameterException, DependentObjectsExistException,
 1861  
                         OperationFailedException, PermissionDeniedException {
 1862  0
                 checkForMissingParameter(cluPublicationId, "cluPublicationId");
 1863  
 
 1864  0
                 luDao.delete(CluPublication.class, cluPublicationId);
 1865  
 
 1866  0
                 StatusInfo statusInfo = new StatusInfo();
 1867  0
                 statusInfo.setSuccess(true);
 1868  
 
 1869  0
                 return statusInfo;        }
 1870  
 
 1871  
         @Override
 1872  
         public List<ValidationResultInfo> validateCluResult(String validationType,
 1873  
                         CluResultInfo cluResultInfo) throws DoesNotExistException,
 1874  
                         InvalidParameterException, MissingParameterException,
 1875  
                         OperationFailedException {
 1876  0
                 checkForMissingParameter(validationType, "validationType");
 1877  0
                 checkForMissingParameter(cluResultInfo, "cluResultInfo");
 1878  
 
 1879  0
         ObjectStructureDefinition objStructure = this.getObjectStructure(CluResultInfo.class.getName());
 1880  0
         Validator defaultValidator = validatorFactory.getValidator();
 1881  0
         List<ValidationResultInfo> validationResults = defaultValidator.validateObject(cluResultInfo, objStructure);
 1882  0
         return validationResults;
 1883  
         }
 1884  
 
 1885  
         @Override
 1886  
         @Transactional(readOnly=false)
 1887  
         public CluResultInfo createCluResult(String cluId, String cluResultTypeKey,
 1888  
                         CluResultInfo cluResultInfo) throws AlreadyExistsException,
 1889  
                         DataValidationErrorException, InvalidParameterException,
 1890  
                         MissingParameterException, OperationFailedException,
 1891  
                         PermissionDeniedException, DoesNotExistException {
 1892  
 
 1893  0
                 checkForMissingParameter(cluId, "cluId");
 1894  0
                 checkForMissingParameter(cluResultTypeKey, "cluResultTypeKey");
 1895  0
                 checkForMissingParameter(cluResultInfo, "cluResultInfo");
 1896  
 
 1897  
                 // Validate CluResult
 1898  0
                 List<ValidationResultInfo> val = validateCluResult("SYSTEM", cluResultInfo);
 1899  0
                 if(null != val && val.size() > 0) {
 1900  0
                         throw new DataValidationErrorException("Validation error!", val);
 1901  
                 }
 1902  
 
 1903  0
                 cluResultInfo.setType(cluResultTypeKey);
 1904  0
                 cluResultInfo.setCluId(cluId);
 1905  
 
 1906  0
                 List<ResultOption> resOptList = new ArrayList<ResultOption>();
 1907  0
                 for (ResultOptionInfo resOptInfo : cluResultInfo.getResultOptions()) {
 1908  0
                         ResultOption resOpt = new ResultOption();
 1909  0
                         BeanUtils.copyProperties(resOptInfo, resOpt, new String[] { "id",
 1910  
                                         "metaInfo", "resultUsageType", "desc" });
 1911  
 
 1912  0
                         if(resOptInfo.getResultUsageTypeKey() != null) {
 1913  0
                                 ResultUsageType resUsageType = luDao.fetch(ResultUsageType.class,
 1914  
                                                 resOptInfo.getResultUsageTypeKey());
 1915  0
                                 resOpt.setResultUsageType(resUsageType);
 1916  
                         }
 1917  0
                         resOpt.setDesc(LuServiceAssembler.toRichText(LuRichText.class, resOptInfo.getDesc()));
 1918  0
                         luDao.create(resOpt);
 1919  0
                         resOptList.add(resOpt);
 1920  0
                 }
 1921  
 
 1922  0
                 CluResult cluResult = new CluResult();
 1923  0
                 BeanUtils.copyProperties(cluResultInfo, cluResult, new String[] { "id",
 1924  
                                 "desc", "resultOptions", "metaInfo" });
 1925  
 
 1926  0
                 cluResult.setDesc(LuServiceAssembler
 1927  
                                 .toRichText(LuRichText.class, cluResultInfo.getDesc()));
 1928  0
                 cluResult.setResultOptions(resOptList);
 1929  
 
 1930  0
                 Clu clu = luDao.fetch(Clu.class, cluId);
 1931  0
                 cluResult.setClu(clu);
 1932  
 
 1933  0
                 CluResultType type = luDao.fetch(CluResultType.class, cluResultTypeKey);
 1934  0
                 cluResult.setCluResultType(type);
 1935  
 
 1936  0
                 luDao.create(cluResult);
 1937  
 
 1938  0
                 return LuServiceAssembler.toCluResultInfo(cluResult);
 1939  
         }
 1940  
 
 1941  
         @Override
 1942  
         @Transactional(readOnly=false)
 1943  
         public CluResultInfo updateCluResult(String cluResultId,
 1944  
                         CluResultInfo cluResultInfo) throws DataValidationErrorException,
 1945  
                         DoesNotExistException, InvalidParameterException,
 1946  
                         MissingParameterException, OperationFailedException,
 1947  
                         PermissionDeniedException, VersionMismatchException {
 1948  
 
 1949  0
                 checkForMissingParameter(cluResultId, "cluResultId");
 1950  0
                 checkForMissingParameter(cluResultInfo, "cluResultInfo");
 1951  
 
 1952  
                 // Validate CluResult
 1953  0
                 List<ValidationResultInfo> val = validateCluResult("SYSTEM", cluResultInfo);
 1954  0
                 if(null != val && val.size() > 0) {
 1955  0
                         throw new DataValidationErrorException("Validation error!", val);
 1956  
                 }
 1957  
 
 1958  0
                 CluResult result = luDao.fetch(CluResult.class, cluResultId);
 1959  0
                 if (!String.valueOf(result.getVersionNumber()).equals(
 1960  
                                 cluResultInfo.getMetaInfo().getVersionInd())) {
 1961  0
                         throw new VersionMismatchException(
 1962  
                                         "CluResult to be updated is not the current version");
 1963  
                 }
 1964  
 
 1965  
                 // Update the list of resultoptions
 1966  
                 // Get a map of Id->object of all the currently persisted objects in the
 1967  
                 // list
 1968  0
                 Map<String, ResultOption> oldResultOptionMap = new HashMap<String, ResultOption>();
 1969  0
                 for (ResultOption opt : result.getResultOptions()) {
 1970  0
                         oldResultOptionMap.put(opt.getId(), opt);
 1971  
                 }
 1972  0
                 result.getResultOptions().clear();
 1973  
 
 1974  
                 // Loop through the new list, if the item exists already update and
 1975  
                 // remove from the list otherwise create a new entry
 1976  0
                 for (ResultOptionInfo resOptInfo : cluResultInfo.getResultOptions()) {
 1977  0
                         ResultOption opt = oldResultOptionMap.remove(resOptInfo.getId());
 1978  0
                         if (opt == null) {
 1979  
                                 // New result option
 1980  0
                                 opt = new ResultOption();
 1981  
                                 // Copy properties
 1982  0
                                 BeanUtils.copyProperties(resOptInfo, opt, new String[] {
 1983  
                                                 "resultUsageType", "desc" });
 1984  
                         } else {
 1985  
                                 // Get existing result option
 1986  0
                                 opt = luDao.fetch(ResultOption.class, resOptInfo.getId());
 1987  
                                 // Copy properties
 1988  0
                                 BeanUtils.copyProperties(resOptInfo, opt, new String[] {
 1989  
                                                 "id", "resultUsageType", "desc" });
 1990  
                         }
 1991  0
                         if(resOptInfo.getResultUsageTypeKey() != null && !resOptInfo.getResultUsageTypeKey().isEmpty()) {
 1992  0
                                 ResultUsageType resUsageType = luDao.fetch(ResultUsageType.class,
 1993  
                                                 resOptInfo.getResultUsageTypeKey());
 1994  0
                                 opt.setResultUsageType(resUsageType);
 1995  
                         }
 1996  0
                         opt.setDesc(LuServiceAssembler.toRichText(LuRichText.class, resOptInfo.getDesc()));
 1997  0
                         result.getResultOptions().add(opt);
 1998  0
                 }
 1999  
 
 2000  
                 // Now delete anything left over
 2001  0
                 for (Entry<String, ResultOption> entry : oldResultOptionMap.entrySet()) {
 2002  0
                         luDao.delete(entry.getValue());
 2003  
                 }
 2004  
 
 2005  0
                 BeanUtils.copyProperties(cluResultInfo, result, new String[] { "id",
 2006  
                                 "desc", "resultOptions" });
 2007  
 
 2008  0
                 result.setDesc(LuServiceAssembler.toRichText(LuRichText.class, cluResultInfo.getDesc()));
 2009  0
                 CluResultType type = luDao.fetch(CluResultType.class, cluResultInfo.getType());
 2010  0
                 result.setCluResultType(type);
 2011  
 
 2012  0
                 CluResult updated = luDao.update(result);
 2013  
 
 2014  0
                 return LuServiceAssembler.toCluResultInfo(updated);
 2015  
         }
 2016  
 
 2017  
         @Override
 2018  
         @Transactional(readOnly=false)
 2019  
         public StatusInfo deleteCluResult(String cluResultId)
 2020  
                         throws DoesNotExistException, InvalidParameterException,
 2021  
                         MissingParameterException, DependentObjectsExistException,
 2022  
                         OperationFailedException, PermissionDeniedException {
 2023  
 
 2024  0
                 checkForMissingParameter(cluResultId, "cluResultId");
 2025  
 
 2026  0
                 luDao.delete(CluResult.class, cluResultId);
 2027  
 
 2028  0
                 StatusInfo statusInfo = new StatusInfo();
 2029  0
                 statusInfo.setSuccess(true);
 2030  
 
 2031  0
                 return statusInfo;
 2032  
         }
 2033  
 
 2034  
         @Override
 2035  
         public List<ValidationResultInfo> validateCluLoRelation(
 2036  
                         String validationType, CluLoRelationInfo cluLoRelationInfo)
 2037  
                         throws DoesNotExistException, InvalidParameterException,
 2038  
                         MissingParameterException, OperationFailedException {
 2039  
 
 2040  0
                 checkForMissingParameter(validationType, "validationType");
 2041  0
                 checkForMissingParameter(cluLoRelationInfo, "cluLoRelationInfo");
 2042  
 
 2043  0
         ObjectStructureDefinition objStructure = this.getObjectStructure(CluLoRelation.class.getName());
 2044  0
         Validator defaultValidator = validatorFactory.getValidator();
 2045  0
         List<ValidationResultInfo> validationResults = defaultValidator.validateObject(cluLoRelationInfo, objStructure);
 2046  0
         return validationResults;
 2047  
         }
 2048  
 
 2049  
         @Override
 2050  
         @Transactional(readOnly=false)
 2051  
         public CluLoRelationInfo createCluLoRelation(String cluId, String loId,
 2052  
                         String cluLoRelationType, CluLoRelationInfo cluLoRelationInfo)
 2053  
                         throws AlreadyExistsException, DoesNotExistException,
 2054  
                         InvalidParameterException, MissingParameterException,
 2055  
                         OperationFailedException, PermissionDeniedException, DataValidationErrorException {
 2056  0
                 checkForMissingParameter(loId, "loId");
 2057  0
                 checkForMissingParameter(cluId, "cluId");
 2058  0
                 checkForEmptyList(cluLoRelationType, "cluLoRelationType");
 2059  0
                 checkForEmptyList(cluLoRelationInfo, "cluLoRelationInfo");
 2060  
 
 2061  
                 // Validate CluLoRelation
 2062  0
                 List<ValidationResultInfo> val = validateCluLoRelation("SYSTEM", cluLoRelationInfo);
 2063  0
                 if(null != val && val.size() > 0) {
 2064  0
                         throw new DataValidationErrorException("Validation error!", val);
 2065  
                 }
 2066  
 
 2067  0
                 Clu clu = luDao.fetch(Clu.class, cluId);
 2068  0
                 if (clu == null) {
 2069  0
                         throw new DoesNotExistException("Clu does not exist for id: "
 2070  
                                         + cluId);
 2071  
                 }
 2072  
                 
 2073  0
                 CluLoRelationType cluLoRelationTypeEntity = luDao.fetch(CluLoRelationType.class, cluLoRelationType);
 2074  0
                 if (cluLoRelationTypeEntity == null) {
 2075  0
                         throw new DoesNotExistException("CluLoRelationType does not exist for id: "
 2076  
                                         + cluLoRelationType);
 2077  
                 }
 2078  
 
 2079  
                 // Check to see if this relation already exists
 2080  0
                 List<CluLoRelation> reltns = luDao.getCluLoRelationsByCludIdAndLoId(
 2081  
                                 cluId, loId);
 2082  0
                 if (reltns.size() > 0) {
 2083  0
                         throw new AlreadyExistsException(
 2084  
                                         "Relation already exists for cluId:" + cluId + " and Lo:"
 2085  
                                                         + loId);
 2086  
                 }
 2087  
 
 2088  0
                 CluLoRelation cluLoRelation = new CluLoRelation();
 2089  0
                 BeanUtils.copyProperties(cluLoRelationInfo, cluLoRelation,
 2090  
                                 new String[] { "cluId", "attributes", "metaInfo", "type" });
 2091  
 
 2092  0
                 cluLoRelation.setClu(clu);
 2093  0
                 cluLoRelation.setAttributes(LuServiceAssembler.toGenericAttributes(
 2094  
                                 CluLoRelationAttribute.class,
 2095  
                                 cluLoRelationInfo.getAttributes(), cluLoRelation, luDao));
 2096  0
                 cluLoRelation.setType(cluLoRelationTypeEntity);
 2097  
                 
 2098  0
                 luDao.create(cluLoRelation);
 2099  
 
 2100  0
                 return LuServiceAssembler.toCluLoRelationInfo(cluLoRelation);
 2101  
         }
 2102  
 
 2103  
         @Override
 2104  
         @Transactional(readOnly=false)
 2105  
         public CluLoRelationInfo updateCluLoRelation(String cluLoRelationId,
 2106  
                         CluLoRelationInfo cluLoRelationInfo)
 2107  
                         throws DataValidationErrorException, DoesNotExistException,
 2108  
                         InvalidParameterException, MissingParameterException,
 2109  
                         OperationFailedException, PermissionDeniedException,
 2110  
                         VersionMismatchException {
 2111  0
                 checkForMissingParameter(cluLoRelationId, "cluLoRelationId");
 2112  0
                 checkForMissingParameter(cluLoRelationInfo, "cluLoRelationInfo");
 2113  
 
 2114  
                 // Validate CluLoRelation
 2115  0
                 List<ValidationResultInfo> val = validateCluLoRelation("SYSTEM", cluLoRelationInfo);
 2116  0
                 if(null != val && val.size() > 0) {
 2117  0
                         throw new DataValidationErrorException("Validation error!", val);
 2118  
                 }
 2119  
 
 2120  0
                 CluLoRelation reltn = luDao.fetch(CluLoRelation.class, cluLoRelationId);
 2121  
 
 2122  0
                 if (!String.valueOf(reltn.getVersionNumber()).equals(
 2123  
                                 cluLoRelationInfo.getMetaInfo().getVersionInd())) {
 2124  0
                         throw new VersionMismatchException(
 2125  
                                         "CluLoRelation to be updated is not the current version");
 2126  
                 }
 2127  
 
 2128  0
                 Clu clu = luDao.fetch(Clu.class, cluLoRelationInfo.getCluId());
 2129  0
                 if (clu == null) {
 2130  0
                         throw new DoesNotExistException("Clu does not exist for id: "
 2131  
                                         + cluLoRelationInfo.getCluId());
 2132  
                 }
 2133  
 
 2134  0
                 CluLoRelationType cluLoRelationTypeEntity = luDao.fetch(CluLoRelationType.class, cluLoRelationInfo.getType());
 2135  0
                 if (cluLoRelationTypeEntity == null) {
 2136  0
                         throw new DoesNotExistException("CluLoRelationType does not exist for id: "
 2137  
                                         + cluLoRelationInfo.getType());
 2138  
                 }
 2139  
                 
 2140  0
                 BeanUtils.copyProperties(cluLoRelationInfo, reltn, new String[] {
 2141  
                                 "cluId", "attributes", "metaInfo", "type"});
 2142  
 
 2143  0
                 reltn.setClu(clu);
 2144  0
                 reltn.setAttributes(LuServiceAssembler.toGenericAttributes(
 2145  
                                 CluLoRelationAttribute.class,
 2146  
                                 cluLoRelationInfo.getAttributes(), reltn, luDao));
 2147  0
                 reltn.setType(cluLoRelationTypeEntity);
 2148  0
                 CluLoRelation updated = luDao.update(reltn);
 2149  
 
 2150  0
                 return LuServiceAssembler.toCluLoRelationInfo(updated);
 2151  
         }
 2152  
 
 2153  
         @Override
 2154  
         @Transactional(readOnly=false)
 2155  
         public StatusInfo deleteCluLoRelation(String cluLoRelationId)
 2156  
                         throws DoesNotExistException, InvalidParameterException,
 2157  
                         MissingParameterException, OperationFailedException,
 2158  
                         PermissionDeniedException {
 2159  0
                 checkForMissingParameter(cluLoRelationId, "cluLoRelationId");
 2160  
 
 2161  0
                 CluLoRelation reltn = luDao.fetch(CluLoRelation.class, cluLoRelationId);
 2162  0
                 if (reltn == null) {
 2163  0
                         throw new DoesNotExistException(
 2164  
                                         "CluLoRelation does not exist for id: " + cluLoRelationId);
 2165  
                 }
 2166  
 
 2167  0
                 luDao.delete(CluLoRelation.class, cluLoRelationId);
 2168  
 
 2169  0
                 StatusInfo statusInfo = new StatusInfo();
 2170  0
                 statusInfo.setSuccess(true);
 2171  
 
 2172  0
                 return statusInfo;
 2173  
         }
 2174  
 
 2175  
         @Override
 2176  
         @Transactional(readOnly=false)
 2177  
         public StatusInfo addCluResourceRequirement(String resourceTypeKey,
 2178  
                         String cluId) throws AlreadyExistsException, DoesNotExistException,
 2179  
                         InvalidParameterException, MissingParameterException,
 2180  
                         OperationFailedException, PermissionDeniedException {
 2181  0
               throw new UnsupportedOperationException("Method not yet implemented!");
 2182  
         }
 2183  
 
 2184  
         @Override
 2185  
         @Transactional(readOnly=false)
 2186  
         public StatusInfo removeCluResourceRequirement(String resourceTypeKey,
 2187  
                         String cluId) throws DoesNotExistException,
 2188  
                         InvalidParameterException, MissingParameterException,
 2189  
                         OperationFailedException, PermissionDeniedException {
 2190  
                 // TODO Auto-generated method stub
 2191  0
                 return null;
 2192  
         }
 2193  
 
 2194  
         @Override
 2195  
         public List<ValidationResultInfo> validateCluSet(String validationType,
 2196  
                         CluSetInfo cluSetInfo) throws DoesNotExistException,
 2197  
                         InvalidParameterException, MissingParameterException,
 2198  
                         OperationFailedException {
 2199  0
                 checkForMissingParameter(validationType, "validationType");
 2200  0
                 checkForMissingParameter(cluSetInfo, "cluSetInfo");
 2201  
 
 2202  0
         ObjectStructureDefinition objStructure = this.getObjectStructure(CluSetInfo.class.getName());
 2203  0
         Validator defaultValidator = validatorFactory.getValidator();
 2204  0
         List<ValidationResultInfo> validationResults = defaultValidator.validateObject(cluSetInfo, objStructure);
 2205  0
         return validationResults;
 2206  
         }
 2207  
 
 2208  
         @Override
 2209  
         @Transactional(readOnly=false)
 2210  
         public CluSetInfo createCluSet(String cluSetType, CluSetInfo cluSetInfo)
 2211  
                         throws AlreadyExistsException, DataValidationErrorException,
 2212  
                         InvalidParameterException, MissingParameterException,
 2213  
                         OperationFailedException, PermissionDeniedException,
 2214  
                         UnsupportedActionException {
 2215  
 
 2216  0
                 checkForMissingParameter(cluSetType, "cluSetType");
 2217  0
                 checkForMissingParameter(cluSetInfo, "cluSetInfo");
 2218  
 
 2219  0
                 cluSetInfo.setType(cluSetType);
 2220  
 
 2221  0
                 validateCluSet(cluSetInfo);
 2222  
 
 2223  
                 // Validate CluSet
 2224  
                 List<ValidationResultInfo> val;
 2225  
                 try {
 2226  0
                         val = validateCluSet("SYSTEM", cluSetInfo);
 2227  0
                 } catch (DoesNotExistException e) {
 2228  0
                         throw new DataValidationErrorException("Validation error! " + e.getMessage());
 2229  0
                 }
 2230  0
                 if(null != val && val.size() > 0) {
 2231  0
                         throw new DataValidationErrorException("Validation error!", val);
 2232  
                 }
 2233  
 
 2234  0
                 List<String> cluIdList = getMembershipQuerySearchResult(cluSetInfo.getMembershipQuery());
 2235  
 
 2236  0
                 CluSet cluSet = null;
 2237  
                 try {
 2238  0
                         cluSet = LuServiceAssembler.toCluSetEntity(cluSetInfo, this.luDao);
 2239  0
                 } catch (DoesNotExistException e) {
 2240  0
                         throw new DataValidationErrorException("Creating CluSet entity failed. Clu or CluSet does not exist: " + e.getMessage());
 2241  0
                 }
 2242  
 
 2243  0
                 cluSet = luDao.create(cluSet);
 2244  
 
 2245  0
                 CluSetInfo newCluSetInfo = LuServiceAssembler.toCluSetInfo(cluSet);
 2246  
 
 2247  0
                 if(cluIdList != null) {
 2248  0
                         newCluSetInfo.getCluIds().addAll(cluIdList);
 2249  
                 }
 2250  
 
 2251  0
                 return newCluSetInfo;
 2252  
         }
 2253  
 
 2254  
         private void setMembershipQuerySearchResult(CluSetInfo cluSetInfo) throws MissingParameterException {
 2255  0
                 if(cluSetInfo.getMembershipQuery() == null) {
 2256  0
                         return;
 2257  
                 }
 2258  0
                 List<String> cluIds = getMembershipQuerySearchResult(cluSetInfo.getMembershipQuery());
 2259  0
                 cluSetInfo.getCluIds().addAll(cluIds);
 2260  0
         }
 2261  
 
 2262  
         private List<String> getMembershipQuerySearchResult(MembershipQueryInfo query) throws MissingParameterException {
 2263  0
                 if(query == null) {
 2264  0
                         return null;
 2265  
                 }
 2266  0
                 SearchRequest sr = new SearchRequest();
 2267  0
                 sr.setSearchKey(query.getSearchTypeKey());
 2268  0
                 sr.setParams(query.getQueryParamValueList());
 2269  
 
 2270  0
                 SearchResult result = search(sr);
 2271  
 
 2272  0
                 Set<String> cluIds = new HashSet<String>();
 2273  0
                 List<SearchResultRow> rows = result.getRows();
 2274  0
                 for(SearchResultRow row : rows) {
 2275  0
                         List<SearchResultCell> cells = row.getCells();
 2276  0
                         for(SearchResultCell cell : cells) {
 2277  0
                                 if(cell.getKey().equals("lu.resultColumn.luOptionalVersionIndId")&&cell.getValue()!=null) {
 2278  0
                                         cluIds.add(cell.getValue());
 2279  
                                 }
 2280  
                         }
 2281  0
                 }
 2282  0
                 return new ArrayList<String>(cluIds);
 2283  
         }
 2284  
 
 2285  
         private void validateCluSet(CluSetInfo cluSetInfo) throws UnsupportedActionException {
 2286  0
                 MembershipQueryInfo mqInfo = cluSetInfo.getMembershipQuery();
 2287  
 
 2288  0
                 if (cluSetInfo.getType() == null) {
 2289  0
                         throw new UnsupportedActionException("CluSet type cannot be null. CluSet id="+cluSetInfo.getId());
 2290  
                 }
 2291  0
                 else if(mqInfo != null && mqInfo.getSearchTypeKey() != null && !mqInfo.getSearchTypeKey().isEmpty() &&
 2292  
                                 (cluSetInfo.getCluIds().size() > 0 || cluSetInfo.getCluSetIds().size() > 0)) {
 2293  0
                         throw new UnsupportedActionException("Dynamic CluSet cannot contain Clus and/or CluSets. CluSet id="+cluSetInfo.getId());
 2294  
                 }
 2295  0
                 else if (cluSetInfo.getCluIds().size() > 0 && cluSetInfo.getCluSetIds().size() > 0) {
 2296  0
                         throw new UnsupportedActionException("CluSet cannot contain both Clus and CluSets. CluSet id="+cluSetInfo.getId());
 2297  
                 }
 2298  0
         }
 2299  
 
 2300  
         @Override
 2301  
         @Transactional(readOnly=false)
 2302  
         public CluSetInfo updateCluSet(String cluSetId, CluSetInfo cluSetInfo)
 2303  
                         throws DataValidationErrorException, DoesNotExistException,
 2304  
                         InvalidParameterException, MissingParameterException,
 2305  
                         OperationFailedException, PermissionDeniedException,
 2306  
                         VersionMismatchException, CircularRelationshipException,
 2307  
                         UnsupportedActionException {
 2308  
 
 2309  
                 // Check Missing params
 2310  0
                 checkForMissingParameter(cluSetId, "cluSetId");
 2311  0
                 checkForMissingParameter(cluSetInfo, "cluSetInfo");
 2312  
 
 2313  
                 // Validate CluSet
 2314  0
                 List<ValidationResultInfo> val = validateCluSet("SYSTEM", cluSetInfo);
 2315  0
                 if(null != val && val.size() > 0) {
 2316  0
                         throw new DataValidationErrorException("Validation error!", val);
 2317  
                 }
 2318  
 
 2319  0
                 cluSetInfo.setId(cluSetId);
 2320  
 
 2321  0
                 validateCluSet(cluSetInfo);
 2322  
 
 2323  0
                 List<String> cluIdList = getMembershipQuerySearchResult(cluSetInfo.getMembershipQuery());
 2324  
 
 2325  0
                 CluSet cluSet = luDao.fetch(CluSet.class, cluSetId);
 2326  
 
 2327  0
                 if (!cluSetInfo.getType().equals(cluSet.getType())) {
 2328  0
                         throw new UnsupportedActionException("CluSet type is set at creation time and cannot be updated. CluSet id="+cluSetId);
 2329  
                 }
 2330  
 
 2331  0
                 if (!String.valueOf(cluSet.getVersionNumber()).equals(
 2332  
                                 cluSetInfo.getMetaInfo().getVersionInd())) {
 2333  0
                         throw new VersionMismatchException(
 2334  
                                         "CluSet (id=" + cluSetId +
 2335  
                                         ") to be updated is not the current version " +
 2336  
                                         "(version=" + cluSetInfo.getMetaInfo().getVersionInd() +
 2337  
                                         "), current version="+cluSet.getVersionNumber());
 2338  
                 }
 2339  
 
 2340  
                 // update the cluIds
 2341  0
                 Map<String, CluSetJoinVersionIndClu> oldClus = new HashMap<String, CluSetJoinVersionIndClu>();
 2342  0
                 for(CluSetJoinVersionIndClu join:cluSet.getCluVerIndIds()){
 2343  0
                         oldClus.put(join.getCluVersionIndId(), join);
 2344  
                 }
 2345  
 
 2346  0
                 cluSet.getCluVerIndIds().clear();
 2347  
                 // Loop through the new list, if the item exists already update and remove from the list otherwise create a new entry
 2348  0
                 for (String newCluId : cluSetInfo.getCluIds()) {
 2349  0
                         CluSetJoinVersionIndClu join = oldClus.remove(newCluId);
 2350  0
                         if (join == null) {
 2351  0
                                 join = new CluSetJoinVersionIndClu();
 2352  0
                                 join.setCluSet(cluSet);
 2353  0
                                 join.setCluVersionIndId(newCluId);
 2354  
                         }
 2355  0
                         cluSet.getCluVerIndIds().add(join);
 2356  0
                 }
 2357  
 
 2358  
                 // Now delete anything left over
 2359  0
                 for (Entry<String, CluSetJoinVersionIndClu> entry : oldClus.entrySet()) {
 2360  0
                         luDao.delete(entry.getValue());
 2361  
                 }
 2362  
 
 2363  
         // clean up existing wrappers if any
 2364  0
         if (cluSetInfo.getId() != null) {
 2365  0
             CluSetInfo originalCluSet = getCluSetInfo(cluSetInfo.getId());
 2366  0
             List<CluSetInfo> origSubCSs = null;
 2367  0
             List<String> origSubCSIds = originalCluSet.getCluSetIds();
 2368  0
             if (origSubCSIds != null && !origSubCSIds.isEmpty()) {
 2369  0
                 origSubCSs = getCluSetInfoByIdList(origSubCSIds);
 2370  
             }
 2371  0
             if (origSubCSs != null) {
 2372  0
                 for (CluSetInfo origSubCS : origSubCSs) {
 2373  0
                     if (!origSubCS.getIsReusable()) {
 2374  0
                         deleteCluSet(origSubCS.getId());
 2375  
                     }
 2376  
                 }
 2377  
             }
 2378  
         }
 2379  
 
 2380  
                 // update the cluSetIds
 2381  0
                 if(cluSet.getCluSets()==null){
 2382  0
                         cluSet.setCluSets(new ArrayList<CluSet>());
 2383  
                 }
 2384  0
                 cluSet.setCluSets(null);
 2385  0
                 if(!cluSetInfo.getCluSetIds().isEmpty()) {
 2386  0
                         Set<String> newCluSetIds = new HashSet<String>(cluSetInfo.getCluSetIds());
 2387  0
                         if(cluSet.getCluSets()!=null){
 2388  0
                                 for (Iterator<CluSet> i = cluSet.getCluSets().iterator(); i.hasNext();) {
 2389  0
                                         if (!newCluSetIds.remove(i.next().getId())) {
 2390  0
                                                 i.remove();
 2391  
                                         }
 2392  
                                 }
 2393  
                         }
 2394  0
                         List<CluSet> cluSetList = luDao.getCluSetInfoByIdList(new ArrayList<String>(newCluSetIds));
 2395  0
                         cluSet.setCluSets(cluSetList);
 2396  
                 }
 2397  
 
 2398  0
                 BeanUtils.copyProperties(cluSetInfo, cluSet, new String[] { "descr",
 2399  
                                 "attributes", "metaInfo", "membershipQuery" });
 2400  0
                 cluSet.setAttributes(LuServiceAssembler.toGenericAttributes(
 2401  
                                 CluSetAttribute.class, cluSetInfo.getAttributes(), cluSet, luDao));
 2402  0
                 cluSet.setDescr(LuServiceAssembler.toRichText(LuRichText.class, cluSetInfo.getDescr()));
 2403  
 
 2404  0
                 MembershipQuery mq = LuServiceAssembler.toMembershipQueryEntity(cluSetInfo.getMembershipQuery());
 2405  0
                 cluSet.setMembershipQuery(mq);
 2406  
 
 2407  0
                 CluSet updated = luDao.update(cluSet);
 2408  
 
 2409  0
                 CluSetInfo updatedCluSetInfo = LuServiceAssembler.toCluSetInfo(updated);
 2410  
 
 2411  0
                 if(cluIdList != null) {
 2412  0
                         updatedCluSetInfo.getCluIds().addAll(cluIdList);
 2413  
                 }
 2414  
 
 2415  0
                 return updatedCluSetInfo;
 2416  
         }
 2417  
 
 2418  
         @Override
 2419  
         @Transactional(readOnly=false)
 2420  
         public StatusInfo deleteCluSet(String cluSetId)
 2421  
                         throws DoesNotExistException, InvalidParameterException,
 2422  
                         MissingParameterException, OperationFailedException,
 2423  
                         PermissionDeniedException {
 2424  
 
 2425  0
                 checkForMissingParameter(cluSetId, "cluSetId");
 2426  
 
 2427  0
                 luDao.delete(CluSet.class, cluSetId);
 2428  
 
 2429  0
                 StatusInfo statusInfo = new StatusInfo();
 2430  0
                 statusInfo.setSuccess(true);
 2431  
 
 2432  0
                 return statusInfo;
 2433  
         }
 2434  
 
 2435  
         @Override
 2436  
         @Transactional(readOnly=false)
 2437  
         public StatusInfo addCluSetToCluSet(String cluSetId, String addedCluSetId)
 2438  
                         throws DoesNotExistException, InvalidParameterException,
 2439  
                         MissingParameterException, OperationFailedException,
 2440  
                         PermissionDeniedException, UnsupportedActionException,
 2441  
                         CircularRelationshipException {
 2442  0
                 checkForMissingParameter(cluSetId, "cluSetId");
 2443  0
                 checkForMissingParameter(addedCluSetId, "addedCluSetId");
 2444  
 
 2445  0
                 CluSet cluSet = luDao.fetch(CluSet.class, cluSetId);
 2446  
 
 2447  0
                 checkCluSetAlreadyAdded(cluSet, addedCluSetId);
 2448  
 
 2449  0
                 CluSet addedCluSet = luDao.fetch(CluSet.class, addedCluSetId);
 2450  
 
 2451  0
                 checkCluSetCircularReference(addedCluSet, cluSetId);
 2452  
 
 2453  0
                 if(cluSet.getCluSets()==null){
 2454  0
                         cluSet.setCluSets(new ArrayList<CluSet>());
 2455  
                 }
 2456  0
                 cluSet.getCluSets().add(addedCluSet);
 2457  
 
 2458  0
                 luDao.update(cluSet);
 2459  
 
 2460  0
                 StatusInfo statusInfo = new StatusInfo();
 2461  0
                 statusInfo.setSuccess(true);
 2462  
 
 2463  0
                 return statusInfo;
 2464  
         }
 2465  
 
 2466  
         @Override
 2467  
         @Transactional(readOnly=false)
 2468  
         public StatusInfo removeCluSetFromCluSet(String cluSetId,
 2469  
                         String removedCluSetId) throws DoesNotExistException,
 2470  
                         InvalidParameterException, MissingParameterException,
 2471  
                         OperationFailedException, PermissionDeniedException,
 2472  
                         UnsupportedActionException {
 2473  
 
 2474  0
                 checkForMissingParameter(cluSetId, "cluSetId");
 2475  0
                 checkForMissingParameter(removedCluSetId, "removedCluSetId");
 2476  
 
 2477  0
                 CluSet cluSet = luDao.fetch(CluSet.class, cluSetId);
 2478  0
                 if(cluSet.getCluSets()!=null){
 2479  0
                         for (Iterator<CluSet> i = cluSet.getCluSets().iterator(); i.hasNext();) {
 2480  0
                                 CluSet childCluSet = i.next();
 2481  0
                                 if (childCluSet.getId().equals(removedCluSetId)) {
 2482  0
                                         i.remove();
 2483  0
                                         luDao.update(cluSet);
 2484  0
                                         StatusInfo statusInfo = new StatusInfo();
 2485  0
                                         statusInfo.setSuccess(true);
 2486  
         
 2487  0
                                         return statusInfo;
 2488  
                                 }
 2489  0
                         }
 2490  
                 }
 2491  
 
 2492  0
                 StatusInfo statusInfo = new StatusInfo();
 2493  0
                 statusInfo.setSuccess(false);
 2494  0
                 statusInfo.setMessage("CluSet does not contain CluSet:"
 2495  
                                 + removedCluSetId);
 2496  
 
 2497  0
                 return statusInfo;
 2498  
         }
 2499  
 
 2500  
         @Override
 2501  
         @Transactional(readOnly=false)
 2502  
         public StatusInfo addCluToCluSet(String cluId, String cluSetId)
 2503  
                         throws DoesNotExistException, InvalidParameterException,
 2504  
                         MissingParameterException, OperationFailedException,
 2505  
                         PermissionDeniedException, UnsupportedActionException {
 2506  
 
 2507  0
                 checkForMissingParameter(cluId, "cluId");
 2508  0
                 checkForMissingParameter(cluSetId, "cluSetId");
 2509  
 
 2510  0
                 CluSet cluSet = luDao.fetch(CluSet.class, cluSetId);
 2511  
 
 2512  0
                 checkCluAlreadyAdded(cluSet, cluId);
 2513  
                 
 2514  
                 try{
 2515  0
                         luDao.getCurrentCluVersionInfo(cluId, LuServiceConstants.CLU_NAMESPACE_URI);
 2516  0
                 }catch(NoResultException e){
 2517  0
                         throw new DoesNotExistException();
 2518  0
                 }
 2519  
                 
 2520  0
                 CluSetJoinVersionIndClu join = new CluSetJoinVersionIndClu();
 2521  0
                 join.setCluSet(cluSet);
 2522  0
                 join.setCluVersionIndId(cluId);
 2523  
                 
 2524  0
                 cluSet.getCluVerIndIds().add(join);
 2525  
 
 2526  0
                 luDao.update(cluSet);
 2527  
 
 2528  0
                 StatusInfo statusInfo = new StatusInfo();
 2529  0
                 statusInfo.setSuccess(true);
 2530  
 
 2531  0
                 return statusInfo;
 2532  
         }
 2533  
 
 2534  
         @Override
 2535  
         @Transactional(readOnly=false)
 2536  
         public StatusInfo removeCluFromCluSet(String cluId, String cluSetId)
 2537  
                         throws DoesNotExistException, InvalidParameterException,
 2538  
                         MissingParameterException, OperationFailedException,
 2539  
                         PermissionDeniedException, UnsupportedActionException {
 2540  
 
 2541  0
                 checkForMissingParameter(cluId, "cluId");
 2542  0
                 checkForMissingParameter(cluSetId, "cluSetId");
 2543  
 
 2544  0
                 CluSet cluSet = luDao.fetch(CluSet.class, cluSetId);
 2545  
 
 2546  0
                 for (Iterator<CluSetJoinVersionIndClu> i = cluSet.getCluVerIndIds().iterator(); i.hasNext();) {
 2547  0
                         CluSetJoinVersionIndClu join = i.next();
 2548  0
                         if (join.getCluVersionIndId().equals(cluId)) {
 2549  0
                                 i.remove();
 2550  0
                                 luDao.delete(join);
 2551  0
                                 luDao.update(cluSet);
 2552  0
                                 StatusInfo statusInfo = new StatusInfo();
 2553  0
                                 statusInfo.setSuccess(true);
 2554  
 
 2555  0
                                 return statusInfo;
 2556  
                         }
 2557  0
                 }
 2558  
 
 2559  0
                 StatusInfo statusInfo = new StatusInfo();
 2560  0
                 statusInfo.setSuccess(false);
 2561  0
                 statusInfo.setMessage("Clu set does not contain Clu:" + cluId);
 2562  
 
 2563  0
                 return statusInfo;
 2564  
         }
 2565  
 
 2566  
         @Override
 2567  
         public List<ValidationResultInfo> validateLui(String validationType,
 2568  
                         LuiInfo luiInfo) throws DoesNotExistException,
 2569  
                         InvalidParameterException, MissingParameterException,
 2570  
                         OperationFailedException {
 2571  0
                 checkForMissingParameter(validationType, "validationType");
 2572  0
                 checkForMissingParameter(luiInfo, "luiInfo");
 2573  
 
 2574  0
         ObjectStructureDefinition objStructure = this.getObjectStructure(LuiInfo.class.getName());
 2575  0
         Validator defaultValidator = validatorFactory.getValidator();
 2576  0
         List<ValidationResultInfo> validationResults = defaultValidator.validateObject(luiInfo, objStructure);
 2577  0
         return validationResults;
 2578  
         }
 2579  
 
 2580  
         @Override
 2581  
         @Transactional(readOnly=false)
 2582  
         public LuiInfo createLui(String cluId, String atpKey, LuiInfo luiInfo)
 2583  
                         throws AlreadyExistsException, DataValidationErrorException,
 2584  
                         DoesNotExistException, InvalidParameterException,
 2585  
                         MissingParameterException, OperationFailedException,
 2586  
                         PermissionDeniedException {
 2587  0
                 checkForMissingParameter(cluId, "cludId");
 2588  0
                 checkForMissingParameter(atpKey, "atpKey");
 2589  0
                 checkForMissingParameter(luiInfo, "luiInfo");
 2590  
 
 2591  
                 // Validate Lui
 2592  0
                 List<ValidationResultInfo> val = validateLui("SYSTEM", luiInfo);
 2593  0
                 if(null != val && val.size() > 0) {
 2594  0
                         throw new DataValidationErrorException("Validation error!", val);
 2595  
                 }
 2596  
 
 2597  0
                 Lui lui = new Lui();
 2598  0
                 luiInfo.setCluId(cluId);
 2599  0
                 luiInfo.setAtpId(atpKey);
 2600  
 
 2601  
                 try {
 2602  0
                         lui = LuServiceAssembler.toLui(false, luiInfo, luDao);
 2603  0
                 } catch (VersionMismatchException vme) {
 2604  0
                 }
 2605  
 
 2606  0
                 luDao.create(lui);
 2607  
 
 2608  0
                 return LuServiceAssembler.toLuiInfo(lui);
 2609  
         }
 2610  
 
 2611  
         @Override
 2612  
         @Transactional(readOnly=false)
 2613  
         public LuiInfo updateLui(String luiId, LuiInfo luiInfo)
 2614  
                         throws DataValidationErrorException, DoesNotExistException,
 2615  
                         InvalidParameterException, MissingParameterException,
 2616  
                         OperationFailedException, PermissionDeniedException,
 2617  
                         VersionMismatchException {
 2618  
 
 2619  0
                 checkForMissingParameter(luiId, "luiId");
 2620  0
                 checkForMissingParameter(luiInfo, "luiInfo");
 2621  
 
 2622  
                 // Validate Lui
 2623  0
                 List<ValidationResultInfo> val = validateLui("SYSTEM", luiInfo);
 2624  0
                 if(null != val && val.size() > 0) {
 2625  0
                         throw new DataValidationErrorException("Validation error!", val);
 2626  
                 }
 2627  
 
 2628  0
                 Lui lui = luDao.fetch(Lui.class, luiId);
 2629  
 
 2630  0
                 if (!String.valueOf(lui.getVersionNumber()).equals(
 2631  
                                 luiInfo.getMetaInfo().getVersionInd())) {
 2632  0
                         throw new VersionMismatchException(
 2633  
                                         "Lui to be updated is not the current version");
 2634  
                 }
 2635  
 
 2636  0
                 Clu clu = luDao.fetch(Clu.class, luiInfo.getCluId());
 2637  0
                 lui.setClu(clu);
 2638  
 
 2639  0
                 lui.setAttributes(LuServiceAssembler.toGenericAttributes(
 2640  
                                 LuiAttribute.class, luiInfo.getAttributes(), lui, luDao));
 2641  
 
 2642  
                 // Now copy standard properties
 2643  0
                 BeanUtils.copyProperties(luiInfo, lui, new String[] { "cluId",
 2644  
                                 "attributes" });
 2645  
 
 2646  0
                 Lui updated = luDao.update(lui);
 2647  
 
 2648  0
                 return LuServiceAssembler.toLuiInfo(updated);
 2649  
         }
 2650  
 
 2651  
         @Override
 2652  
         @Transactional(readOnly=false)
 2653  
         public StatusInfo deleteLui(String luiId)
 2654  
                         throws DependentObjectsExistException, DoesNotExistException,
 2655  
                         InvalidParameterException, MissingParameterException,
 2656  
                         OperationFailedException, PermissionDeniedException {
 2657  
 
 2658  0
                 checkForMissingParameter(luiId, "luiId");
 2659  
 
 2660  0
                 luDao.delete(Lui.class, luiId);
 2661  
 
 2662  0
                 StatusInfo statusInfo = new StatusInfo();
 2663  0
                 statusInfo.setSuccess(true);
 2664  
 
 2665  0
                 return statusInfo;
 2666  
         }
 2667  
 
 2668  
         @Override
 2669  
         @Transactional(readOnly=false)
 2670  
         public LuiInfo updateLuiState(String luiId, String luiState)
 2671  
                         throws DataValidationErrorException, DoesNotExistException,
 2672  
                         InvalidParameterException, MissingParameterException,
 2673  
                         OperationFailedException, PermissionDeniedException {
 2674  
 
 2675  
                 // check for missing params
 2676  0
                 checkForMissingParameter(luiId, "luiId");
 2677  0
                 checkForMissingParameter(luiState, "luiState");
 2678  0
                 Lui lui = luDao.fetch(Lui.class, luiId);
 2679  0
                 lui.setState(luiState);
 2680  0
                 Lui updated = luDao.update(lui);
 2681  0
                 return LuServiceAssembler.toLuiInfo(updated);
 2682  
         }
 2683  
 
 2684  
         @Override
 2685  
         public List<ValidationResultInfo> validateLuiLuiRelation(
 2686  
                         String validationType, LuiLuiRelationInfo luiLuiRelationInfo)
 2687  
                         throws DoesNotExistException, InvalidParameterException,
 2688  
                         MissingParameterException, OperationFailedException {
 2689  0
                 checkForMissingParameter(validationType, "validationType");
 2690  0
                 checkForMissingParameter(luiLuiRelationInfo, "luiLuiRelationInfo");
 2691  
 
 2692  0
         ObjectStructureDefinition objStructure = this.getObjectStructure(LuiLuiRelation.class.getName());
 2693  0
         Validator defaultValidator = validatorFactory.getValidator();
 2694  0
         List<ValidationResultInfo> validationResults = defaultValidator.validateObject(luiLuiRelationInfo, objStructure);
 2695  0
         return validationResults;
 2696  
         }
 2697  
 
 2698  
         @Override
 2699  
         @Transactional(readOnly=false)
 2700  
         public LuiLuiRelationInfo createLuiLuiRelation(String luiId,
 2701  
                         String relatedLuiId, String luLuRelationTypeKey,
 2702  
                         LuiLuiRelationInfo luiLuiRelationInfo)
 2703  
                         throws AlreadyExistsException, CircularRelationshipException,
 2704  
                         DataValidationErrorException, DoesNotExistException,
 2705  
                         InvalidParameterException, MissingParameterException,
 2706  
                         OperationFailedException, PermissionDeniedException {
 2707  0
                 checkForMissingParameter(luiId, "luiId");
 2708  0
                 checkForMissingParameter(relatedLuiId, "relatedLuiId");
 2709  0
                 checkForMissingParameter(luLuRelationTypeKey, "luLuRelationTypeKey");
 2710  0
                 checkForMissingParameter(luiLuiRelationInfo, "luiLuiRelationInfo");
 2711  
 
 2712  
                 // Validate LuiLuiRelation
 2713  0
                 List<ValidationResultInfo> val = validateLuiLuiRelation("SYSTEM", luiLuiRelationInfo);
 2714  0
                 if(null != val && val.size() > 0) {
 2715  0
                         throw new DataValidationErrorException("Validation error!", val);
 2716  
                 }
 2717  
 
 2718  0
                 if (luiId.equals(relatedLuiId)) {
 2719  0
                         throw new CircularRelationshipException(
 2720  
                                         "Can not relate a Lui to itself");
 2721  
                 }
 2722  
 
 2723  0
                 Lui lui = luDao.fetch(Lui.class, luiId);
 2724  0
                 Lui relatedLui = luDao.fetch(Lui.class, relatedLuiId);
 2725  
 
 2726  0
                 LuiLuiRelation luiLuiRelation = new LuiLuiRelation();
 2727  0
                 BeanUtils.copyProperties(luiLuiRelationInfo, luiLuiRelation,
 2728  
                                 new String[] { "luiId", "relatedLuiId", "attributes",
 2729  
                                                 "metaInfo" });
 2730  
 
 2731  0
                 luiLuiRelation.setLui(lui);
 2732  0
                 luiLuiRelation.setRelatedLui(relatedLui);
 2733  0
                 luiLuiRelation.setAttributes(LuServiceAssembler.toGenericAttributes(
 2734  
                                 LuiLuiRelationAttribute.class, luiLuiRelationInfo
 2735  
                                                 .getAttributes(), luiLuiRelation, luDao));
 2736  
 
 2737  0
                 LuLuRelationType luLuRelationType = luDao.fetch(LuLuRelationType.class,
 2738  
                                 luLuRelationTypeKey);
 2739  
 
 2740  0
                 luiLuiRelation.setLuLuRelationType(luLuRelationType);
 2741  
 
 2742  0
                 luDao.create(luiLuiRelation);
 2743  
 
 2744  0
                 return LuServiceAssembler.toLuiLuiRelationInfo(luiLuiRelation);
 2745  
         }
 2746  
 
 2747  
         @Override
 2748  
         @Transactional(readOnly=false)
 2749  
         public LuiLuiRelationInfo updateLuiLuiRelation(String luiLuiRelationId,
 2750  
                         LuiLuiRelationInfo luiLuiRelationInfo)
 2751  
                         throws DataValidationErrorException, DoesNotExistException,
 2752  
                         InvalidParameterException, MissingParameterException,
 2753  
                         OperationFailedException, PermissionDeniedException,
 2754  
                         VersionMismatchException {
 2755  
 
 2756  0
                 checkForMissingParameter(luiLuiRelationId, "luiLuiRelationId");
 2757  0
                 checkForMissingParameter(luiLuiRelationInfo, "luiLuiRelationInfo");
 2758  
 
 2759  
                 // Validate LuiLuiRelation
 2760  0
                 List<ValidationResultInfo> val = validateLuiLuiRelation("SYSTEM", luiLuiRelationInfo);
 2761  0
                 if(null != val && val.size() > 0) {
 2762  0
                         throw new DataValidationErrorException("Validation error!", val);
 2763  
                 }
 2764  
 
 2765  0
                 LuiLuiRelation luiLuiRelation = luDao.fetch(LuiLuiRelation.class,
 2766  
                                 luiLuiRelationId);
 2767  
 
 2768  0
                 if (!String.valueOf(luiLuiRelation.getVersionNumber()).equals(
 2769  
                                 luiLuiRelationInfo.getMetaInfo().getVersionInd())) {
 2770  0
                         throw new VersionMismatchException(
 2771  
                                         "LuiLuiRelation to be updated is not the current version");
 2772  
                 }
 2773  
 
 2774  0
                 BeanUtils.copyProperties(luiLuiRelationInfo, luiLuiRelation,
 2775  
                                 new String[] { "luiId", "relatedLuiId", "attributes",
 2776  
                                                 "metaInfo" });
 2777  
 
 2778  0
                 if (!luiLuiRelationInfo.getLuiId().equals(
 2779  
                                 luiLuiRelation.getLui().getId())) {
 2780  0
                         luiLuiRelation.setLui(luDao.fetch(Lui.class, luiLuiRelationInfo
 2781  
                                         .getLuiId()));
 2782  
                 }
 2783  
 
 2784  0
                 if (!luiLuiRelationInfo.getRelatedLuiId().equals(
 2785  
                                 luiLuiRelation.getRelatedLui().getId())) {
 2786  0
                         luiLuiRelation.setRelatedLui(luDao.fetch(Lui.class,
 2787  
                                         luiLuiRelationInfo.getRelatedLuiId()));
 2788  
                 }
 2789  
 
 2790  0
                 luiLuiRelation.setAttributes(LuServiceAssembler.toGenericAttributes(
 2791  
                                 LuiLuiRelationAttribute.class, luiLuiRelationInfo
 2792  
                                                 .getAttributes(), luiLuiRelation, luDao));
 2793  
 
 2794  0
                 if (!luiLuiRelationInfo.getType().equals(
 2795  
                                 luiLuiRelation.getLuLuRelationType().getId())) {
 2796  0
                         luiLuiRelation.setLuLuRelationType(luDao.fetch(
 2797  
                                         LuLuRelationType.class, luiLuiRelationInfo.getType()));
 2798  
                 }
 2799  
 
 2800  0
                 LuiLuiRelation updated = luDao.update(luiLuiRelation);
 2801  
 
 2802  0
                 return LuServiceAssembler.toLuiLuiRelationInfo(updated);
 2803  
         }
 2804  
 
 2805  
         @Override
 2806  
         @Transactional(readOnly=false)
 2807  
         public StatusInfo deleteLuiLuiRelation(String luiLuiRelationId)
 2808  
                         throws DoesNotExistException, InvalidParameterException,
 2809  
                         MissingParameterException, OperationFailedException,
 2810  
                         PermissionDeniedException {
 2811  
 
 2812  0
                 checkForMissingParameter(luiLuiRelationId, "luiLuiRelationId");
 2813  
 
 2814  0
                 luDao.delete(LuiLuiRelation.class, luiLuiRelationId);
 2815  
 
 2816  0
                 StatusInfo statusInfo = new StatusInfo();
 2817  0
                 statusInfo.setSuccess(true);
 2818  
 
 2819  0
                 return statusInfo;
 2820  
         }
 2821  
 
 2822  
         /**************************************************************************
 2823  
          * SEARCH OPERATIONS *
 2824  
          **************************************************************************/
 2825  
 
 2826  
         @Override
 2827  
         public SearchCriteriaTypeInfo getSearchCriteriaType(
 2828  
                         String searchCriteriaTypeKey) throws DoesNotExistException,
 2829  
                         InvalidParameterException, MissingParameterException,
 2830  
                         OperationFailedException {
 2831  
 
 2832  0
                 return searchManager.getSearchCriteriaType(searchCriteriaTypeKey);
 2833  
         }
 2834  
 
 2835  
         @Override
 2836  
         public List<SearchCriteriaTypeInfo> getSearchCriteriaTypes()
 2837  
                         throws OperationFailedException {
 2838  0
                 return searchManager.getSearchCriteriaTypes();
 2839  
         }
 2840  
 
 2841  
         @Override
 2842  
         public SearchResultTypeInfo getSearchResultType(String searchResultTypeKey)
 2843  
                         throws DoesNotExistException, InvalidParameterException,
 2844  
                         MissingParameterException, OperationFailedException {
 2845  0
                 checkForMissingParameter(searchResultTypeKey, "searchResultTypeKey");
 2846  0
                 return searchManager.getSearchResultType(searchResultTypeKey);
 2847  
         }
 2848  
 
 2849  
         @Override
 2850  
         public List<SearchResultTypeInfo> getSearchResultTypes()
 2851  
                         throws OperationFailedException {
 2852  0
                 return searchManager.getSearchResultTypes();
 2853  
         }
 2854  
 
 2855  
         @Override
 2856  
         public SearchTypeInfo getSearchType(String searchTypeKey)
 2857  
                         throws DoesNotExistException, InvalidParameterException,
 2858  
                         MissingParameterException, OperationFailedException {
 2859  0
                 checkForMissingParameter(searchTypeKey, "searchTypeKey");
 2860  0
                 return searchManager.getSearchType(searchTypeKey);
 2861  
         }
 2862  
 
 2863  
         @Override
 2864  
         public List<SearchTypeInfo> getSearchTypes()
 2865  
                         throws OperationFailedException {
 2866  0
                 return searchManager.getSearchTypes();
 2867  
         }
 2868  
 
 2869  
         @Override
 2870  
         public List<SearchTypeInfo> getSearchTypesByCriteria(
 2871  
                         String searchCriteriaTypeKey) throws DoesNotExistException,
 2872  
                         InvalidParameterException, MissingParameterException,
 2873  
                         OperationFailedException {
 2874  0
                 checkForMissingParameter(searchCriteriaTypeKey, "searchCriteriaTypeKey");
 2875  0
                 return searchManager.getSearchTypesByCriteria(searchCriteriaTypeKey);
 2876  
         }
 2877  
 
 2878  
         @Override
 2879  
         public List<SearchTypeInfo> getSearchTypesByResult(
 2880  
                         String searchResultTypeKey) throws DoesNotExistException,
 2881  
                         InvalidParameterException, MissingParameterException,
 2882  
                         OperationFailedException {
 2883  0
                 checkForMissingParameter(searchResultTypeKey, "searchResultTypeKey");
 2884  0
                 return searchManager.getSearchTypesByResult(searchResultTypeKey);
 2885  
         }
 2886  
 
 2887  
         private void checkCluAlreadyAdded(CluSet cluSet, String cluId)
 2888  
                         throws OperationFailedException {
 2889  0
                 for (CluSetJoinVersionIndClu join : cluSet.getCluVerIndIds()) {
 2890  0
                         if (join.getCluVersionIndId().equals(cluId)) {
 2891  0
                                 throw new OperationFailedException("CluSet already contains Clu (id='" + cluId + "')");
 2892  
                         }
 2893  
                 }
 2894  0
         }
 2895  
 
 2896  
         private void checkCluSetAlreadyAdded(CluSet cluSet, String cluSetIdToAdd)
 2897  
                         throws OperationFailedException {
 2898  0
                 if(cluSet.getCluSets()!=null){
 2899  0
                         for (CluSet childCluSet : cluSet.getCluSets()) {
 2900  0
                                 if (childCluSet.getId().equals(cluSetIdToAdd)) {
 2901  0
                                         throw new OperationFailedException("CluSet (id=" + cluSet.getId() +
 2902  
                                                         ") already contains CluSet (id='" + cluSetIdToAdd + "')");
 2903  
                                 }
 2904  
                         }
 2905  
                 }
 2906  0
         }
 2907  
 
 2908  
         private void checkCluSetCircularReference(CluSet addedCluSet, String hostCluSetId)
 2909  
                         throws CircularRelationshipException {
 2910  0
                 if (addedCluSet.getId().equals(hostCluSetId)) {
 2911  0
                         throw new CircularRelationshipException(
 2912  
                                         "Cannot add a CluSet (id=" + hostCluSetId + ") to ifself");
 2913  
                 }
 2914  0
                 if(addedCluSet.getCluSets()!=null){
 2915  0
                         for (CluSet childSet : addedCluSet.getCluSets()) {
 2916  0
                                 if (childSet.getId().equals(hostCluSetId)) {
 2917  0
                                         throw new CircularRelationshipException(
 2918  
                                                         "CluSet (id=" + hostCluSetId +
 2919  
                                                         ") already contains this CluSet (id=" +
 2920  
                                                         childSet.getId() + ")");
 2921  
                                 }
 2922  0
                                 checkCluSetCircularReference(childSet, hostCluSetId);
 2923  
                         }
 2924  
                 }
 2925  0
         }
 2926  
 
 2927  
         private void findClusInCluSet(List<String> clus, CluSet parentCluSet)
 2928  
                         throws DoesNotExistException {
 2929  0
         List<String> processedCluSetIds = new ArrayList<String>();
 2930  0
         doFindClusInCluSet(processedCluSetIds, clus, parentCluSet);
 2931  0
         }
 2932  
         
 2933  
         private void doFindClusInCluSet(List<String> processedCluSetIds, 
 2934  
                 List<String> clus, CluSet parentCluSet) {
 2935  0
         for (CluSetJoinVersionIndClu join : parentCluSet.getCluVerIndIds()) {
 2936  0
             if (!clus.contains(join.getCluVersionIndId())) {
 2937  0
                 clus.add(join.getCluVersionIndId());
 2938  
             }
 2939  
         }
 2940  0
         if(parentCluSet.getCluSets()!=null){
 2941  0
             for (CluSet cluSet : parentCluSet.getCluSets()) {
 2942  
                 // This condition avoids infinite recursion problem
 2943  0
                 if (!processedCluSetIds.contains(cluSet.getId())) {
 2944  0
                     processedCluSetIds.add(cluSet.getId());
 2945  0
                     doFindClusInCluSet(processedCluSetIds, clus, cluSet);
 2946  
                 }
 2947  
             }
 2948  
         }
 2949  0
         }
 2950  
 
 2951  
         @Override
 2952  
         public ObjectStructureDefinition getObjectStructure(String objectTypeKey) {
 2953  0
                 return dictionaryServiceDelegate.getObjectStructure(objectTypeKey);
 2954  
         }
 2955  
 
 2956  
         @Override
 2957  
         public List<String> getObjectTypes() {
 2958  0
                 return dictionaryServiceDelegate.getObjectTypes();
 2959  
         }
 2960  
 
 2961  
         public LuDao getLuDao() {
 2962  0
                 return luDao;
 2963  
         }
 2964  
 
 2965  
         public void setLuDao(LuDao luDao) {
 2966  0
                 this.luDao = luDao;
 2967  0
         }
 2968  
 
 2969  
         @Override
 2970  
         public SearchResult search(SearchRequest searchRequest) throws MissingParameterException {
 2971  0
         checkForMissingParameter(searchRequest, "searchRequest");
 2972  
         
 2973  0
         if(SEARCH_KEY_DEPENDENCY_ANALYSIS.equals(searchRequest.getSearchKey())){
 2974  0
                 String cluId = null;
 2975  0
                     for(SearchParam param:searchRequest.getParams()){
 2976  0
                             if("lu.queryParam.luOptionalCluId".equals(param.getKey())){
 2977  0
                                     cluId = (String)param.getValue();
 2978  0
                                     break;
 2979  
                             }
 2980  
                     }
 2981  
                 try {
 2982  0
                                 return doDependencyAnalysisSearch(cluId);
 2983  0
                         } catch (DoesNotExistException e) {
 2984  0
                                 throw new RuntimeException("Error performing search");//FIXME should be more checked service exceptions thrown
 2985  
                         }
 2986  0
         }else if(SEARCH_KEY_BROWSE_PROGRAM.equals(searchRequest.getSearchKey())){
 2987  0
                 return doBrowseProgramSearch();
 2988  
         }
 2989  0
         return searchManager.search(searchRequest, luDao);
 2990  
         }
 2991  
 
 2992  
         private SearchResult doBrowseProgramSearch() throws MissingParameterException {
 2993  
                 //This is our main result
 2994  0
                 SearchResult programSearchResults = searchManager.search(new SearchRequest(SEARCH_KEY_BROWSE_PROGRAM), luDao);
 2995  
                 
 2996  
                 //These variations need to be mapped back to the program search results
 2997  0
                 SearchResult variationSearchResults = searchManager.search(new SearchRequest(SEARCH_KEY_BROWSE_VARIATIONS), luDao);
 2998  
                 
 2999  
                 //Get a mapping of program id to variation long name mapping:
 3000  0
                 Map<String,List<String>> variationMapping = new HashMap<String,List<String>>();
 3001  0
                 for(SearchResultRow row:variationSearchResults.getRows()){
 3002  0
                         String programId = null;
 3003  0
                         String variationLongName = null;
 3004  0
                         for(SearchResultCell cell:row.getCells()){
 3005  0
                                 if("lu.resultColumn.cluId".equals(cell.getKey())){
 3006  0
                                         programId = cell.getValue();
 3007  0
                                 }else if("lu.resultColumn.luOptionalLongName".equals(cell.getKey())){
 3008  0
                                         variationLongName = cell.getValue();
 3009  
                                 }
 3010  
                         }
 3011  0
                         List<String> variationLongNames = variationMapping.get(programId);
 3012  0
                         if(variationLongNames == null){
 3013  0
                                 variationLongNames = new ArrayList<String>();
 3014  0
                                 variationMapping.put(programId, variationLongNames);
 3015  
                         }
 3016  0
                         variationLongNames.add(variationLongName);
 3017  0
                 }
 3018  
                 
 3019  
                 
 3020  
                 //The result component types need to be mapped back as well
 3021  0
                 SearchRequest resultComponentSearchRequest = new SearchRequest(SEARCH_KEY_RESULT_COMPONENT);
 3022  0
                 resultComponentSearchRequest.addParam("lrc.queryParam.resultComponent.type", "kuali.resultComponentType.degree");
 3023  0
                 SearchResult resultComponentSearchResults = searchDispatcher.dispatchSearch(resultComponentSearchRequest);
 3024  
                 
 3025  
                 //Get a mapping of result type id to result type name:
 3026  0
                 Map<String,String> resultComponentMapping = new HashMap<String,String>();
 3027  0
                 for(SearchResultRow row:resultComponentSearchResults.getRows()){
 3028  0
                         String resultComponentTypeId = null;
 3029  0
                         String resultComponentTypeName = null;
 3030  0
                         for(SearchResultCell cell:row.getCells()){
 3031  0
                                 if("lrc.resultColumn.resultComponent.id".equals(cell.getKey())){
 3032  0
                                         resultComponentTypeId = cell.getValue();
 3033  0
                                 }else if("lrc.resultColumn.resultComponent.name".equals(cell.getKey())){
 3034  0
                                         resultComponentTypeName = cell.getValue();
 3035  
                                 }
 3036  
                         }
 3037  0
                         resultComponentMapping.put(resultComponentTypeId, resultComponentTypeName);
 3038  0
                 }
 3039  
                 
 3040  0
                 Map<String, Set<SearchResultCell>> orgIdToCellMapping = new HashMap<String, Set<SearchResultCell>>();
 3041  0
                 Map<String, Set<SearchResultCell>> resultComponentToCellMapping = new HashMap<String, Set<SearchResultCell>>(); 
 3042  0
                 Map<String, Set<SearchResultCell>> campusToCellMapping = new HashMap<String, Set<SearchResultCell>>();
 3043  0
                 Map<String, SearchResultCell> progIdToOrgCellMapping = new HashMap<String, SearchResultCell>(); 
 3044  0
                 Map<String, SearchResultCell> progIdToResultComponentCellMapping = new HashMap<String, SearchResultCell>(); 
 3045  0
                 Map<String, SearchResultCell> progIdToCampusCellMapping = new HashMap<String, SearchResultCell>();
 3046  
                 
 3047  
                 
 3048  
                 //We need to reduce the programSearchResults, translating variations, result options, etc and creating a mapping for org id translation
 3049  0
                 for(Iterator<SearchResultRow> rowIter = programSearchResults.getRows().iterator();rowIter.hasNext();){
 3050  0
                         SearchResultRow row = rowIter.next();
 3051  0
                         String programId = null;
 3052  0
                         String orgId = null;
 3053  0
                         String resultComponentName = null;
 3054  0
                         String campusCode = null;
 3055  0
                         SearchResultCell orgCell = null;
 3056  0
                         SearchResultCell resultComponentCell = null;
 3057  0
                         SearchResultCell variationCell = null;
 3058  0
                         SearchResultCell campusLocationCell = null;
 3059  
                         
 3060  0
                         for(SearchResultCell cell:row.getCells()){
 3061  0
                                 if("lu.resultColumn.cluId".equals(cell.getKey())){
 3062  0
                                         programId = cell.getValue();
 3063  0
                                 }else if("lu.resultColumn.luOptionalAdminOrg".equals(cell.getKey())){
 3064  0
                                         orgId = cell.getValue();
 3065  0
                                         orgCell = cell;
 3066  0
                                 }else if("lu.resultColumn.resultComponentId".equals(cell.getKey())){
 3067  0
                                         resultComponentName = resultComponentMapping.get(cell.getValue());
 3068  0
                                         resultComponentCell = cell;
 3069  0
                                 }else if("lu.resultColumn.variationId".equals(cell.getKey())){
 3070  0
                                         variationCell = cell;
 3071  0
                                 }else if("lu.resultColumn.luOptionalCampusLocation".equals(cell.getKey())){
 3072  0
                                         campusLocationCell = cell;
 3073  0
                                         campusCode = cell.getValue();
 3074  
                                 }
 3075  
                         }
 3076  0
                         if(!progIdToOrgCellMapping.containsKey(programId)){
 3077  
                                 //Add in the Variations
 3078  0
                                 List<String> variations = variationMapping.get(programId);
 3079  0
                                 variationCell.setValue("");
 3080  0
                                 if(variations!=null){
 3081  0
                                         for(Iterator<String> variationIter = variations.iterator();variationIter.hasNext();){
 3082  0
                                                 String variation = variationIter.next();
 3083  0
                                                 if(variationIter.hasNext()){
 3084  0
                                                         variation += "<br/>";
 3085  
                                                 }
 3086  0
                                                 variationCell.setValue(variationCell.getValue()+variation);
 3087  0
                                         }
 3088  
                                 }
 3089  
 
 3090  
                                 //Add the cell to the org id mapping
 3091  0
                                 Set<SearchResultCell> orgCells = orgIdToCellMapping.get(orgId);
 3092  0
                                 if(orgCells == null){
 3093  0
                                         orgCells = new HashSet<SearchResultCell>();
 3094  0
                                         orgIdToCellMapping.put(orgId, orgCells);
 3095  
                                 }
 3096  0
                                 orgCells.add(orgCell);
 3097  0
                                 orgCell.setValue(null);
 3098  
                                 
 3099  
                                                                 
 3100  
                                 //Add this to the map
 3101  0
                                 Set<SearchResultCell> campusCells = campusToCellMapping.get(campusCode);
 3102  0
                                 if(campusCells == null){
 3103  0
                                         campusCells = new HashSet<SearchResultCell>();
 3104  0
                                         campusToCellMapping.put(campusCode, campusCells);
 3105  
                                 }
 3106  0
                                 campusCells.add(campusLocationCell);
 3107  0
                                 campusLocationCell.setValue(null);
 3108  
                                 
 3109  
                                 //Add this to the map
 3110  0
                                 Set<SearchResultCell> resultCells = resultComponentToCellMapping.get(resultComponentName);
 3111  0
                                 if(resultCells == null){
 3112  0
                                         resultCells = new HashSet<SearchResultCell>();
 3113  0
                                         resultComponentToCellMapping.put(resultComponentName, resultCells);
 3114  
                                 }
 3115  0
                                 resultCells.add(resultComponentCell);
 3116  0
                                 resultComponentCell.setValue(null);
 3117  
                                 
 3118  0
                                 progIdToOrgCellMapping.put(programId, orgCell);
 3119  0
                                 progIdToResultComponentCellMapping.put(programId, resultComponentCell);
 3120  0
                                 progIdToCampusCellMapping.put(programId, campusLocationCell);
 3121  0
                         }else{
 3122  
                                 //this row already exists so we need to concatenate the result component and add the org id
 3123  
                                 //Get the result component row
 3124  0
                                 Set<SearchResultCell> resultCells = resultComponentToCellMapping.get(resultComponentName);
 3125  0
                                 if(resultCells == null){
 3126  0
                                         resultCells = new HashSet<SearchResultCell>();
 3127  0
                                         resultComponentToCellMapping.put(resultComponentName, resultCells);
 3128  
                                 }
 3129  0
                                 resultCells.add(progIdToResultComponentCellMapping.get(programId));
 3130  
                                 
 3131  
                                 //Add a new mapping to the org cell for this org id
 3132  0
                                 Set<SearchResultCell> orgCells = orgIdToCellMapping.get(orgId);
 3133  0
                                 if(orgCells == null){
 3134  0
                                         orgCells = new HashSet<SearchResultCell>();
 3135  0
                                         orgIdToCellMapping.put(orgId, orgCells);
 3136  
                                 }
 3137  0
                                 orgCells.add(progIdToOrgCellMapping.get(programId));
 3138  
                                 
 3139  
                                 //Concatenate the campus location
 3140  0
                                 Set<SearchResultCell> campusCells = campusToCellMapping.get(campusCode);
 3141  0
                                 if(campusCells == null){
 3142  0
                                         campusCells = new HashSet<SearchResultCell>();
 3143  0
                                         campusToCellMapping.put(campusCode, campusCells);
 3144  
                                 }
 3145  0
                                 campusCells.add(progIdToCampusCellMapping.get(programId));
 3146  
                                 
 3147  
                                 //Remove this row from results
 3148  0
                                 rowIter.remove();
 3149  
                         }
 3150  0
                 }
 3151  
                 
 3152  0
                 if(!resultComponentToCellMapping.isEmpty()){
 3153  0
                         List<String> resultComponentNames = new ArrayList<String>(resultComponentToCellMapping.keySet());
 3154  0
                         Collections.sort(resultComponentNames);
 3155  0
                         for(String resultComponentName:resultComponentNames){
 3156  
                                 //Concatenate resultComponent names in the holder cells
 3157  0
                                 Set<SearchResultCell> cells = resultComponentToCellMapping.get(resultComponentName);
 3158  0
                                 if(cells!=null){
 3159  0
                                         for(SearchResultCell cell:cells){
 3160  0
                                                 if(cell.getValue()==null){
 3161  0
                                                         cell.setValue(resultComponentName);
 3162  
                                                 }else{
 3163  0
                                                         cell.setValue(cell.getValue()+"<br/>"+resultComponentName);
 3164  
                                                 }
 3165  
                                         }
 3166  
                                 }
 3167  0
                         }
 3168  
                 }
 3169  
                 
 3170  0
                 if(!campusToCellMapping.isEmpty()){
 3171  0
                         List<String> campusCodes = new ArrayList<String>(campusToCellMapping.keySet());
 3172  0
                         Collections.sort(campusCodes);
 3173  0
                         for(String campusCode:campusCodes){
 3174  
                                 //Concatenate campus code names in the holder cells
 3175  0
                                 Set<SearchResultCell> cells = campusToCellMapping.get(campusCode);
 3176  0
                                 if(cells!=null){
 3177  0
                                         for(SearchResultCell cell:cells){
 3178  0
                                                 if(cell.getValue()==null){
 3179  0
                                                         cell.setValue(campusCode);
 3180  
                                                 }else{
 3181  0
                                                         cell.setValue(cell.getValue()+"<br/>"+campusCode);
 3182  
                                                 }
 3183  
                                         }
 3184  
                                 }
 3185  0
                         }
 3186  
                 }
 3187  
                 
 3188  
                 //Use the org search to Translate the orgIds into Org names and update the holder cells
 3189  0
                 if(!orgIdToCellMapping.isEmpty()){
 3190  
                         //Perform the Org search
 3191  0
                         SearchRequest orgIdTranslationSearchRequest = new SearchRequest("org.search.generic");
 3192  0
                         orgIdTranslationSearchRequest.addParam("org.queryParam.orgOptionalIds", new ArrayList<String>(orgIdToCellMapping.keySet()));
 3193  0
                         orgIdTranslationSearchRequest.setSortColumn("org.resultColumn.orgShortName");
 3194  0
                         SearchResult orgIdTranslationSearchResult = searchDispatcher.dispatchSearch(orgIdTranslationSearchRequest);
 3195  
                         
 3196  
                         //For each translation, update the result cell with the translated org name
 3197  0
                         for(SearchResultRow row:orgIdTranslationSearchResult.getRows()){
 3198  
                                 
 3199  
                                 //Get Params
 3200  0
                                 String orgId="";
 3201  0
                                 String orgName="";
 3202  0
                                 for(SearchResultCell cell:row.getCells()){
 3203  0
                                         if("org.resultColumn.orgId".equals(cell.getKey())){
 3204  0
                                                 orgId = cell.getValue();
 3205  0
                                                 continue;
 3206  0
                                         }else if("org.resultColumn.orgShortName".equals(cell.getKey())){
 3207  0
                                                 orgName = cell.getValue();
 3208  
                                         }
 3209  
                                 }
 3210  
                                 
 3211  
                                 //Concatenate org names in the holder cells
 3212  0
                                 Set<SearchResultCell> cells = orgIdToCellMapping.get(orgId);
 3213  0
                                 if(cells!=null){
 3214  0
                                         for(SearchResultCell cell:cells){
 3215  0
                                                 if(cell.getValue()==null){
 3216  0
                                                         cell.setValue(orgName);
 3217  
                                                 }else{
 3218  0
                                                         cell.setValue(cell.getValue()+"<br/>"+orgName);
 3219  
                                                 }
 3220  
                                         }
 3221  
                                 }
 3222  0
                         }
 3223  
                 }
 3224  
 
 3225  0
                 return programSearchResults;
 3226  
         }
 3227  
 
 3228  
         private SearchResult doDependencyAnalysisSearch(String cluId) throws MissingParameterException, DoesNotExistException {
 3229  
 
 3230  0
                 checkForMissingParameter(cluId, "cluId");
 3231  
 
 3232  0
                 Clu triggerClu = luDao.fetch(Clu.class, cluId);
 3233  
                 
 3234  0
                 List<String> cluVersionIndIds = new ArrayList<String>();
 3235  0
                 cluVersionIndIds.add(triggerClu.getVersion().getVersionIndId());
 3236  
                 
 3237  
                 //Find all clusets that contain this course
 3238  0
                 List<CluSet> cluSets = luDao.getCluSetsByCluVersionIndId(cluVersionIndIds);
 3239  
                 
 3240  
                 //Get a mapping of clusetId to cluset for easy referencing
 3241  0
                 Map<String, CluSet> cluSetMap = new HashMap<String, CluSet>();
 3242  0
                 if(cluSets!=null){
 3243  0
                         for(CluSet cluSet:cluSets){
 3244  0
                                 cluSetMap.put(cluSet.getId(), cluSet);
 3245  
                         }
 3246  
                 }
 3247  
                 
 3248  
                 //Execute all dynamic queries to see if the target clu is in the cluset and add those clusets
 3249  0
                 List<CluSet> dynamicCluSets = luDao.getAllDynamicCluSets();
 3250  0
                 if(dynamicCluSets!=null){
 3251  0
                         for(CluSet cluSet:dynamicCluSets){
 3252  0
                                 MembershipQueryInfo queryInfo = LuServiceAssembler.toMembershipQueryInfo(cluSet.getMembershipQuery());
 3253  0
                                 List<String> memberCluVersionIndIds = getMembershipQuerySearchResult(queryInfo);
 3254  0
                                 if(memberCluVersionIndIds!=null){
 3255  0
                                         for(String cluVersionIndId:cluVersionIndIds){
 3256  0
                                                 if(memberCluVersionIndIds.contains(cluVersionIndId)){
 3257  0
                                                         cluSetMap.put(cluSet.getId(),cluSet);
 3258  0
                                                         break;
 3259  
                                                 }
 3260  
                                         }
 3261  
                                 }
 3262  0
                         }
 3263  
                 }                
 3264  
                 //TODO Is it possible we need to search up the cluset hierarchies?
 3265  
                 //        If Cluset A contains clu 1 and cluset B contains cluset A, do we also return cluset B as a dependency?
 3266  
                 
 3267  
                 //Now we have the clu id and the list of clusets that the id appears in,
 3268  
                 //We need to do a statement service search to see what statements use these as 
 3269  
                 //dependencies
 3270  0
                 SearchRequest statementSearchRequest = new SearchRequest("stmt.search.dependencyAnalysis");
 3271  
                 
 3272  0
                 statementSearchRequest.addParam("stmt.queryParam.cluSetIds", new ArrayList<String>(cluSetMap.keySet()));
 3273  0
                 statementSearchRequest.addParam("stmt.queryParam.cluVersionIndIds", cluVersionIndIds);
 3274  
                 
 3275  0
                 SearchResult statementSearchResult = searchDispatcher.dispatchSearch(statementSearchRequest);
 3276  
                 
 3277  
                 //Create a search result for the return value
 3278  0
                 SearchResult searchResult = new SearchResult();
 3279  
                 
 3280  0
                 Map<String,List<SearchResultCell>> orgIdToCellMapping = new HashMap<String,List<SearchResultCell>>();
 3281  
                 
 3282  
                 //Now we need to take the statement ids and find the clus that relate to them
 3283  
                 //We will also transform the search result from the statement search result to 
 3284  
                 //the dependency analysis search result
 3285  0
                 Set<String> processed = new HashSet<String>();
 3286  0
                 for(SearchResultRow stmtRow:statementSearchResult.getRows()){
 3287  
 
 3288  
                         //Determine result column values
 3289  0
                         String refObjId = null;
 3290  0
                         String statementType = null;
 3291  0
                         String statementTypeName = null;
 3292  0
                         String rootId = null;
 3293  0
                         String requirementComponentIds = null;
 3294  
                         
 3295  0
                         for(SearchResultCell stmtCell:stmtRow.getCells()){
 3296  0
                                 if("stmt.resultColumn.refObjId".equals(stmtCell.getKey())){
 3297  0
                                         refObjId = stmtCell.getValue();
 3298  0
                                         continue;
 3299  0
                                 }else if("stmt.resultColumn.statementTypeId".equals(stmtCell.getKey())){
 3300  0
                                         statementType = stmtCell.getValue();
 3301  0
                                         continue;
 3302  0
                                 }else if("stmt.resultColumn.statementTypeName".equals(stmtCell.getKey())){
 3303  0
                                         statementTypeName = stmtCell.getValue();
 3304  0
                                         continue;
 3305  0
                                 }else if("stmt.resultColumn.rootId".equals(stmtCell.getKey())){
 3306  0
                                         rootId = stmtCell.getValue();
 3307  0
                                         continue;
 3308  0
                                 }else if("stmt.resultColumn.requirementComponentIds".equals(stmtCell.getKey())){
 3309  0
                                         requirementComponentIds = stmtCell.getValue();
 3310  
                                 }
 3311  
                         }
 3312  
                         
 3313  
                         //Find the clu
 3314  0
                         Clu clu = luDao.fetch(Clu.class, refObjId);
 3315  
 
 3316  
                         //Program statements are attached to dummy clus, so look up the parent program
 3317  0
                         if("kuali.lu.type.Requirement".equals(clu.getLuType().getId())){
 3318  
                                 
 3319  0
                                 List<Clu> clus = luDao.getClusByRelatedCluId(clu.getId(), "kuali.lu.lu.relation.type.hasProgramRequirement");
 3320  
                                 
 3321  0
                                 rootId = clu.getId();
 3322  
 
 3323  0
                                 if(clus==null||clus.size()==0){
 3324  0
                                         throw new RuntimeException("Statement Dependency clu found, but no parent Program exists");
 3325  0
                                 }else if(clus.size()>1){
 3326  0
                                         throw new RuntimeException("Statement Dependency clu can only have one parent Program relation");
 3327  
                                 }
 3328  0
                                 clu = clus.get(0);
 3329  
                         }
 3330  
 
 3331  
                         //Only process clus that are not active and that we have not already processed
 3332  0
                         String rowId = clu.getId()+"|"+statementType+"|"+rootId;
 3333  
                         
 3334  0
                         if("Active".equals(clu.getState()) && !processed.contains(rowId)){
 3335  
                                 
 3336  0
                                 processed.add(rowId);
 3337  
                                 
 3338  0
                                 SearchResultRow resultRow = new SearchResultRow();
 3339  
                                 
 3340  
                                 //Map the result cells
 3341  0
                                 resultRow.addCell("lu.resultColumn.cluId",clu.getId());
 3342  0
                                 resultRow.addCell("lu.resultColumn.cluType",clu.getLuType().getId());
 3343  0
                                 resultRow.addCell("lu.resultColumn.luOptionalCode",clu.getOfficialIdentifier().getCode());
 3344  0
                                 resultRow.addCell("lu.resultColumn.luOptionalShortName",clu.getOfficialIdentifier().getShortName());
 3345  0
                                 resultRow.addCell("lu.resultColumn.luOptionalLongName",clu.getOfficialIdentifier().getLongName());
 3346  0
                                 resultRow.addCell("lu.resultColumn.luOptionalDependencyType",statementType);
 3347  0
                                 resultRow.addCell("lu.resultColumn.luOptionalDependencyTypeName",statementTypeName);        
 3348  0
                                 resultRow.addCell("lu.resultColumn.luOptionalDependencyRootId",rootId);
 3349  0
                                 resultRow.addCell("lu.resultColumn.luOptionalDependencyRequirementComponentIds",requirementComponentIds);
 3350  
                                 
 3351  
                                 //Make a holder cell for the org names, to be populated later
 3352  0
                                 SearchResultCell orgIdsCell = new SearchResultCell("lu.resultColumn.luOptionalOversightCommitteeIds",null);
 3353  0
                                 resultRow.getCells().add(orgIdsCell);
 3354  
 
 3355  
                                 //Make a holder cell for the org ids, to be populated later
 3356  0
                                 SearchResultCell orgNamesCell = new SearchResultCell("lu.resultColumn.luOptionalOversightCommitteeNames",null);
 3357  0
                                 resultRow.getCells().add(orgNamesCell);
 3358  
                                 
 3359  
                                 //For each curriculum oversight committee we want to look up the Org Name
 3360  
                                 //We're going to save a mapping of the org id to a holder cell so we can make just one org 
 3361  
                                 //service call with all the org ids, and update the holder cells later.
 3362  0
                                 boolean differentAdminOrg = true;
 3363  0
                                 for(CluAdminOrg adminOrg:clu.getAdminOrgs()){
 3364  0
                                         if("kuali.adminOrg.type.CurriculumOversight".equals(adminOrg.getType()) || 
 3365  
                                            "kuali.adminOrg.type.CurriculumOversightUnit".equals(adminOrg.getType())){
 3366  
                                                 
 3367  
                                                 //Add the cell to the mapping for that perticular org id
 3368  0
                                                 List<SearchResultCell> cells = orgIdToCellMapping.get(adminOrg.getOrgId());
 3369  0
                                                 if(cells == null){
 3370  0
                                                         cells = new ArrayList<SearchResultCell>();
 3371  0
                                                         orgIdToCellMapping.put(adminOrg.getOrgId(), cells);
 3372  
                                                 }
 3373  0
                                                 cells.add(orgNamesCell);
 3374  
                                                 
 3375  
                                                 //Add the orgid to the orgIds cell so there is a comma delimited list of org ids
 3376  0
                                                 if(orgIdsCell.getValue()==null){
 3377  0
                                                         orgIdsCell.setValue(adminOrg.getId());
 3378  
                                                 }else{
 3379  0
                                                         orgIdsCell.setValue(orgIdsCell.getValue()+","+adminOrg.getId());
 3380  
                                                 }
 3381  
                                                 
 3382  0
                                                 for(CluAdminOrg triggerAdminOrg:triggerClu.getAdminOrgs()){
 3383  0
                                                         if(triggerAdminOrg.getOrgId().equals(adminOrg.getOrgId())){
 3384  0
                                                                 differentAdminOrg = false;
 3385  
                                                         }
 3386  
                                                 }
 3387  0
                                         }
 3388  
                                 }
 3389  0
                                 resultRow.addCell("lu.resultColumn.luOptionalDependencyRequirementDifferentAdminOrg", String.valueOf(differentAdminOrg));
 3390  
                                 
 3391  
                                 //Add the result row
 3392  0
                                 searchResult.getRows().add(resultRow);
 3393  
                         }
 3394  0
                 }
 3395  
                 
 3396  
                 //Use the org search to Translate the orgIds into Org names and update the holder cells
 3397  0
                 if(!orgIdToCellMapping.isEmpty()){
 3398  
                         //Perform the Org search
 3399  0
                         SearchRequest orgIdTranslationSearchRequest = new SearchRequest("org.search.generic");
 3400  0
                         orgIdTranslationSearchRequest.addParam("org.queryParam.orgOptionalIds", new ArrayList<String>(orgIdToCellMapping.keySet()));
 3401  0
                         SearchResult orgIdTranslationSearchResult = searchDispatcher.dispatchSearch(orgIdTranslationSearchRequest);
 3402  
                         
 3403  
                         //For each translation, update the result cell with the translated org name
 3404  0
                         for(SearchResultRow row:orgIdTranslationSearchResult.getRows()){
 3405  
                                 
 3406  
                                 //Get Params
 3407  0
                                 String orgId="";
 3408  0
                                 String orgName="";
 3409  0
                                 for(SearchResultCell cell:row.getCells()){
 3410  0
                                         if("org.resultColumn.orgId".equals(cell.getKey())){
 3411  0
                                                 orgId = cell.getValue();
 3412  0
                                                 continue;
 3413  0
                                         }else if("org.resultColumn.orgShortName".equals(cell.getKey())){
 3414  0
                                                 orgName = cell.getValue();
 3415  
                                         }
 3416  
                                 }
 3417  
                                 
 3418  
                                 //Concatenate org names in the holder cells
 3419  0
                                 List<SearchResultCell> cells = orgIdToCellMapping.get(orgId);
 3420  0
                                 if(cells!=null){
 3421  0
                                         for(SearchResultCell cell:cells){
 3422  0
                                                 if(cell.getValue()==null){
 3423  0
                                                         cell.setValue(orgName);
 3424  
                                                 }else{
 3425  0
                                                         cell.setValue(cell.getValue()+", "+orgName);
 3426  
                                                 }
 3427  
                                         }
 3428  
                                 }
 3429  0
                         }
 3430  
                 }
 3431  
                 
 3432  
                 //Add in CluSets and ignore ones named AdHoc
 3433  0
                 for(CluSet cluSet:cluSetMap.values()){
 3434  0
                         if(!"AdHock".equals(cluSet.getName())){
 3435  
 
 3436  0
                                 SearchResultRow resultRow = new SearchResultRow();
 3437  
                                 
 3438  0
                                 resultRow.addCell("lu.resultColumn.cluId",cluSet.getId());
 3439  0
                                 resultRow.addCell("lu.resultColumn.luOptionalShortName",cluSet.getName());
 3440  0
                                 resultRow.addCell("lu.resultColumn.luOptionalLongName",cluSet.getName());
 3441  0
                                 resultRow.addCell("lu.resultColumn.luOptionalDependencyType","cluSet");
 3442  0
                                 resultRow.addCell("lu.resultColumn.luOptionalDependencyTypeName", "Course Set");                        
 3443  
 
 3444  0
                                 searchResult.getRows().add(resultRow);
 3445  0
                         }
 3446  
                 }
 3447  
                 
 3448  
                 //Get any joints here and add them into the results
 3449  0
                 List<Clu> joints = luDao.getClusByRelation(cluId,"kuali.lu.relation.type.co-located");
 3450  0
                 if(joints!=null){
 3451  0
                         for(Clu clu:joints){
 3452  
                                 
 3453  0
                                 SearchResultRow resultRow = new SearchResultRow();
 3454  
                                 
 3455  0
                                 resultRow.addCell("lu.resultColumn.cluId", clu.getId());
 3456  0
                                 resultRow.addCell("lu.resultColumn.luOptionalCode", clu.getOfficialIdentifier().getCode());
 3457  0
                                 resultRow.addCell("lu.resultColumn.luOptionalShortName", clu.getOfficialIdentifier().getShortName());
 3458  0
                                 resultRow.addCell("lu.resultColumn.luOptionalLongName", clu.getOfficialIdentifier().getLongName());        
 3459  0
                                 resultRow.addCell("lu.resultColumn.luOptionalDependencyType", "joint");
 3460  0
                                 resultRow.addCell("lu.resultColumn.luOptionalDependencyTypeName", "jointly offered");
 3461  
                                 
 3462  0
                                 searchResult.getRows().add(resultRow);
 3463  0
                         }
 3464  
                 }
 3465  
                 
 3466  
                 //Lookup cross-listings and add to the results
 3467  0
                 for(CluIdentifier altId:triggerClu.getAlternateIdentifiers()){
 3468  0
                         if("kuali.lu.type.CreditCourse.identifier.crosslisting".equals(altId.getType())){
 3469  0
                                 SearchResultRow resultRow = new SearchResultRow();
 3470  
                                 
 3471  0
                                 resultRow.addCell("lu.resultColumn.luOptionalCode", altId.getCode());
 3472  0
                                 resultRow.addCell("lu.resultColumn.luOptionalShortName", altId.getShortName());
 3473  0
                                 resultRow.addCell("lu.resultColumn.luOptionalLongName", altId.getLongName());        
 3474  0
                                 resultRow.addCell("lu.resultColumn.luOptionalDependencyType", "crossListed");
 3475  0
                                 resultRow.addCell("lu.resultColumn.luOptionalDependencyTypeName", "cross-listed");                
 3476  
                                 
 3477  0
                                 searchResult.getRows().add(resultRow);
 3478  0
                         }
 3479  
                 }
 3480  
 
 3481  
                 //Sort results by Code
 3482  0
                 Collections.sort(searchResult.getRows(), new SearchResultRowComparator("lu.resultColumn.luOptionalCode"));
 3483  
                 
 3484  0
                 return searchResult;
 3485  
         }
 3486  
         
 3487  0
         public class SearchResultRowComparator implements Comparator<SearchResultRow>{
 3488  
                 private String sortColumn;
 3489  
                 
 3490  0
                 SearchResultRowComparator(String sortColumn){
 3491  0
                         super();
 3492  0
                         this.sortColumn = sortColumn;
 3493  0
                 }
 3494  
                 
 3495  
                 @Override
 3496  
                 public int compare(SearchResultRow o1, SearchResultRow o2) {
 3497  0
                         String o1SortValue = null;
 3498  0
                         String o2SortValue = null;
 3499  0
                         for(SearchResultCell cell:o1.getCells()){
 3500  0
                                 if(sortColumn.equals(cell.getKey())){
 3501  0
                                         o1SortValue = cell.getValue();
 3502  0
                                         break;
 3503  
                                 }
 3504  
                         }
 3505  0
                         for(SearchResultCell cell:o2.getCells()){
 3506  0
                                 if(sortColumn.equals(cell.getKey())){
 3507  0
                                         o2SortValue = cell.getValue();
 3508  0
                                         break;
 3509  
                                 }
 3510  
                         }
 3511  0
                         if(o1SortValue!=null){
 3512  0
                                 if(o2SortValue==null){
 3513  0
                                         return 1;
 3514  
                                 }
 3515  0
                                 return o1SortValue.compareTo(o2SortValue);
 3516  0
                         }if(o2SortValue==null){
 3517  0
                                 return 0;
 3518  
                         }
 3519  0
                         return -1;
 3520  
                 }
 3521  
                 
 3522  
         }
 3523  
 
 3524  
         /**
 3525  
          * Check for missing parameter and throw localized exception if missing
 3526  
          *
 3527  
          * @param param
 3528  
          * @param parameter
 3529  
          *            name
 3530  
          * @throws MissingParameterException
 3531  
          */
 3532  
         private void checkForMissingParameter(Object param, String paramName)
 3533  
                         throws MissingParameterException {
 3534  0
                 if (param == null) {
 3535  0
                         throw new MissingParameterException(paramName + " can not be null");
 3536  
                 }
 3537  0
         }
 3538  
 
 3539  
         /**
 3540  
          * @param param
 3541  
          * @param paramName
 3542  
          * @throws MissingParameterException
 3543  
          */
 3544  
         private void checkForEmptyList(Object param, String paramName)
 3545  
                         throws MissingParameterException {
 3546  0
                 if (param != null && param instanceof List<?>
 3547  
                                 && ((List<?>) param).size() == 0) {
 3548  0
                         throw new MissingParameterException(paramName
 3549  
                                         + " can not be an empty list");
 3550  
                 }
 3551  0
         }
 3552  
 
 3553  
         @Override
 3554  
         @Transactional(readOnly=false)
 3555  
         public StatusInfo addCluSetsToCluSet(String cluSetId, List<String> cluSetIdList)
 3556  
                 throws CircularRelationshipException,
 3557  
                         DoesNotExistException, InvalidParameterException,
 3558  
                         MissingParameterException, OperationFailedException,
 3559  
                         PermissionDeniedException, UnsupportedActionException {
 3560  
 
 3561  0
                 checkForMissingParameter(cluSetId, "cluSetId");
 3562  0
                 checkForMissingParameter(cluSetIdList, "cluSetIdList");
 3563  
 
 3564  
                 // Check that CluSet exists
 3565  0
                 luDao.fetch(CluSet.class, cluSetId);
 3566  
 
 3567  0
                 for(String cluSetIdToAdd : cluSetIdList) {
 3568  0
                         StatusInfo status = addCluSetToCluSet(cluSetId, cluSetIdToAdd);
 3569  0
                         if (!status.getSuccess()) {
 3570  0
                                 return status;
 3571  
                         }
 3572  0
                 }
 3573  
 
 3574  0
                 StatusInfo statusInfo = new StatusInfo();
 3575  0
                 statusInfo.setSuccess(true);
 3576  
 
 3577  0
                 return statusInfo;
 3578  
         }
 3579  
 
 3580  
         @Override
 3581  
         @Transactional(readOnly=false)
 3582  
         public StatusInfo addClusToCluSet(List<String> cluIdList, String cluSetId)
 3583  
                 throws DoesNotExistException, InvalidParameterException,
 3584  
                         MissingParameterException, OperationFailedException,
 3585  
                         PermissionDeniedException, UnsupportedActionException {
 3586  
 
 3587  0
                 checkForMissingParameter(cluIdList, "cluIdList");
 3588  0
                 checkForMissingParameter(cluSetId, "cluSetId");
 3589  
                 
 3590  0
                 for(String cluId : cluIdList) {
 3591  0
                         StatusInfo status = addCluToCluSet(cluId, cluSetId);
 3592  0
                         if (!status.getSuccess()) {
 3593  0
                                 return status;
 3594  
                         }
 3595  0
                 }
 3596  
 
 3597  0
                 StatusInfo statusInfo = new StatusInfo();
 3598  0
                 statusInfo.setSuccess(true);
 3599  
 
 3600  0
                 return statusInfo;
 3601  
         }
 3602  
 
 3603  
         public ValidatorFactory getValidatorFactory() {
 3604  0
                 return validatorFactory;
 3605  
         }
 3606  
 
 3607  
         public void setValidatorFactory(ValidatorFactory validatorFactory) {
 3608  0
                 this.validatorFactory = validatorFactory;
 3609  0
         }
 3610  
 
 3611  
         /********* Versioning Methods ***************************/
 3612  
         
 3613  
         @Override
 3614  
     @Transactional(readOnly=false)
 3615  
         public CluInfo createNewCluVersion(String versionIndCluId, String versionComment) throws DataValidationErrorException, DoesNotExistException, InvalidParameterException, MissingParameterException, OperationFailedException, PermissionDeniedException, VersionMismatchException {            
 3616  
                 Clu latestClu;
 3617  
                 Clu currentClu; 
 3618  
                 try{
 3619  0
                         latestClu = luDao.getLatestCluVersion(versionIndCluId);
 3620  0
                 }catch(NoResultException e){
 3621  0
                         throw new DoesNotExistException("There are no matching versions of this clu", e);
 3622  0
                 }
 3623  
                 try{
 3624  0
                         currentClu = luDao.getCurrentCluVersion(versionIndCluId);
 3625  0
                 }catch(NoResultException e){
 3626  0
                         throw new DoesNotExistException("There is no current version of this clu. Only current clus can be versioned. Use setCurrentCluVersion to make a clu current.", e);
 3627  0
                 }
 3628  
                 
 3629  0
             CluInfo cluInfo = LuServiceAssembler.toCluInfo(currentClu);
 3630  
             
 3631  
             // Reset the Clu
 3632  0
             clearCluIds(cluInfo);
 3633  
             
 3634  
             // Create the new Clu Version            
 3635  0
             CluInfo newClu = null;
 3636  
             
 3637  
         try {
 3638  0
                     Clu clu = toCluForCreate(cluInfo.getType(), cluInfo);
 3639  
                 //Set the Version data
 3640  0
                     Version version = new Version();
 3641  0
                     version.setSequenceNumber(latestClu.getVersion().getSequenceNumber() + 1);
 3642  0
                     version.setVersionIndId(versionIndCluId);
 3643  0
                     version.setCurrentVersionStart(null);
 3644  0
                     version.setCurrentVersionEnd(null);
 3645  0
                     version.setVersionComment(versionComment);
 3646  0
                     version.setVersionedFromId(currentClu.getId());
 3647  0
                     clu.setVersion(version);
 3648  0
                     luDao.create(clu);
 3649  0
             newClu = LuServiceAssembler.toCluInfo(clu); 
 3650  0
         } catch (AlreadyExistsException e) {
 3651  0
             throw new OperationFailedException("Error creating a new clu version", e);
 3652  0
         }
 3653  
             
 3654  0
             return newClu;
 3655  
         }
 3656  
 
 3657  
     private void clearCluIds(CluInfo clu) {
 3658  
             // Clear out all ids so a copy can be made
 3659  0
         clu.setState(DtoConstants.STATE_DRAFT);// TODO check if this should be set from outside
 3660  0
             clu.setId(null);
 3661  
                                     
 3662  0
             if(clu.getAccountingInfo()!=null){
 3663  0
                     clu.getAccountingInfo().setId(null);
 3664  
             
 3665  0
                     for(AffiliatedOrgInfo affiliatedOrg:clu.getAccountingInfo().getAffiliatedOrgs()){
 3666  0
                             affiliatedOrg.setId(null);
 3667  
                     }
 3668  
             }
 3669  0
             for(AccreditationInfo accredation:clu.getAccreditations()){
 3670  0
                     accredation.setId(null);
 3671  
             }
 3672  0
             for(AdminOrgInfo adminOrg:clu.getAdminOrgs()){
 3673  0
                     adminOrg.setId(null);
 3674  
             }
 3675  0
             for(CluIdentifierInfo alternateIdentifier:clu.getAlternateIdentifiers()){
 3676  0
                     alternateIdentifier.setId(null);
 3677  
             }
 3678  0
             if(clu.getFeeInfo()!=null){
 3679  0
                     clu.getFeeInfo().setId(null);
 3680  0
                     for(CluFeeRecordInfo cluFeeRecord:clu.getFeeInfo().getCluFeeRecords()){
 3681  0
                             cluFeeRecord.setId(null);
 3682  0
                             for(AffiliatedOrgInfo affiliatedOrg:cluFeeRecord.getAffiliatedOrgs()){
 3683  0
                                     affiliatedOrg.setId(null);
 3684  
                             }
 3685  0
                             for(CurrencyAmountInfo feeAmount:cluFeeRecord.getFeeAmounts()){
 3686  0
                                     feeAmount.setId(null);
 3687  
                             }
 3688  
                     }
 3689  
             }
 3690  0
             for(LuCodeInfo luCode:clu.getLuCodes()){
 3691  0
                     luCode.setId(null);
 3692  
             }
 3693  0
             if(clu.getOfficialIdentifier()!=null){
 3694  0
                     clu.getOfficialIdentifier().setId(null);
 3695  
             }
 3696  0
         }
 3697  
 
 3698  
         /**
 3699  
          * This method sets the CLU with ID of cluVersionId as the current version and will set the version end date of the previously current version to currentVersionStart or now() if null.  This will NOT update state of the current or previously current CLU.  All state changes must be handled either by the business service or from the client application. 
 3700  
          * 
 3701  
          * @param currentVersionStart if set to null, will default the current version start to the time when this method is called.
 3702  
          * You can set this to a future date as well. 
 3703  
          */
 3704  
         @Override
 3705  
         @Transactional(readOnly=false)
 3706  
     public StatusInfo setCurrentCluVersion(String cluVersionId, Date currentVersionStart) throws DoesNotExistException, InvalidParameterException, MissingParameterException, IllegalVersionSequencingException, OperationFailedException, PermissionDeniedException {
 3707  
         //Check params
 3708  0
                 Date currentDbDate = new Date();//FIXME, this should be DB time
 3709  0
                 if(currentVersionStart!=null&&currentVersionStart.compareTo(currentDbDate)<0){
 3710  0
                         throw new InvalidParameterException("currentVersionStart must be in the future.");
 3711  
                 }
 3712  
                 //Default the currentVersionStart to the current date
 3713  0
                 if(currentVersionStart==null){
 3714  0
                         currentVersionStart = currentDbDate;
 3715  
                 }
 3716  
                 
 3717  
                 //get the clu we are setting as current 
 3718  0
                 Clu clu = luDao.fetch(Clu.class, cluVersionId);
 3719  0
                 String versionIndId = clu.getVersion().getVersionIndId();
 3720  
 
 3721  0
                 Clu oldClu = null;
 3722  
                 try{
 3723  0
                         oldClu = luDao.getCurrentCluVersion(versionIndId);
 3724  0
                 }catch(NoResultException e){}
 3725  
                 
 3726  
                 //Check that the clu you are trying to version has a sequence number greater than the current clu
 3727  0
                 if(oldClu!=null){
 3728  0
                         if(clu.getVersion().getSequenceNumber()<=oldClu.getVersion().getSequenceNumber()){
 3729  0
                                 throw new OperationFailedException("Clu to make current must have been versioned from the current Clu");
 3730  
                         }
 3731  
                 }else{
 3732  
                         //Ignore the start date set if this is the first version (it will be set to the current time to avoid weird time problems)
 3733  0
                         currentVersionStart = currentDbDate;
 3734  
                 }
 3735  
                 
 3736  
                 
 3737  
                 //Get any clus that are set to become current in the future, and clear their current dates
 3738  0
                 List<VersionDisplayInfo> versionsInFuture = luDao.getVersionsInDateRange(versionIndId, null, currentDbDate, null);
 3739  0
                 for(VersionDisplayInfo versionInFuture:versionsInFuture){
 3740  0
                         if(oldClu==null || !versionInFuture.getId().equals(oldClu.getId())){
 3741  0
                                 VersionEntity futureClu = luDao.fetch(Clu.class, versionInFuture.getId());
 3742  0
                                 futureClu.getVersion().setCurrentVersionStart(null);
 3743  0
                                 futureClu.getVersion().setCurrentVersionEnd(null);
 3744  0
                                 futureClu = luDao.update(futureClu);
 3745  0
                         }
 3746  
                 }
 3747  
                 
 3748  
                 //If there is a current clu, set its end date to the new clu's start date
 3749  0
                 if(oldClu!=null){
 3750  0
                         oldClu.getVersion().setCurrentVersionEnd(currentVersionStart);
 3751  0
                         oldClu = luDao.update(oldClu);
 3752  
                 }
 3753  
                 
 3754  
                 //Set the startdate of the new current clu
 3755  0
                 clu.getVersion().setCurrentVersionStart(currentVersionStart);
 3756  0
                 clu.getVersion().setCurrentVersionEnd(null);
 3757  0
                 clu = luDao.update(clu);
 3758  
                 
 3759  0
                 StatusInfo statusInfo = new StatusInfo();
 3760  0
                 statusInfo.setSuccess(true);
 3761  0
         return statusInfo;
 3762  
     }   
 3763  
         
 3764  
     @Override
 3765  
     public VersionDisplayInfo getLatestVersion(String refObjectTypeURI, String refObjectId) throws DoesNotExistException, InvalidParameterException, MissingParameterException, OperationFailedException, PermissionDeniedException {
 3766  0
                 VersionDisplayInfo versionInfo = null;
 3767  0
                 if(LuServiceConstants.CLU_NAMESPACE_URI.equals(refObjectTypeURI)){
 3768  
                 try{
 3769  0
                         versionInfo = luDao.getLatestVersion(refObjectId, refObjectTypeURI);
 3770  0
                 }catch(NoResultException e){
 3771  0
                         throw new DoesNotExistException();
 3772  0
                 }
 3773  
         }else{
 3774  0
                 throw new UnsupportedOperationException("This method does not know how to handle object type:"+refObjectTypeURI);
 3775  
         }
 3776  0
                 return versionInfo;
 3777  
         }
 3778  
 
 3779  
         @Override
 3780  
     public VersionDisplayInfo getCurrentVersion(String refObjectTypeURI, String refObjectId) throws DoesNotExistException, InvalidParameterException, MissingParameterException, OperationFailedException, PermissionDeniedException {
 3781  0
                 VersionDisplayInfo versionInfo = null;
 3782  0
                 if(LuServiceConstants.CLU_NAMESPACE_URI.equals(refObjectTypeURI)){
 3783  
                 try{
 3784  0
                         versionInfo = luDao.getCurrentCluVersionInfo(refObjectId, refObjectTypeURI);
 3785  0
                 }catch(NoResultException e){
 3786  0
                         throw new DoesNotExistException();
 3787  0
                 }
 3788  
         }else{
 3789  0
                 throw new UnsupportedOperationException("This method does not know how to handle object type:"+refObjectTypeURI);
 3790  
         }
 3791  0
                 return versionInfo;
 3792  
         }
 3793  
 
 3794  
     @Override
 3795  
     public VersionDisplayInfo getCurrentVersionOnDate(String refObjectTypeURI, String refObjectId, Date date) throws DoesNotExistException, InvalidParameterException, MissingParameterException, OperationFailedException, PermissionDeniedException {
 3796  0
                 VersionDisplayInfo versionInfo = null;
 3797  0
                 if(LuServiceConstants.CLU_NAMESPACE_URI.equals(refObjectTypeURI)){
 3798  
                 try{
 3799  0
                         versionInfo = luDao.getCurrentVersionOnDate(refObjectId, refObjectTypeURI, date);
 3800  0
                 }catch(NoResultException e){
 3801  0
                         throw new DoesNotExistException();
 3802  0
                 }
 3803  
         }else{
 3804  0
                 throw new UnsupportedOperationException("This method does not know how to handle object type:"+refObjectTypeURI);
 3805  
         }
 3806  0
                 return versionInfo;
 3807  
     }
 3808  
 
 3809  
     @Override
 3810  
     public VersionDisplayInfo getFirstVersion(String refObjectTypeURI, String refObjectId) throws DoesNotExistException, InvalidParameterException, MissingParameterException, OperationFailedException, PermissionDeniedException {
 3811  0
                 VersionDisplayInfo versionInfo = null;
 3812  0
                 if(LuServiceConstants.CLU_NAMESPACE_URI.equals(refObjectTypeURI)){
 3813  
                 try{
 3814  0
                         versionInfo = luDao.getFirstVersion(refObjectId, refObjectTypeURI);
 3815  0
                 }catch(NoResultException e){
 3816  0
                         throw new DoesNotExistException();
 3817  0
                 }
 3818  
         }else{
 3819  0
                 throw new UnsupportedOperationException("This method does not know how to handle object type:"+refObjectTypeURI);
 3820  
         }
 3821  0
                 return versionInfo;
 3822  
     }
 3823  
 
 3824  
     @Override
 3825  
     public VersionDisplayInfo getVersionBySequenceNumber(String refObjectTypeURI, String refObjectId, Long sequence) throws DoesNotExistException, InvalidParameterException, MissingParameterException, OperationFailedException, PermissionDeniedException {
 3826  0
                 VersionDisplayInfo versionInfo = null;
 3827  0
                 if(LuServiceConstants.CLU_NAMESPACE_URI.equals(refObjectTypeURI)){
 3828  
                 try{
 3829  0
                         versionInfo = luDao.getVersionBySequenceNumber(refObjectId, refObjectTypeURI, sequence);
 3830  0
                 }catch(NoResultException e){
 3831  0
                         throw new DoesNotExistException();
 3832  0
                 }
 3833  
         }else{
 3834  0
                 throw new UnsupportedOperationException("This method does not know how to handle object type:"+refObjectTypeURI);
 3835  
         }
 3836  0
                 return versionInfo;
 3837  
     }
 3838  
 
 3839  
     @Override
 3840  
     public List<VersionDisplayInfo> getVersions(String refObjectTypeURI, String refObjectId) throws DoesNotExistException, InvalidParameterException, MissingParameterException, OperationFailedException, PermissionDeniedException {
 3841  0
             List<VersionDisplayInfo> versionInfos = null;
 3842  0
                 if(LuServiceConstants.CLU_NAMESPACE_URI.equals(refObjectTypeURI)){
 3843  0
                        versionInfos = luDao.getVersions(refObjectId, refObjectTypeURI);
 3844  0
                        if(versionInfos==null){
 3845  0
                                versionInfos = Collections.<VersionDisplayInfo>emptyList();
 3846  
                        }
 3847  
         }else{
 3848  0
                 throw new UnsupportedOperationException("This method does not know how to handle object type:"+refObjectTypeURI);
 3849  
         }
 3850  0
                 return versionInfos;
 3851  
     }
 3852  
 
 3853  
     @Override
 3854  
     public List<VersionDisplayInfo> getVersionsInDateRange(String refObjectTypeURI, String refObjectId, Date from, Date to) throws DoesNotExistException, InvalidParameterException, MissingParameterException, OperationFailedException, PermissionDeniedException {
 3855  0
             List<VersionDisplayInfo> versionInfos = null;
 3856  0
                 if(LuServiceConstants.CLU_NAMESPACE_URI.equals(refObjectTypeURI)){
 3857  0
                     versionInfos = luDao.getVersionsInDateRange(refObjectId, refObjectTypeURI, from, to);
 3858  0
                        if(versionInfos==null){
 3859  0
                                versionInfos = Collections.<VersionDisplayInfo>emptyList();
 3860  
                        }
 3861  
         }else{
 3862  0
                 throw new UnsupportedOperationException("This method does not know how to handle object type:"+refObjectTypeURI);
 3863  
         }
 3864  0
                 return versionInfos;
 3865  
     }
 3866  
 
 3867  
         public void setSearchDispatcher(SearchDispatcher searchDispatcher) {
 3868  0
                 this.searchDispatcher = searchDispatcher;
 3869  0
         }
 3870  
 }