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.kew.impl.repository;
017
018
019import java.util.ArrayList;
020import java.util.Collection;
021import java.util.Collections;
022import java.util.HashMap;
023import java.util.List;
024import java.util.Map;
025
026import org.apache.commons.lang.StringUtils;
027import org.kuali.rice.core.api.exception.RiceIllegalArgumentException;
028import org.kuali.rice.core.api.exception.RiceIllegalStateException;
029import org.kuali.rice.kew.api.repository.type.KewTypeAttribute;
030import org.kuali.rice.kew.api.repository.type.KewTypeDefinition;
031import org.kuali.rice.kew.api.repository.type.KewTypeRepositoryService;
032import org.kuali.rice.kew.impl.type.KewTypeAttributeBo;
033import org.kuali.rice.kew.impl.type.KewTypeBo;
034import org.kuali.rice.krad.service.BusinessObjectService;
035
036public final class KewTypeBoServiceImpl implements KewTypeRepositoryService {
037
038    private BusinessObjectService businessObjectService;
039
040        /**
041         * This overridden method creates a KewType if it does not already exist in the repository.
042         * 
043         * @see org.kuali.rice.kew.api.repository.type.KewTypeRepositoryService#createKewType(org.kuali.rice.kew.api.repository.type.KewTypeDefinition)
044         */
045        @Override
046        public KewTypeDefinition createKewType(KewTypeDefinition kewType) {
047                if (kewType == null){
048                throw new RiceIllegalArgumentException("kewType is null");
049                }
050                final String nameKey = kewType.getName();
051                final String namespaceKey = kewType.getNamespace();
052                final KewTypeDefinition existing = getTypeByNameAndNamespace(nameKey, namespaceKey);
053                if (existing != null && existing.getName().equals(nameKey) && existing.getNamespace().equals(namespaceKey)){
054            throw new RiceIllegalStateException("The KEW Type to create already exists: " + kewType);
055                }
056                
057                KewTypeBo bo = (KewTypeBo)businessObjectService.save(KewTypeBo.from(kewType));
058                
059                return KewTypeBo.to(bo);
060        }
061
062        /**
063         * This overridden method updates an existing KewType
064         * 
065         * @see org.kuali.rice.kew.api.repository.type.KewTypeRepositoryService#updateKewType(org.kuali.rice.kew.api.repository.type.KewTypeDefinition)
066         */
067        @Override
068        public void updateKewType(KewTypeDefinition kewType) {
069        if (kewType == null) {
070            throw new RiceIllegalArgumentException("kewType is null");
071        }
072                final String idKey = kewType.getId();
073                final KewTypeBo existing = businessObjectService.findBySinglePrimaryKey(KewTypeBo.class, idKey);
074        if (existing == null) {
075            throw new RiceIllegalStateException("The KEW type does not exist: " + kewType);
076        }
077        final KewTypeDefinition toUpdate;
078        if (!existing.getId().equals(kewType.getId())){
079                final KewTypeDefinition.Builder builder = KewTypeDefinition.Builder.create(kewType);
080                builder.setId(existing.getId());
081                toUpdate = builder.build();
082        } else {
083                toUpdate = kewType;
084        }
085        
086        businessObjectService.save(KewTypeBo.from(toUpdate));
087        }
088
089    @Override
090    public KewTypeDefinition getTypeById(final String id) {
091        if (StringUtils.isBlank(id)) {
092            throw new RiceIllegalArgumentException("id is blank");
093        }
094
095        KewTypeBo kewTypeBo = businessObjectService.findBySinglePrimaryKey(KewTypeBo.class, id);
096
097        return KewTypeBo.to(kewTypeBo);
098    }
099
100    @Override
101    public KewTypeDefinition getTypeByNameAndNamespace(final String name, final String namespace) {
102        if (StringUtils.isBlank(name)) {
103            throw new RiceIllegalArgumentException("name is blank");
104        }
105        if (StringUtils.isBlank(namespace)) {
106            throw new RiceIllegalArgumentException("namespace is blank");
107        }
108
109        final Map<String, Object> map = new HashMap<String, Object>();
110        map.put("name", name);
111        map.put("namespace", namespace);
112
113        KewTypeBo myType = businessObjectService.findByPrimaryKey(KewTypeBo.class, Collections.unmodifiableMap(map));
114        return KewTypeBo.to(myType);
115    }
116
117    @Override
118    public List<KewTypeDefinition> findAllTypesByNamespace(final String namespace) {
119        if (StringUtils.isBlank(namespace)) {
120            throw new RiceIllegalArgumentException("namespace is blank");
121        }
122        final Map<String, Object> map = new HashMap<String, Object>();
123        map.put("namespace", namespace);
124        map.put("active", Boolean.TRUE);
125
126        Collection<KewTypeBo> kewTypeBos = businessObjectService.findMatching(KewTypeBo.class, Collections.unmodifiableMap(map));
127
128        return convertListOfBosToImmutables(kewTypeBos);
129    }
130
131    @Override
132    public List<KewTypeDefinition> findAllTypes() {
133        final Map<String, Object> map = new HashMap<String, Object>();
134        map.put("active", Boolean.TRUE);
135
136        Collection<KewTypeBo> kewTypeBos = businessObjectService.findMatching(KewTypeBo.class, Collections.unmodifiableMap(map));
137        return convertListOfBosToImmutables(kewTypeBos);
138    }
139
140    /**
141     * Sets the businessObjectService attribute value.
142     *
143     * @param businessObjectService The businessObjectService to set.
144     */
145    public void setBusinessObjectService(final BusinessObjectService businessObjectService) {
146        this.businessObjectService = businessObjectService;
147    }
148
149    /**
150     * Converts a List<KewTypeBo> to an Unmodifiable List<KewType>
151     *
152     * @param kewTypeBos a mutable List<KewTypeBo> to made completely immutable.
153     * @return An unmodifiable List<KewType>
154     */
155    List<KewTypeDefinition> convertListOfBosToImmutables(final Collection<KewTypeBo> kewTypeBos) {
156        ArrayList<KewTypeDefinition> kewTypes = new ArrayList<KewTypeDefinition>();
157        for (KewTypeBo bo : kewTypeBos) {
158            KewTypeDefinition kewType = KewTypeBo.to(bo);
159            kewTypes.add(kewType);
160        }
161        return Collections.unmodifiableList(kewTypes);
162    }
163
164        /**
165     * This overridden method creates a KewTypeAttribute if it does not already exist in the repository.
166         * 
167         * @see org.kuali.rice.kew.api.repository.type.KewTypeRepositoryService#createKewTypeAttribute(org.kuali.rice.kew.api.repository.type.KewTypeAttribute)
168         */
169        @Override
170        public void createKewTypeAttribute(KewTypeAttribute kewTypeAttribute) {
171        if (kewTypeAttribute == null){
172            throw new RiceIllegalArgumentException("kewTypeAttribute is null");
173        }
174        
175        final String typeIdKey = kewTypeAttribute.getTypeId();
176        final String attrDefIdKey = kewTypeAttribute.getAttributeDefinitionId();
177        
178        final Map<String, Object> map = new HashMap<String, Object>();
179        map.put("typeId", typeIdKey);
180        map.put("attributeDefinitionId", attrDefIdKey);
181
182        KewTypeAttributeBo existing = businessObjectService.findByPrimaryKey(KewTypeAttributeBo.class, Collections.unmodifiableMap(map));
183
184        if (existing != null && existing.getTypeId().equals(typeIdKey) && existing.getAttributeDefinitionId().equals(attrDefIdKey)){
185            throw new RiceIllegalStateException("The KEW Type Attribute to create already exists: " + kewTypeAttribute);
186        }
187        
188        KewTypeAttributeBo bo = (KewTypeAttributeBo)businessObjectService.save(KewTypeAttributeBo.from(kewTypeAttribute));
189    }
190
191    /**
192     * This overridden method updates an existing KewTypeAttribute
193     * 
194     * @see org.kuali.rice.kew.api.repository.type.KewTypeRepositoryService#updateKewTypeAttribute(org.kuali.rice.kew.api.repository.type.KewTypeAttribute)
195     */
196    @Override
197    public void updateKewTypeAttribute(KewTypeAttribute kewTypeAttribute) {
198        if (kewTypeAttribute == null) {
199            throw new RiceIllegalArgumentException("kewTypeAttribute is null");
200        }
201        final String idKey = kewTypeAttribute.getId();
202        final KewTypeAttributeBo existing = businessObjectService.findBySinglePrimaryKey(KewTypeAttributeBo.class, idKey);
203        if (existing == null) {
204            throw new RiceIllegalStateException("The KEW type Attribute does not exist: " + kewTypeAttribute);
205        }
206        final KewTypeAttribute toUpdate;
207        if (!existing.getId().equals(kewTypeAttribute.getId())){
208            final KewTypeAttribute.Builder builder = KewTypeAttribute.Builder.create(kewTypeAttribute);
209            builder.setId(existing.getId());
210            toUpdate = builder.build();
211        } else {
212            toUpdate = kewTypeAttribute;
213        }
214        
215        businessObjectService.save(KewTypeAttributeBo.from(toUpdate));
216    }
217}