001/**
002 * Copyright 2005-2017 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.data.DataObjectService;
020import org.kuali.rice.krad.data.PersistenceOption;
021import org.kuali.rice.krms.api.repository.typerelation.RelationshipType;
022import org.kuali.rice.krms.api.repository.typerelation.TypeTypeRelation;
023
024import java.util.Collection;
025import java.util.Collections;
026import java.util.HashMap;
027import java.util.LinkedList;
028import java.util.List;
029import java.util.Map;
030
031import static org.kuali.rice.krms.impl.repository.BusinessObjectServiceMigrationUtils.findMatching;
032
033/**
034 * Implementation of the @{link TypeTypeRelationBoService} interface for accessing  {@link TypeTypeRelationBo} related business objects.
035 * 
036 * @author Kuali Rice Team (rice.collab@kuali.org)
037 * 
038 */
039public class TypeTypeRelationBoServiceImpl implements TypeTypeRelationBoService {
040
041    private DataObjectService dataObjectService;
042    private KrmsAttributeDefinitionService attributeDefinitionService;
043
044    /**
045     * Sets the value of DataObjectService to the given value.
046     * 
047     * @param dataObjectService the DataObjectService value to set.
048     * 
049     */
050    public void setDataObjectService(DataObjectService dataObjectService) {
051        this.dataObjectService = dataObjectService;
052    }
053
054    public void setAttributeDefinitionService(KrmsAttributeDefinitionService attributeDefinitionService) {
055        this.attributeDefinitionService = attributeDefinitionService;
056    }
057
058    public KrmsAttributeDefinitionService getAttributeDefinitionService() {
059        if (attributeDefinitionService == null) {
060            attributeDefinitionService = KrmsRepositoryServiceLocator.getKrmsAttributeDefinitionService();
061        }
062        return attributeDefinitionService;
063    }
064
065    @Override
066    public TypeTypeRelation createTypeTypeRelation(TypeTypeRelation typeTypeRelation) {
067        incomingParamCheck(typeTypeRelation , "typeTypeRelation");
068        if (StringUtils.isNotEmpty(typeTypeRelation.getId())) {
069            final String typeTypeRelationIdKey = typeTypeRelation.getId();
070            final TypeTypeRelation existing = getTypeTypeRelation(typeTypeRelationIdKey);
071
072            if (existing != null) {
073                throw new IllegalStateException("the TypeTypeRelation to create already exists: " + typeTypeRelation);
074            }
075        }
076
077        TypeTypeRelationBo bo = dataObjectService.save(from(typeTypeRelation), PersistenceOption.FLUSH);
078
079        return TypeTypeRelationBo.to(bo);
080    }
081
082    @Override
083    public TypeTypeRelation getTypeTypeRelation(String typeTypeRelationId) {
084        incomingParamCheck(typeTypeRelationId , "typeTypeRelationId");
085        TypeTypeRelationBo bo = dataObjectService.find(TypeTypeRelationBo.class, typeTypeRelationId);
086
087        return TypeTypeRelationBo.to(bo);
088    }
089
090    @Override
091    public TypeTypeRelation updateTypeTypeRelation(TypeTypeRelation typeTypeRelation) {
092        incomingParamCheck(typeTypeRelation , "typeTypeRelation");
093        final TypeTypeRelation existing = getTypeTypeRelation(typeTypeRelation.getId());
094
095        if (existing == null) {
096            throw new IllegalStateException("the TypeTypeRelation to update does not exists: " + typeTypeRelation);
097        }
098
099        final TypeTypeRelation toUpdate;
100
101        if (!existing.getId().equals(typeTypeRelation.getId())){
102            // if passed in id does not match existing id, correct it
103            final TypeTypeRelation.Builder builder = TypeTypeRelation.Builder.create(typeTypeRelation);
104            builder.setId(existing.getId());
105            toUpdate = builder.build();
106        } else {
107            toUpdate = typeTypeRelation;
108        }
109
110        // copy all updateable fields to bo
111        TypeTypeRelationBo boToUpdate = from(toUpdate);
112
113        // update the rule and create new attributes
114        TypeTypeRelationBo updatedData = dataObjectService.save(boToUpdate, PersistenceOption.FLUSH);
115
116        return to(updatedData);
117    }
118
119    @Override
120    public void deleteTypeTypeRelation(String typeTypeRelationId) {
121        incomingParamCheck(typeTypeRelationId , "typeTypeRelationId");
122        final TypeTypeRelation existing = getTypeTypeRelation(typeTypeRelationId);
123
124        if (existing == null) {
125            throw new IllegalStateException("the TypeTypeRelation to delete does not exists: " + typeTypeRelationId);
126        }
127
128        dataObjectService.delete(from(existing));
129    }
130
131    @Override
132    public List<TypeTypeRelation> findTypeTypeRelationsByFromType(String fromTypeId) {
133        if (org.apache.commons.lang.StringUtils.isBlank(fromTypeId)) {
134            throw new IllegalArgumentException("fromTypeId is null or blank");
135        }
136
137        final Map<String, Object> map = new HashMap<String, Object>();
138        map.put("fromTypeId", fromTypeId);
139        List<TypeTypeRelationBo> bos = findMatching(dataObjectService, TypeTypeRelationBo.class, map);
140
141        return convertBosToImmutables(bos);
142    }
143
144    @Override
145    public List<TypeTypeRelation> findTypeTypeRelationsByToType(String toTypeId) {
146        if (org.apache.commons.lang.StringUtils.isBlank(toTypeId)) {
147            throw new IllegalArgumentException("toTypeId is null or blank");
148        }
149
150        final Map<String, Object> map = new HashMap<String, Object>();
151        map.put("toTypeId", toTypeId);
152        List<TypeTypeRelationBo> bos = findMatching(dataObjectService, TypeTypeRelationBo.class, map);
153
154        return convertBosToImmutables(bos);
155    }
156
157    @Override
158    public List<TypeTypeRelation> findTypeTypeRelationsByRelationshipType(RelationshipType relationshipType) {
159        if (relationshipType == null) {
160            throw new IllegalArgumentException("relationshipType is null");
161        }
162
163        final Map<String, Object> map = new HashMap<String, Object>();
164        map.put("relationshipType", relationshipType);
165        List<TypeTypeRelationBo> bos = findMatching(dataObjectService, TypeTypeRelationBo.class, map);
166
167        return convertBosToImmutables(bos);
168    }
169
170    @Override
171    public List<TypeTypeRelation> findTypeTypeRelationsBySequenceNumber(Integer sequenceNumber) {
172        if (sequenceNumber == null) {
173            throw new IllegalArgumentException("sequenceNumber is null");
174        }
175
176        final Map<String, Object> map = new HashMap<String, Object>();
177        map.put("sequenceNumber", sequenceNumber);
178        List<TypeTypeRelationBo> bos = findMatching(dataObjectService, TypeTypeRelationBo.class, map);
179
180        return convertBosToImmutables(bos);
181    }
182
183    public List<TypeTypeRelation> convertBosToImmutables(final Collection<TypeTypeRelationBo> typeTypeRelationBos) {
184        List<TypeTypeRelation> immutables = new LinkedList<TypeTypeRelation>();
185
186        if (typeTypeRelationBos != null) {
187            TypeTypeRelation immutable = null;
188            for (TypeTypeRelationBo bo : typeTypeRelationBos ) {
189                immutable = to(bo);
190                immutables.add(immutable);
191            }
192        }
193
194        return Collections.unmodifiableList(immutables);
195    }
196
197    @Override
198    public TypeTypeRelation to(TypeTypeRelationBo typeTypeRelationBo) {
199        return TypeTypeRelationBo.to(typeTypeRelationBo);
200    }
201
202    public TypeTypeRelationBo from(TypeTypeRelation typeTypeRelation) {
203        return TypeTypeRelationBo.from(typeTypeRelation);
204    }
205
206    private void incomingParamCheck(Object object, String name) {
207        if (object == null) {
208            throw new IllegalArgumentException(name + " was null");
209        } else if (object instanceof String
210                && StringUtils.isBlank((String)object)) {
211            throw new IllegalArgumentException(name + " was blank");
212        }
213    }
214
215}