001    /**
002     * Copyright 2005-2011 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     */
016    package org.kuali.rice.krms.impl.repository;
017    
018    import java.util.ArrayList;
019    import java.util.Collection;
020    import java.util.Collections;
021    import java.util.HashMap;
022    import java.util.List;
023    import java.util.Map;
024    
025    import org.apache.commons.lang.StringUtils;
026    import org.kuali.rice.krad.service.BusinessObjectService;
027    import org.kuali.rice.krms.api.repository.term.TermDefinition;
028    import org.kuali.rice.krms.api.repository.term.TermResolverDefinition;
029    import org.kuali.rice.krms.api.repository.term.TermSpecificationDefinition;
030    import org.springframework.util.CollectionUtils;
031    
032    /**
033     * Implementation of {@link TermBoService}
034     * 
035     * @author Kuali Rice Team (rice.collab@kuali.org)
036     *
037     */
038    public class TermBoServiceImpl implements TermBoService {
039            
040            private BusinessObjectService businessObjectService;
041    
042            /**
043             * @param businessObjectService the businessObjectService to set
044             */
045            public void setBusinessObjectService(BusinessObjectService businessObjectService) {
046                    this.businessObjectService = businessObjectService;
047            }
048            
049            /**
050             * @see org.kuali.rice.krms.impl.repository.TermBoService#getTermSpecificationById(java.lang.String)
051             */
052            @Override
053            public TermSpecificationDefinition getTermSpecificationById(String id) {
054                    TermSpecificationBo termSpecificationBo = 
055                            businessObjectService.findBySinglePrimaryKey(TermSpecificationBo.class, id);
056                    return TermSpecificationDefinition.Builder.create(termSpecificationBo).build();
057            }
058            
059            /**
060             * @see org.kuali.rice.krms.impl.repository.TermBoService#createTermSpecification(org.kuali.rice.krms.api.repository.term.TermSpecificationDefinition)
061             */
062            @Override
063            public TermSpecificationDefinition createTermSpecification(TermSpecificationDefinition termSpec) {
064                    if (!StringUtils.isBlank(termSpec.getId())) {
065                            throw new IllegalArgumentException("for creation, TermSpecification.id must be null");
066                    }
067                    
068                    TermSpecificationBo termSpecBo = TermSpecificationBo.from(termSpec);
069                    
070                    businessObjectService.save(termSpecBo);
071                    
072                    return TermSpecificationBo.to(termSpecBo);
073            }
074            
075            /**
076             * @see org.kuali.rice.krms.impl.repository.TermBoService#createTermDefinition(org.kuali.rice.krms.api.repository.term.TermDefinition)
077             */
078            @Override
079            public TermDefinition createTermDefinition(TermDefinition termDef) {
080                    if (!StringUtils.isBlank(termDef.getId())) {
081                            throw new IllegalArgumentException("for creation, TermDefinition.id must be null");
082                    }
083                    
084                    TermBo termBo = TermBo.from(termDef);
085                    
086                    businessObjectService.save(termBo);
087                    
088                    return TermBo.to(termBo);
089            }
090            
091            /**
092             * @see org.kuali.rice.krms.impl.repository.TermBoService#createTermResolver(org.kuali.rice.krms.api.repository.term.TermResolverDefinition)
093             */
094            @Override
095            public TermResolverDefinition createTermResolver(TermResolverDefinition termResolver) {
096                    if (!StringUtils.isBlank(termResolver.getId())) {
097                            throw new IllegalArgumentException("for creation, TermResolverDefinition.id must be null");
098                    }
099                    
100                    TermResolverBo termResolverBo = TermResolverBo.from(termResolver);
101                    
102                    termResolverBo = (TermResolverBo)businessObjectService.save(termResolverBo);
103                    
104                    return TermResolverBo.to(termResolverBo);
105            }
106            
107            /**
108             * @see org.kuali.rice.krms.impl.repository.TermBoService#getTermById(java.lang.String)
109             */
110            @Override
111            public TermDefinition getTermById(String id) {
112                    TermDefinition result = null;
113                    
114                    if (StringUtils.isBlank(id)) {
115                            throw new IllegalArgumentException("id must not be blank or null");
116                    }
117                    TermBo termBo = businessObjectService.findBySinglePrimaryKey(TermBo.class, id);
118                    
119                    if (termBo != null) {
120                            result= TermBo.to(termBo);
121                    }
122                    
123                    return result;
124            }
125            
126            /**
127             * @see org.kuali.rice.krms.impl.repository.TermBoService#getTermResolverById(java.lang.String)
128             */
129            @Override
130            public TermResolverDefinition getTermResolverById(String id) {
131                    TermResolverDefinition result = null;
132                    
133                    if (StringUtils.isBlank(id)) {
134                            throw new IllegalArgumentException("id must not be blank or null");
135                    }
136                    TermResolverBo termResolverBo = businessObjectService.findBySinglePrimaryKey(TermResolverBo.class, id);
137                    
138                    if (termResolverBo != null) {
139                            result = TermResolverBo.to(termResolverBo);
140                    }
141                    
142                    return result;
143            }
144            
145        @Override
146        public List<TermResolverDefinition> getTermResolversByNamespace(String namespace) {
147            List<TermResolverDefinition> results = null;
148    
149            if (StringUtils.isBlank(namespace)) {
150                throw new IllegalArgumentException("namespace must not be blank or null");
151            }
152    
153            Map fieldValues = new HashMap();
154            fieldValues.put("namespace", namespace);
155    
156            Collection<TermResolverBo> termResolverBos = businessObjectService.findMatching(TermResolverBo.class, fieldValues);
157    
158            if (!CollectionUtils.isEmpty(termResolverBos)) {
159                results = new ArrayList<TermResolverDefinition>(termResolverBos.size());
160    
161                for (TermResolverBo termResolverBo : termResolverBos) if (termResolverBo != null) {
162                    results.add(TermResolverBo.to(termResolverBo));
163                }
164            } else {
165                results = Collections.emptyList();
166            }
167    
168            return results;
169        }
170    }