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.core.api.exception.RiceIllegalArgumentException;
020import org.kuali.rice.krad.data.DataObjectService;
021import org.kuali.rice.krad.data.PersistenceOption;
022import org.kuali.rice.krms.api.repository.language.NaturalLanguageUsage;
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;
032import static org.kuali.rice.krms.impl.repository.BusinessObjectServiceMigrationUtils.findSingleMatching;
033
034/**
035 * Implementation of the @{link NaturalLanguageUsageBoService} interface for accessing  {@link NaturalLanguageUsageBo}
036 * related business objects.
037 *
038 * @author Kuali Rice Team (rice.collab@kuali.org)
039 */
040public class NaturalLanguageUsageBoServiceImpl implements NaturalLanguageUsageBoService {
041
042    private DataObjectService dataObjectService;
043    private KrmsAttributeDefinitionService attributeDefinitionService;
044
045    /**
046     * Sets the value of DataObjectService to the given value.
047     *
048     * @param dataObjectService the DataObjectService value to set.
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
063        return attributeDefinitionService;
064    }
065
066    @Override
067    public NaturalLanguageUsage createNaturalLanguageUsage(NaturalLanguageUsage naturalLanguageUsage) {
068        incomingParamCheck(naturalLanguageUsage, "naturalLanguageUsage");
069        if (StringUtils.isNotEmpty(naturalLanguageUsage.getId())) {
070            final String naturalLanguageUsageIdKey = naturalLanguageUsage.getId();
071            final NaturalLanguageUsage existing = getNaturalLanguageUsage(naturalLanguageUsageIdKey);
072
073            if (existing != null) {
074                throw new IllegalStateException(
075                        "the NaturalLanguageUsage to create already exists: " + naturalLanguageUsage);
076            }
077        } else {
078            final NaturalLanguageUsage existing = getNaturalLanguageUsageByName(naturalLanguageUsage.getNamespace(),
079                    naturalLanguageUsage.getName());
080
081            if (existing != null) {
082                throw new IllegalStateException(
083                        "the NaturalLanguageUsage to create already exists: " + naturalLanguageUsage);
084            }
085        }
086
087        NaturalLanguageUsageBo bo = dataObjectService.save(from(naturalLanguageUsage), PersistenceOption.FLUSH);
088
089        return NaturalLanguageUsageBo.to(bo);
090    }
091
092    @Override
093    public NaturalLanguageUsage getNaturalLanguageUsage(String naturalLanguageUsageId) {
094        incomingParamCheck(naturalLanguageUsageId, "naturalLanguageUsageId");
095        NaturalLanguageUsageBo bo = dataObjectService.find(NaturalLanguageUsageBo.class, naturalLanguageUsageId);
096
097        return NaturalLanguageUsageBo.to(bo);
098    }
099
100    @Override
101    public NaturalLanguageUsage getNaturalLanguageUsageByName(String namespace, String name) {
102        if (StringUtils.isBlank(namespace)) {
103            throw new RiceIllegalArgumentException("namespace was a null or blank value");
104        }
105
106        if (StringUtils.isBlank(name)) {
107            throw new RiceIllegalArgumentException("name was a null or blank value");
108        }
109
110        final Map<String, Object> map = new HashMap<String, Object>();
111        map.put("namespace", namespace);
112        map.put("name", name);
113
114        NaturalLanguageUsageBo usageBo = findSingleMatching(dataObjectService, NaturalLanguageUsageBo.class, map);
115
116        return NaturalLanguageUsageBo.to(usageBo);
117    }
118
119    @Override
120    public NaturalLanguageUsage updateNaturalLanguageUsage(NaturalLanguageUsage naturalLanguageUsage) {
121        incomingParamCheck(naturalLanguageUsage, "naturalLanguageUsage");
122        final NaturalLanguageUsage existing = getNaturalLanguageUsage(naturalLanguageUsage.getId());
123
124        if (existing == null) {
125            throw new IllegalStateException(
126                    "the NaturalLanguageUsage to update does not exists: " + naturalLanguageUsage);
127        }
128
129        final NaturalLanguageUsage toUpdate;
130
131        if (!existing.getId().equals(naturalLanguageUsage.getId())) {
132            // if passed in id does not match existing id, correct it
133            final NaturalLanguageUsage.Builder builder = NaturalLanguageUsage.Builder.create(naturalLanguageUsage);
134            builder.setId(existing.getId());
135            toUpdate = builder.build();
136        } else {
137            toUpdate = naturalLanguageUsage;
138        }
139
140        // copy all updateable fields to bo
141        NaturalLanguageUsageBo boToUpdate = from(toUpdate);
142
143        // update the rule and create new attributes
144        NaturalLanguageUsageBo updatedData = dataObjectService.save(boToUpdate, PersistenceOption.FLUSH);
145
146        return to(updatedData);
147    }
148
149    @Override
150    public void deleteNaturalLanguageUsage(String naturalLanguageUsageId) {
151        incomingParamCheck(naturalLanguageUsageId, "naturalLanguageUsageId");
152        final NaturalLanguageUsage existing = getNaturalLanguageUsage(naturalLanguageUsageId);
153
154        if (existing == null) {
155            throw new IllegalStateException(
156                    "the NaturalLanguageUsage to delete does not exists: " + naturalLanguageUsageId);
157        }
158
159        dataObjectService.delete(from(existing));
160    }
161
162    @Override
163    public List<NaturalLanguageUsage> findNaturalLanguageUsagesByName(String name) {
164        if (org.apache.commons.lang.StringUtils.isBlank(name)) {
165            throw new IllegalArgumentException("name is null or blank");
166        }
167
168        final Map<String, Object> map = new HashMap<String, Object>();
169        map.put("name", name);
170        List<NaturalLanguageUsageBo> bos = findMatching(dataObjectService, NaturalLanguageUsageBo.class, map);
171
172        return convertBosToImmutables(bos);
173    }
174
175    @Override
176    public List<NaturalLanguageUsage> findNaturalLanguageUsagesByDescription(String description) {
177        if (org.apache.commons.lang.StringUtils.isBlank(description)) {
178            throw new IllegalArgumentException("description is null or blank");
179        }
180
181        final Map<String, Object> map = new HashMap<String, Object>();
182        map.put("description", description);
183        List<NaturalLanguageUsageBo> bos = findMatching(dataObjectService, NaturalLanguageUsageBo.class, map);
184
185        return convertBosToImmutables(bos);
186    }
187
188    @Override
189    public List<NaturalLanguageUsage> findNaturalLanguageUsagesByNamespace(String namespace) {
190        if (org.apache.commons.lang.StringUtils.isBlank(namespace)) {
191            throw new IllegalArgumentException("namespace is null or blank");
192        }
193
194        final Map<String, Object> map = new HashMap<String, Object>();
195        map.put("namespace", namespace);
196        List<NaturalLanguageUsageBo> bos = findMatching(dataObjectService, NaturalLanguageUsageBo.class, map);
197
198        return convertBosToImmutables(bos);
199    }
200
201    public List<NaturalLanguageUsage> convertBosToImmutables(
202            final Collection<NaturalLanguageUsageBo> naturalLanguageUsageBos) {
203        List<NaturalLanguageUsage> immutables = new LinkedList<NaturalLanguageUsage>();
204
205        if (naturalLanguageUsageBos != null) {
206            NaturalLanguageUsage immutable = null;
207            for (NaturalLanguageUsageBo bo : naturalLanguageUsageBos) {
208                immutable = to(bo);
209                immutables.add(immutable);
210            }
211        }
212
213        return Collections.unmodifiableList(immutables);
214    }
215
216    @Override
217    public NaturalLanguageUsage to(NaturalLanguageUsageBo naturalLanguageUsageBo) {
218        return NaturalLanguageUsageBo.to(naturalLanguageUsageBo);
219    }
220
221    public NaturalLanguageUsageBo from(NaturalLanguageUsage naturalLanguageUsage) {
222        return NaturalLanguageUsageBo.from(naturalLanguageUsage);
223    }
224
225    private void incomingParamCheck(Object object, String name) {
226        if (object == null) {
227            throw new IllegalArgumentException(name + " was null");
228        } else if (object instanceof String && StringUtils.isBlank((String) object)) {
229            throw new IllegalArgumentException(name + " was blank");
230        }
231    }
232}