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.kim.impl.type;
017
018import java.util.ArrayList;
019import java.util.Collection;
020import java.util.Collections;
021import java.util.HashMap;
022import java.util.Map;
023
024import org.apache.commons.lang.StringUtils;
025import org.kuali.rice.core.api.criteria.QueryByCriteria;
026import org.kuali.rice.core.api.criteria.QueryResults;
027import org.kuali.rice.core.api.exception.RiceIllegalArgumentException;
028import org.kuali.rice.kim.api.type.KimType;
029import org.kuali.rice.kim.api.type.KimTypeInfoService;
030import org.kuali.rice.krad.data.DataObjectService;
031
032public class KimTypeInfoServiceImpl implements KimTypeInfoService {
033
034    protected DataObjectService dataObjectService;
035    
036    @Override
037    public KimType getKimType(final String id) throws RiceIllegalArgumentException {
038        incomingParamCheck(id, "id");
039
040        return KimTypeBo.to(dataObjectService.find(KimTypeBo.class, id));
041    }
042
043    @Override
044    public KimType findKimTypeByNameAndNamespace(final String namespaceCode, final String name) throws RiceIllegalArgumentException {
045        incomingParamCheck(namespaceCode, "namespaceCode");
046        incomingParamCheck(name, "name");
047
048        final Map<String, Object> crit = new HashMap<String, Object>(3);
049        crit.put("namespaceCode", namespaceCode);
050        crit.put("name", name);
051        crit.put("active", Boolean.TRUE);
052
053        QueryResults<KimTypeBo> bos = dataObjectService.findMatching(KimTypeBo.class, QueryByCriteria.Builder.andAttributes(crit).build());
054
055        if (bos.getResults().size() > 1) {
056            throw new IllegalStateException("multiple active results were found for the namespace code: " + namespaceCode + " and name: " + name);
057        }
058
059        return bos.getResults().size() > 0 ? KimTypeBo.to(bos.getResults().get(0)) : null;
060    }
061
062    @Override
063    public Collection<KimType> findAllKimTypes() {
064        QueryResults<KimTypeBo> bos
065                = dataObjectService.findMatching(KimTypeBo.class, QueryByCriteria.Builder.forAttribute("active", Boolean.TRUE).build());
066        Collection<KimType> ims = new ArrayList<KimType>(bos.getResults().size());
067
068        for (KimTypeBo bo : bos.getResults()) {
069            if (bo != null) {
070                ims.add(KimTypeBo.to(bo));
071            }
072        }
073        return Collections.unmodifiableCollection(ims);
074    }
075
076    public void setDataObjectService(DataObjectService dataObjectService) {
077        this.dataObjectService = dataObjectService;
078    }
079
080    private void incomingParamCheck(Object object, String name) {
081        if (object == null) {
082            throw new RiceIllegalArgumentException(name + " was null");
083        } else if (object instanceof String
084                && StringUtils.isBlank((String) object)) {
085            throw new RiceIllegalArgumentException(name + " was blank");
086        }
087    }
088}