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 org.apache.commons.lang.StringUtils;
019import org.kuali.rice.core.api.exception.RiceIllegalArgumentException;
020import org.kuali.rice.kim.api.type.KimType;
021import org.kuali.rice.kim.api.type.KimTypeInfoService;
022import org.kuali.rice.krad.service.BusinessObjectService;
023
024import java.util.ArrayList;
025import java.util.Collection;
026import java.util.Collections;
027import java.util.HashMap;
028import java.util.Map;
029
030public class KimTypeInfoServiceImpl implements KimTypeInfoService {
031
032    private BusinessObjectService businessObjectService;
033
034    @Override
035    public KimType getKimType(final String id) throws RiceIllegalArgumentException {
036        incomingParamCheck(id, "id");
037
038        return KimTypeBo.to(businessObjectService.findBySinglePrimaryKey(KimTypeBo.class, id));
039    }
040
041    @Override
042    public KimType findKimTypeByNameAndNamespace(final String namespaceCode, final String name) throws RiceIllegalArgumentException {
043        incomingParamCheck(namespaceCode, "namespaceCode");
044        incomingParamCheck(name, "name");
045
046        final Map<String, Object> crit = new HashMap<String, Object>();
047        crit.put("namespaceCode", namespaceCode);
048        crit.put("name", name);
049        crit.put("active", "true");
050
051        final Collection<KimTypeBo> bos = businessObjectService.findMatching(KimTypeBo.class, crit);
052
053        if (bos != null && bos.size() > 1) {
054            throw new IllegalStateException("multiple active results were found for the namespace code: " + namespaceCode + " and name: " + name);
055        }
056
057        return bos != null && bos.iterator().hasNext() ? KimTypeBo.to(bos.iterator().next()) : null;
058    }
059
060    @Override
061    public Collection<KimType> findAllKimTypes() {
062        final Collection<KimTypeBo> bos
063                = businessObjectService.findMatching(KimTypeBo.class, Collections.singletonMap("active", "true"));
064        final Collection<KimType> ims = new ArrayList<KimType>();
065
066        if (bos != null) {
067            for (KimTypeBo bo : bos) {
068                if (bo != null) {
069                    ims.add(KimTypeBo.to(bo));
070                }
071            }
072        }
073        return Collections.unmodifiableCollection(ims);
074    }
075
076    public void setBusinessObjectService(final BusinessObjectService businessObjectService) {
077        this.businessObjectService = businessObjectService;
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}