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.krms.impl.repository; 017 018import org.apache.commons.lang.StringUtils; 019import org.kuali.rice.core.api.exception.RiceIllegalArgumentException; 020import org.kuali.rice.krad.service.BusinessObjectService; 021import org.kuali.rice.krms.api.repository.language.NaturalLanguageUsage; 022import java.util.Collection; 023import java.util.Collections; 024import java.util.HashMap; 025import java.util.LinkedList; 026import java.util.List; 027import java.util.Map; 028 029/** 030 * Implementation of the @{link NaturalLanguageUsageBoService} interface for accessing {@link NaturalLanguageUsageBo} related business objects. 031 * 032 * @author Kuali Rice Team (rice.collab@kuali.org) 033 * 034 */ 035public final class NaturalLanguageUsageBoServiceImpl 036 implements NaturalLanguageUsageBoService 037{ 038 039 private BusinessObjectService businessObjectService; 040 private KrmsAttributeDefinitionService attributeDefinitionService; 041 042 /** 043 * Sets the value of BusinessObjectService to the given value. 044 * 045 * @param businessObjectService the BusinessObjectService value to set. 046 * 047 */ 048 public void setBusinessObjectService(BusinessObjectService businessObjectService) { 049 this.businessObjectService = businessObjectService; 050 } 051 052 public void setAttributeDefinitionService(KrmsAttributeDefinitionService attributeDefinitionService) { 053 this.attributeDefinitionService = attributeDefinitionService; 054 } 055 056 public KrmsAttributeDefinitionService getAttributeDefinitionService() { 057 if (attributeDefinitionService == null) { 058 attributeDefinitionService = KrmsRepositoryServiceLocator.getKrmsAttributeDefinitionService(); 059 } 060 return attributeDefinitionService; 061 } 062 063 @Override 064 public NaturalLanguageUsage createNaturalLanguageUsage(NaturalLanguageUsage naturalLanguageUsage) { 065 incomingParamCheck(naturalLanguageUsage , "naturalLanguageUsage"); 066 final String naturalLanguageUsageIdKey = naturalLanguageUsage.getId(); 067 final NaturalLanguageUsage existing = getNaturalLanguageUsage(naturalLanguageUsageIdKey); 068 if (existing != null){ throw new IllegalStateException("the NaturalLanguageUsage to create already exists: " + naturalLanguageUsage); } 069 NaturalLanguageUsageBo bo = (NaturalLanguageUsageBo)businessObjectService.save(from(naturalLanguageUsage)); 070 return NaturalLanguageUsageBo.to(bo); 071 } 072 073 @Override 074 public NaturalLanguageUsage getNaturalLanguageUsage(String naturalLanguageUsageId) { 075 incomingParamCheck(naturalLanguageUsageId , "naturalLanguageUsageId"); 076 NaturalLanguageUsageBo bo = businessObjectService.findBySinglePrimaryKey(NaturalLanguageUsageBo.class, naturalLanguageUsageId); 077 return NaturalLanguageUsageBo.to(bo); 078 } 079 080 @Override 081 public NaturalLanguageUsage getNaturalLanguageUsageByName(String namespace, String name) { 082 if (StringUtils.isBlank(namespace)) { 083 throw new RiceIllegalArgumentException("namespace was a null or blank value"); 084 } 085 if (StringUtils.isBlank(name)) { 086 throw new RiceIllegalArgumentException("name was a null or blank value"); 087 } 088 final Map<String, Object> map = new HashMap<String, Object>(); 089 map.put("namespace", namespace); 090 map.put("name", name); 091 092 NaturalLanguageUsageBo usageBo = businessObjectService.findByPrimaryKey(NaturalLanguageUsageBo.class, Collections.unmodifiableMap(map)); 093 return NaturalLanguageUsageBo.to(usageBo); 094 } 095 096 @Override 097 public void updateNaturalLanguageUsage(NaturalLanguageUsage naturalLanguageUsage) { 098 incomingParamCheck(naturalLanguageUsage , "naturalLanguageUsage"); 099 final NaturalLanguageUsage existing = getNaturalLanguageUsage(naturalLanguageUsage.getId()); 100 if (existing == null){ throw new IllegalStateException("the NaturalLanguageUsage to update does not exists: " + naturalLanguageUsage);} 101 final NaturalLanguageUsage toUpdate; 102 if (!existing.getId().equals(naturalLanguageUsage.getId())){ 103 // if passed in id does not match existing id, correct it 104 final NaturalLanguageUsage.Builder builder = NaturalLanguageUsage.Builder.create(naturalLanguageUsage); 105 builder.setId(existing.getId()); 106 toUpdate = builder.build(); 107 } else { 108 toUpdate = naturalLanguageUsage; 109 } 110 111 // copy all updateable fields to bo 112 NaturalLanguageUsageBo boToUpdate = from(toUpdate); 113 114 // update the rule and create new attributes 115 businessObjectService.save(boToUpdate); 116 } 117 118 @Override 119 public void deleteNaturalLanguageUsage(String naturalLanguageUsageId) { 120 incomingParamCheck(naturalLanguageUsageId , "naturalLanguageUsageId"); 121 final NaturalLanguageUsage existing = getNaturalLanguageUsage(naturalLanguageUsageId); 122 if (existing == null){ throw new IllegalStateException("the NaturalLanguageUsage to delete does not exists: " + naturalLanguageUsageId);} 123 businessObjectService.delete(from(existing)); 124 } 125 126 @Override 127 public List<NaturalLanguageUsage> findNaturalLanguageUsagesByName(String name) { 128 if (org.apache.commons.lang.StringUtils.isBlank(name)) { 129 throw new IllegalArgumentException("name is null or blank"); 130 } 131 final Map<String, Object> map = new HashMap<String, Object>(); 132 map.put("name", name); 133 List<NaturalLanguageUsageBo> bos = (List<NaturalLanguageUsageBo>) businessObjectService.findMatching(NaturalLanguageUsageBo.class, map); 134 return convertBosToImmutables(bos); 135 } 136 137 @Override 138 public List<NaturalLanguageUsage> findNaturalLanguageUsagesByDescription(String description) { 139 if (org.apache.commons.lang.StringUtils.isBlank(description)) { 140 throw new IllegalArgumentException("description is null or blank"); 141 } 142 final Map<String, Object> map = new HashMap<String, Object>(); 143 map.put("description", description); 144 List<NaturalLanguageUsageBo> bos = (List<NaturalLanguageUsageBo>) businessObjectService.findMatching(NaturalLanguageUsageBo.class, map); 145 return convertBosToImmutables(bos); 146 } 147 148 @Override 149 public List<NaturalLanguageUsage> findNaturalLanguageUsagesByNamespace(String namespace) { 150 if (org.apache.commons.lang.StringUtils.isBlank(namespace)) { 151 throw new IllegalArgumentException("namespace is null or blank"); 152 } 153 final Map<String, Object> map = new HashMap<String, Object>(); 154 map.put("namespace", namespace); 155 List<NaturalLanguageUsageBo> bos = (List<NaturalLanguageUsageBo>) businessObjectService.findMatching(NaturalLanguageUsageBo.class, map); 156 return convertBosToImmutables(bos); 157 } 158 159 public List<NaturalLanguageUsage> convertBosToImmutables(final Collection<NaturalLanguageUsageBo> naturalLanguageUsageBos) { 160 List<NaturalLanguageUsage> immutables = new LinkedList<NaturalLanguageUsage>(); 161 if (naturalLanguageUsageBos != null) { 162 NaturalLanguageUsage immutable = null; 163 for (NaturalLanguageUsageBo bo : naturalLanguageUsageBos ) { 164 immutable = to(bo); 165 immutables.add(immutable); 166 } 167 } 168 return Collections.unmodifiableList(immutables); 169 } 170 171 @Override 172 public NaturalLanguageUsage to(NaturalLanguageUsageBo naturalLanguageUsageBo) { 173 return NaturalLanguageUsageBo.to(naturalLanguageUsageBo); 174 } 175 176 public NaturalLanguageUsageBo from(NaturalLanguageUsage naturalLanguageUsage) { 177 return NaturalLanguageUsageBo.from(naturalLanguageUsage); 178 } 179 180 private void incomingParamCheck(Object object, String name) { 181 if (object == null) { 182 throw new IllegalArgumentException(name + " was null"); 183 } else if (object instanceof String 184 && StringUtils.isBlank((String)object)) { 185 throw new IllegalArgumentException(name + " was blank"); 186 } 187 } 188 189}