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