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