001/** 002 * Copyright 2005-2016 The Kuali Foundation 003 * 004 * Licensed under the Educational Community License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.opensource.org/licenses/ecl2.php 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016package org.kuali.rice.krms.impl.repository; 017 018import org.apache.commons.lang.StringUtils; 019import org.kuali.rice.krad.service.BusinessObjectService; 020import org.kuali.rice.krms.api.repository.typerelation.RelationshipType; 021import org.kuali.rice.krms.api.repository.typerelation.TypeTypeRelation; 022import java.util.Collection; 023import java.util.Collections; 024import java.util.HashMap; 025import java.util.LinkedList; 026import java.util.List; 027import java.util.Map; 028 029/** 030 * Implementation of the @{link TypeTypeRelationBoService} interface for accessing {@link TypeTypeRelationBo} related business objects. 031 * 032 * @author Kuali Rice Team (rice.collab@kuali.org) 033 * 034 */ 035public final class TypeTypeRelationBoServiceImpl 036 implements TypeTypeRelationBoService 037{ 038 039 private BusinessObjectService businessObjectService; 040 private KrmsAttributeDefinitionService attributeDefinitionService; 041 042 /** 043 * Sets the value of BusinessObjectService to the given value. 044 * 045 * @param businessObjectService the BusinessObjectService value to set. 046 * 047 */ 048 public void setBusinessObjectService(BusinessObjectService businessObjectService) { 049 this.businessObjectService = businessObjectService; 050 } 051 052 public void setAttributeDefinitionService(KrmsAttributeDefinitionService attributeDefinitionService) { 053 this.attributeDefinitionService = attributeDefinitionService; 054 } 055 056 public KrmsAttributeDefinitionService getAttributeDefinitionService() { 057 if (attributeDefinitionService == null) { 058 attributeDefinitionService = KrmsRepositoryServiceLocator.getKrmsAttributeDefinitionService(); 059 } 060 return attributeDefinitionService; 061 } 062 063 @Override 064 public TypeTypeRelation createTypeTypeRelation(TypeTypeRelation typeTypeRelation) { 065 incomingParamCheck(typeTypeRelation , "typeTypeRelation"); 066 final String typeTypeRelationIdKey = typeTypeRelation.getId(); 067 final TypeTypeRelation existing = getTypeTypeRelation(typeTypeRelationIdKey); 068 if (existing != null){ throw new IllegalStateException("the TypeTypeRelation to create already exists: " + typeTypeRelation); } 069 TypeTypeRelationBo bo = (TypeTypeRelationBo)businessObjectService.save(from(typeTypeRelation)); 070 return TypeTypeRelationBo.to(bo); 071 } 072 073 @Override 074 public TypeTypeRelation getTypeTypeRelation(String typeTypeRelationId) { 075 incomingParamCheck(typeTypeRelationId , "typeTypeRelationId"); 076 TypeTypeRelationBo bo = businessObjectService.findBySinglePrimaryKey(TypeTypeRelationBo.class, typeTypeRelationId); 077 return TypeTypeRelationBo.to(bo); 078 } 079 080 @Override 081 public void updateTypeTypeRelation(TypeTypeRelation typeTypeRelation) { 082 incomingParamCheck(typeTypeRelation , "typeTypeRelation"); 083 final TypeTypeRelation existing = getTypeTypeRelation(typeTypeRelation.getId()); 084 if (existing == null){ throw new IllegalStateException("the TypeTypeRelation to update does not exists: " + typeTypeRelation);} 085 final TypeTypeRelation toUpdate; 086 if (!existing.getId().equals(typeTypeRelation.getId())){ 087 // if passed in id does not match existing id, correct it 088 final TypeTypeRelation.Builder builder = TypeTypeRelation.Builder.create(typeTypeRelation); 089 builder.setId(existing.getId()); 090 toUpdate = builder.build(); 091 } else { 092 toUpdate = typeTypeRelation; 093 } 094 095 // copy all updateable fields to bo 096 TypeTypeRelationBo boToUpdate = from(toUpdate); 097 098 // update the rule and create new attributes 099 businessObjectService.save(boToUpdate); 100 } 101 102 @Override 103 public void deleteTypeTypeRelation(String typeTypeRelationId) { 104 incomingParamCheck(typeTypeRelationId , "typeTypeRelationId"); 105 final TypeTypeRelation existing = getTypeTypeRelation(typeTypeRelationId); 106 if (existing == null){ throw new IllegalStateException("the TypeTypeRelation to delete does not exists: " + typeTypeRelationId);} 107 businessObjectService.delete(from(existing)); 108 } 109 110 @Override 111 public List<TypeTypeRelation> findTypeTypeRelationsByFromType(String fromTypeId) { 112 if (org.apache.commons.lang.StringUtils.isBlank(fromTypeId)) { 113 throw new IllegalArgumentException("fromTypeId is null or blank"); 114 } 115 final Map<String, Object> map = new HashMap<String, Object>(); 116 map.put("fromTypeId", fromTypeId); 117 List<TypeTypeRelationBo> bos = (List<TypeTypeRelationBo>) businessObjectService.findMatching(TypeTypeRelationBo.class, map); 118 return convertBosToImmutables(bos); 119 } 120 121 @Override 122 public List<TypeTypeRelation> findTypeTypeRelationsByToType(String toTypeId) { 123 if (org.apache.commons.lang.StringUtils.isBlank(toTypeId)) { 124 throw new IllegalArgumentException("toTypeId is null or blank"); 125 } 126 final Map<String, Object> map = new HashMap<String, Object>(); 127 map.put("toTypeId", toTypeId); 128 List<TypeTypeRelationBo> bos = (List<TypeTypeRelationBo>) businessObjectService.findMatching(TypeTypeRelationBo.class, map); 129 return convertBosToImmutables(bos); 130 } 131 132 @Override 133 public List<TypeTypeRelation> findTypeTypeRelationsByRelationshipType(RelationshipType relationshipType) { 134 if (relationshipType == null) { 135 throw new IllegalArgumentException("relationshipType is null"); 136 } 137 final Map<String, Object> map = new HashMap<String, Object>(); 138 map.put("relationshipType", relationshipType); 139 List<TypeTypeRelationBo> bos = (List<TypeTypeRelationBo>) businessObjectService.findMatching(TypeTypeRelationBo.class, map); 140 return convertBosToImmutables(bos); 141 } 142 143 @Override 144 public List<TypeTypeRelation> findTypeTypeRelationsBySequenceNumber(Integer sequenceNumber) { 145 if (sequenceNumber == null) { 146 throw new IllegalArgumentException("sequenceNumber is null"); 147 } 148 final Map<String, Object> map = new HashMap<String, Object>(); 149 map.put("sequenceNumber", sequenceNumber); 150 List<TypeTypeRelationBo> bos = (List<TypeTypeRelationBo>) businessObjectService.findMatching(TypeTypeRelationBo.class, map); 151 return convertBosToImmutables(bos); 152 } 153 154 public List<TypeTypeRelation> convertBosToImmutables(final Collection<TypeTypeRelationBo> typeTypeRelationBos) { 155 List<TypeTypeRelation> immutables = new LinkedList<TypeTypeRelation>(); 156 if (typeTypeRelationBos != null) { 157 TypeTypeRelation immutable = null; 158 for (TypeTypeRelationBo bo : typeTypeRelationBos ) { 159 immutable = to(bo); 160 immutables.add(immutable); 161 } 162 } 163 return Collections.unmodifiableList(immutables); 164 } 165 166 @Override 167 public TypeTypeRelation to(TypeTypeRelationBo typeTypeRelationBo) { 168 return TypeTypeRelationBo.to(typeTypeRelationBo); 169 } 170 171 public TypeTypeRelationBo from(TypeTypeRelation typeTypeRelation) { 172 return TypeTypeRelationBo.from(typeTypeRelation); 173 } 174 175 private void incomingParamCheck(Object object, String name) { 176 if (object == null) { 177 throw new IllegalArgumentException(name + " was null"); 178 } else if (object instanceof String 179 && StringUtils.isBlank((String)object)) { 180 throw new IllegalArgumentException(name + " was blank"); 181 } 182 } 183 184}