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 018 019import org.apache.commons.collections.CollectionUtils; 020import org.apache.commons.lang.StringUtils; 021import org.kuali.rice.core.api.exception.RiceIllegalArgumentException; 022import org.kuali.rice.core.api.exception.RiceIllegalStateException; 023import org.kuali.rice.krad.service.BusinessObjectService; 024import org.kuali.rice.krms.api.repository.type.KrmsAttributeDefinition; 025import org.kuali.rice.krms.api.repository.type.KrmsTypeDefinition; 026import org.kuali.rice.krms.api.repository.type.KrmsTypeRepositoryService; 027 028import javax.jws.WebParam; 029import java.util.ArrayList; 030import java.util.Collection; 031import java.util.Collections; 032import java.util.HashMap; 033import java.util.List; 034import java.util.Map; 035 036public final class KrmsTypeBoServiceImpl implements KrmsTypeRepositoryService { 037 038 private BusinessObjectService businessObjectService; 039 040 /** 041 * This overridden method creates a KrmsType if it does not 042 * already exist in the repository. 043 * 044 * @see org.kuali.rice.krms.api.repository.type.KrmsTypeRepositoryService#createKrmsType(org.kuali.rice.krms.api.repository.type.KrmsTypeDefinition) 045 */ 046 @Override 047 public KrmsTypeDefinition createKrmsType(KrmsTypeDefinition krmsType) { 048 if (krmsType == null){ 049 throw new RiceIllegalArgumentException("krmsType is null"); 050 } 051 final String nameKey = krmsType.getName(); 052 final String namespaceKey = krmsType.getNamespace(); 053 final KrmsTypeDefinition existing = getTypeByName(namespaceKey, nameKey); 054 if (existing != null && existing.getName().equals(nameKey) && existing.getNamespace().equals(namespaceKey)){ 055 throw new RiceIllegalStateException("the KRMS Type to create already exists: " + krmsType); 056 } 057 058 KrmsTypeBo bo = (KrmsTypeBo)businessObjectService.save(KrmsTypeBo.from(krmsType)); 059 060 return KrmsTypeBo.to(bo); 061 } 062 063 /** 064 * This overridden method updates an existing KrmsType 065 * 066 * @see org.kuali.rice.krms.api.repository.type.KrmsTypeRepositoryService#updateKrmsType(org.kuali.rice.krms.api.repository.type.KrmsTypeDefinition) 067 */ 068 @Override 069 public KrmsTypeDefinition updateKrmsType(KrmsTypeDefinition krmsType) { 070 if (krmsType == null) { 071 throw new RiceIllegalArgumentException("krmsType is null"); 072 } 073 final String idKey = krmsType.getId(); 074 final KrmsTypeBo existing = businessObjectService.findBySinglePrimaryKey(KrmsTypeBo.class, idKey); 075 if (existing == null) { 076 throw new RiceIllegalStateException("the KRMS type does not exist: " + krmsType); 077 } 078 final KrmsTypeDefinition toUpdate; 079 if (!existing.getId().equals(krmsType.getId())){ 080 final KrmsTypeDefinition.Builder builder = KrmsTypeDefinition.Builder.create(krmsType); 081 builder.setId(existing.getId()); 082 toUpdate = builder.build(); 083 } else { 084 toUpdate = krmsType; 085 } 086 087 return KrmsTypeBo.to(businessObjectService.save(KrmsTypeBo.from(toUpdate))); 088 } 089 090 @Override 091 public KrmsTypeDefinition getTypeById(final String id) { 092 if (StringUtils.isBlank(id)) { 093 throw new RiceIllegalArgumentException("id was a null or blank value"); 094 } 095 096 KrmsTypeBo krmsTypeBo = businessObjectService.findBySinglePrimaryKey(KrmsTypeBo.class, id); 097 098 return KrmsTypeBo.to(krmsTypeBo); 099 } 100 101 @Override 102 public KrmsTypeDefinition getTypeByName(final String namespaceCode, final String name) { 103 if (StringUtils.isBlank(namespaceCode)) { 104 throw new RiceIllegalArgumentException("namespaceCode was a null or blank value"); 105 } 106 if (StringUtils.isBlank(name)) { 107 throw new RiceIllegalArgumentException("name was a null or blank value"); 108 } 109 final Map<String, Object> map = new HashMap<String, Object>(); 110 map.put("namespace", namespaceCode); 111 map.put("name", name); 112 113 KrmsTypeBo myType = businessObjectService.findByPrimaryKey(KrmsTypeBo.class, Collections.unmodifiableMap(map)); 114 return KrmsTypeBo.to(myType); 115 } 116 117 @Override 118 public List<KrmsTypeDefinition> findAllTypesByNamespace(final String namespaceCode) { 119 if (StringUtils.isBlank(namespaceCode)) { 120 throw new RiceIllegalArgumentException("namespaceCode was a null or blank value"); 121 } 122 final Map<String, Object> map = new HashMap<String, Object>(); 123 map.put("namespace", namespaceCode); 124 map.put("active", Boolean.TRUE); 125 126 Collection<KrmsTypeBo> krmsTypeBos = businessObjectService.findMatching(KrmsTypeBo.class, Collections.unmodifiableMap(map)); 127 128 return convertListOfBosToImmutables(krmsTypeBos); 129 } 130 131 @Override 132 public List<KrmsTypeDefinition> findAllTypes() { 133 final Map<String, Object> map = new HashMap<String, Object>(); 134 map.put("active", Boolean.TRUE); 135 136 Collection<KrmsTypeBo> krmsTypeBos = businessObjectService.findMatching(KrmsTypeBo.class, Collections.unmodifiableMap(map)); 137 return convertListOfBosToImmutables(krmsTypeBos); 138 } 139 140 @Override 141 public List<KrmsTypeDefinition> findAllAgendaTypesByContextId(String contextId) { 142 if (StringUtils.isBlank(contextId)) { 143 throw new RiceIllegalArgumentException("contextId was a null or blank value"); 144 } 145 final Map<String, Object> map = new HashMap<String, Object>(); 146 map.put("contextId", contextId); 147 Collection<ContextValidAgendaBo> contextValidAgendaBos = businessObjectService.findMatchingOrderBy(ContextValidAgendaBo.class, Collections.unmodifiableMap(map), "agendaType.name", true); 148 List<KrmsTypeDefinition> agendaTypes = new ArrayList<KrmsTypeDefinition>(); 149 for (ContextValidAgendaBo contextValidAgendaBo : contextValidAgendaBos) { 150 agendaTypes.add(KrmsTypeBo.to(contextValidAgendaBo.getAgendaType())); 151 } 152 return agendaTypes; 153 } 154 155 @Override 156 public KrmsTypeDefinition getAgendaTypeByAgendaTypeIdAndContextId(String agendaTypeId, String contextId) { 157 if (StringUtils.isBlank(agendaTypeId)) { 158 throw new RiceIllegalArgumentException("agendaTypeId was a null or blank value"); 159 } 160 if (StringUtils.isBlank(contextId)) { 161 throw new RiceIllegalArgumentException("contextId was a null or blank value"); 162 } 163 final Map<String, Object> map = new HashMap<String, Object>(); 164 map.put("agendaTypeId", agendaTypeId); 165 map.put("contextId", contextId); 166 ContextValidAgendaBo contextValidAgendaBo = businessObjectService.findByPrimaryKey(ContextValidAgendaBo.class, Collections.unmodifiableMap(map)); 167 return KrmsTypeBo.to(contextValidAgendaBo.getAgendaType()); 168 } 169 170 @Override 171 public List<KrmsTypeDefinition> findAllRuleTypesByContextId(String contextId) { 172 if (StringUtils.isBlank(contextId)) { 173 throw new RiceIllegalArgumentException("contextId was a null or blank value"); 174 } 175 final Map<String, Object> map = new HashMap<String, Object>(); 176 map.put("contextId", contextId); 177 Collection<ContextValidRuleBo> contextValidRuleBos = businessObjectService.findMatchingOrderBy(ContextValidRuleBo.class, Collections.unmodifiableMap(map), "ruleType.name", true); 178 List<KrmsTypeDefinition> ruleTypes = new ArrayList<KrmsTypeDefinition>(); 179 for (ContextValidRuleBo contextValidRuleBo : contextValidRuleBos) { 180 ruleTypes.add(KrmsTypeBo.to(contextValidRuleBo.getRuleType())); 181 } 182 return ruleTypes; 183 } 184 185 @Override 186 public KrmsTypeDefinition getRuleTypeByRuleTypeIdAndContextId(String ruleTypeId, String contextId) { 187 if (StringUtils.isBlank(ruleTypeId)) { 188 throw new RiceIllegalArgumentException("ruleTypeId was a null or blank value"); 189 } 190 if (StringUtils.isBlank(contextId)) { 191 throw new RiceIllegalArgumentException("contextId was a null or blank value"); 192 } 193 final Map<String, Object> map = new HashMap<String, Object>(); 194 map.put("ruleTypeId", ruleTypeId); 195 map.put("contextId", contextId); 196 ContextValidRuleBo contextValidRuleBo = businessObjectService.findByPrimaryKey(ContextValidRuleBo.class, Collections.unmodifiableMap(map)); 197 return KrmsTypeBo.to(contextValidRuleBo.getRuleType()); 198 } 199 200 @Override 201 public List<KrmsTypeDefinition> findAllActionTypesByContextId(String contextId) { 202 if (StringUtils.isBlank(contextId)) { 203 throw new RiceIllegalArgumentException("contextId was a null or blank value"); 204 } 205 final Map<String, Object> map = new HashMap<String, Object>(); 206 map.put("contextId", contextId); 207 Collection<ContextValidActionBo> contextValidActionBos = businessObjectService.findMatchingOrderBy(ContextValidActionBo.class, Collections.unmodifiableMap(map), "actionType.name", true); 208 List<KrmsTypeDefinition> actionTypes = new ArrayList<KrmsTypeDefinition>(); 209 for (ContextValidActionBo contextValidActionBo : contextValidActionBos) { 210 actionTypes.add(KrmsTypeBo.to(contextValidActionBo.getActionType())); 211 } 212 return actionTypes; 213 } 214 215 @Override 216 public KrmsTypeDefinition getActionTypeByActionTypeIdAndContextId(String actionTypeId, String contextId) { 217 if (StringUtils.isBlank(actionTypeId)) { 218 throw new RiceIllegalArgumentException("actionTypeId was a null or blank value"); 219 } 220 if (StringUtils.isBlank(contextId)) { 221 throw new RiceIllegalArgumentException("contextId was a null or blank value"); 222 } 223 final Map<String, Object> map = new HashMap<String, Object>(); 224 map.put("actionTypeId", actionTypeId); 225 map.put("contextId", contextId); 226 ContextValidActionBo contextValidActionBo = businessObjectService.findByPrimaryKey(ContextValidActionBo.class, Collections.unmodifiableMap(map)); 227 return KrmsTypeBo.to(contextValidActionBo.getActionType()); 228 } 229 230 @Override 231 public KrmsAttributeDefinition getAttributeDefinitionById(String attributeDefinitionId) { 232 if (StringUtils.isBlank(attributeDefinitionId)) { 233 throw new RiceIllegalArgumentException("attributeDefinitionId was a null or blank value"); 234 } 235 KrmsAttributeDefinitionBo krmsAttributeDefinitionBo = businessObjectService.findBySinglePrimaryKey(KrmsAttributeDefinitionBo.class, attributeDefinitionId); 236 return KrmsAttributeDefinitionBo.to(krmsAttributeDefinitionBo); 237 } 238 239 @Override 240 public KrmsAttributeDefinition getAttributeDefinitionByName(@WebParam(name = "namespaceCode") String namespaceCode, 241 @WebParam(name = "name") String name) { 242 if (StringUtils.isBlank(namespaceCode)) { 243 throw new RiceIllegalArgumentException("namespaceCode was a null or blank value"); 244 } 245 if (StringUtils.isBlank(name)) { 246 throw new RiceIllegalArgumentException("name was a null or blank value"); 247 } 248 final Map<String, Object> criteria = new HashMap<String, Object>(); 249 criteria.put("name", name); 250 criteria.put("namespace", namespaceCode); 251 252 Collection<KrmsAttributeDefinitionBo> attributeDefinitionBos = businessObjectService.findMatching(KrmsAttributeDefinitionBo.class, criteria); 253 if (CollectionUtils.isEmpty(attributeDefinitionBos)) { 254 return null; 255 } 256 return KrmsAttributeDefinitionBo.to(attributeDefinitionBos.iterator().next()); 257 } 258 259 /** 260 * Sets the businessObjectService attribute value. 261 * 262 * @param businessObjectService The businessObjectService to set. 263 */ 264 public void setBusinessObjectService(final BusinessObjectService businessObjectService) { 265 this.businessObjectService = businessObjectService; 266 } 267 268 /** 269 * Converts a List<KrmsTypeBo> to an Unmodifiable List<KrmsType> 270 * 271 * @param krmsTypeBos a mutable List<KrmsTypeBo> to made completely immutable. 272 * @return An unmodifiable List<KrmsType> 273 */ 274 protected List<KrmsTypeDefinition> convertListOfBosToImmutables(final Collection<KrmsTypeBo> krmsTypeBos) { 275 ArrayList<KrmsTypeDefinition> krmsTypes = new ArrayList<KrmsTypeDefinition>(); 276 for (KrmsTypeBo bo : krmsTypeBos) { 277 KrmsTypeDefinition krmsType = KrmsTypeBo.to(bo); 278 krmsTypes.add(krmsType); 279 } 280 return Collections.unmodifiableList(krmsTypes); 281 } 282 283}