| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
package org.kuali.rice.krms.impl.repository; |
| 17 | |
|
| 18 | |
import java.util.Collection; |
| 19 | |
import java.util.Collections; |
| 20 | |
import java.util.HashMap; |
| 21 | |
import java.util.HashSet; |
| 22 | |
import java.util.List; |
| 23 | |
import java.util.Map; |
| 24 | |
import java.util.Map.Entry; |
| 25 | |
import java.util.Set; |
| 26 | |
|
| 27 | |
import org.apache.commons.lang.StringUtils; |
| 28 | |
import org.kuali.rice.core.api.exception.RiceIllegalStateException; |
| 29 | |
import org.kuali.rice.krad.service.BusinessObjectService; |
| 30 | |
import org.kuali.rice.krad.service.KRADServiceLocator; |
| 31 | |
import org.kuali.rice.krad.service.SequenceAccessorService; |
| 32 | |
import org.kuali.rice.krms.api.repository.agenda.AgendaDefinition; |
| 33 | |
import org.kuali.rice.krms.api.repository.agenda.AgendaItem; |
| 34 | |
import org.kuali.rice.krms.api.repository.type.KrmsAttributeDefinition; |
| 35 | |
import org.kuali.rice.krms.impl.util.KrmsImplConstants.PropertyNames; |
| 36 | |
|
| 37 | 20 | public final class AgendaBoServiceImpl implements AgendaBoService { |
| 38 | |
|
| 39 | |
|
| 40 | |
|
| 41 | |
private BusinessObjectService businessObjectService; |
| 42 | |
private KrmsAttributeDefinitionService attributeDefinitionService; |
| 43 | |
private SequenceAccessorService sequenceAccessorService; |
| 44 | |
|
| 45 | |
|
| 46 | |
|
| 47 | |
|
| 48 | |
@Override |
| 49 | |
public AgendaDefinition createAgenda(AgendaDefinition agenda) { |
| 50 | 3 | if (agenda == null){ |
| 51 | 1 | throw new IllegalArgumentException("agenda is null"); |
| 52 | |
} |
| 53 | 2 | final String nameKey = agenda.getName(); |
| 54 | 2 | final String contextId = agenda.getContextId(); |
| 55 | 2 | final AgendaDefinition existing = getAgendaByNameAndContextId(nameKey, contextId); |
| 56 | 2 | if (existing != null){ |
| 57 | 1 | throw new IllegalStateException("the agenda to create already exists: " + agenda); |
| 58 | |
} |
| 59 | |
|
| 60 | 1 | AgendaBo agendaBo = from(agenda); |
| 61 | 1 | businessObjectService.save(agendaBo); |
| 62 | 1 | return to(agendaBo); |
| 63 | |
} |
| 64 | |
|
| 65 | |
|
| 66 | |
|
| 67 | |
|
| 68 | |
@Override |
| 69 | |
public void updateAgenda(AgendaDefinition agenda) { |
| 70 | 3 | if (agenda == null){ |
| 71 | 1 | throw new IllegalArgumentException("agenda is null"); |
| 72 | |
} |
| 73 | |
|
| 74 | |
|
| 75 | 2 | final String agendaIdKey = agenda.getId(); |
| 76 | 2 | final AgendaBo existing = businessObjectService.findBySinglePrimaryKey(AgendaBo.class, agendaIdKey); |
| 77 | 2 | if (existing == null) { |
| 78 | 1 | throw new IllegalStateException("the agenda does not exist: " + agenda); |
| 79 | |
} |
| 80 | |
final AgendaDefinition toUpdate; |
| 81 | 1 | if (existing.getId().equals(agenda.getId())) { |
| 82 | 1 | toUpdate = agenda; |
| 83 | |
} else { |
| 84 | |
|
| 85 | 0 | final AgendaDefinition.Builder builder = AgendaDefinition.Builder.create(agenda); |
| 86 | 0 | builder.setId(existing.getId()); |
| 87 | 0 | toUpdate = builder.build(); |
| 88 | |
} |
| 89 | |
|
| 90 | |
|
| 91 | 1 | AgendaBo boToUpdate = from(toUpdate); |
| 92 | |
|
| 93 | |
|
| 94 | 1 | Map<String,String> fields = new HashMap<String,String>(1); |
| 95 | 1 | fields.put(PropertyNames.Agenda.AGENDA_ID, toUpdate.getId()); |
| 96 | 1 | businessObjectService.deleteMatching(AgendaAttributeBo.class, fields); |
| 97 | |
|
| 98 | |
|
| 99 | 1 | businessObjectService.save(boToUpdate); |
| 100 | 1 | } |
| 101 | |
|
| 102 | |
|
| 103 | |
|
| 104 | |
|
| 105 | |
@Override |
| 106 | |
public AgendaDefinition getAgendaByAgendaId(String agendaId) { |
| 107 | 4 | if (StringUtils.isBlank(agendaId)){ |
| 108 | 2 | throw new IllegalArgumentException("agenda id is null"); |
| 109 | |
} |
| 110 | 2 | AgendaBo bo = businessObjectService.findBySinglePrimaryKey(AgendaBo.class, agendaId); |
| 111 | 2 | return to(bo); |
| 112 | |
} |
| 113 | |
|
| 114 | |
|
| 115 | |
|
| 116 | |
|
| 117 | |
@Override |
| 118 | |
public AgendaDefinition getAgendaByNameAndContextId(String name, String contextId) { |
| 119 | 8 | if (StringUtils.isBlank(name)) { |
| 120 | 2 | throw new IllegalArgumentException("name is blank"); |
| 121 | |
} |
| 122 | 6 | if (StringUtils.isBlank(contextId)) { |
| 123 | 2 | throw new IllegalArgumentException("contextId is blank"); |
| 124 | |
} |
| 125 | |
|
| 126 | 4 | final Map<String, Object> map = new HashMap<String, Object>(); |
| 127 | 4 | map.put("name", name); |
| 128 | 4 | map.put("contextId", contextId); |
| 129 | |
|
| 130 | 4 | AgendaBo myAgenda = businessObjectService.findByPrimaryKey(AgendaBo.class, Collections.unmodifiableMap(map)); |
| 131 | 4 | return to(myAgenda); |
| 132 | |
} |
| 133 | |
|
| 134 | |
|
| 135 | |
|
| 136 | |
|
| 137 | |
@Override |
| 138 | |
public Set<AgendaDefinition> getAgendasByContextId(String contextId) { |
| 139 | 4 | if (StringUtils.isBlank(contextId)){ |
| 140 | 2 | throw new IllegalArgumentException("context ID is null or blank"); |
| 141 | |
} |
| 142 | 2 | final Map<String, Object> map = new HashMap<String, Object>(); |
| 143 | 2 | map.put("contextId", contextId); |
| 144 | 2 | Set<AgendaBo> bos = (Set<AgendaBo>) businessObjectService.findMatching(AgendaBo.class, map); |
| 145 | 2 | return convertListOfBosToImmutables(bos); |
| 146 | |
} |
| 147 | |
|
| 148 | |
|
| 149 | |
|
| 150 | |
|
| 151 | |
@Override |
| 152 | |
public AgendaItem createAgendaItem(AgendaItem agendaItem) { |
| 153 | 0 | if (agendaItem == null){ |
| 154 | 0 | throw new IllegalArgumentException("agendaItem is null"); |
| 155 | |
} |
| 156 | 0 | if (agendaItem.getId() != null){ |
| 157 | 0 | final AgendaDefinition existing = getAgendaByAgendaId(agendaItem.getId()); |
| 158 | 0 | if (existing != null){ |
| 159 | 0 | throw new IllegalStateException("the agendaItem to create already exists: " + agendaItem); |
| 160 | |
} |
| 161 | |
} |
| 162 | |
|
| 163 | 0 | AgendaItemBo bo = AgendaItemBo.from(agendaItem); |
| 164 | 0 | businessObjectService.save(bo); |
| 165 | 0 | return AgendaItemBo.to(bo); |
| 166 | |
} |
| 167 | |
|
| 168 | |
|
| 169 | |
|
| 170 | |
|
| 171 | |
@Override |
| 172 | |
public void updateAgendaItem(AgendaItem agendaItem) { |
| 173 | 0 | if (agendaItem == null){ |
| 174 | 0 | throw new IllegalArgumentException("agendaItem is null"); |
| 175 | |
} |
| 176 | 0 | final String agendaItemIdKey = agendaItem.getId(); |
| 177 | 0 | final AgendaItem existing = getAgendaItemById(agendaItemIdKey); |
| 178 | 0 | if (existing == null) { |
| 179 | 0 | throw new IllegalStateException("the agenda item does not exist: " + agendaItem); |
| 180 | |
} |
| 181 | |
final AgendaItem toUpdate; |
| 182 | 0 | if (existing.getId().equals(agendaItem.getId())) { |
| 183 | 0 | toUpdate = agendaItem; |
| 184 | |
} else { |
| 185 | 0 | final AgendaItem.Builder builder = AgendaItem.Builder.create(agendaItem); |
| 186 | 0 | builder.setId(existing.getId()); |
| 187 | 0 | toUpdate = builder.build(); |
| 188 | |
} |
| 189 | |
|
| 190 | 0 | businessObjectService.save(AgendaItemBo.from(toUpdate)); |
| 191 | 0 | } |
| 192 | |
|
| 193 | |
|
| 194 | |
|
| 195 | |
|
| 196 | |
@Override |
| 197 | |
public void addAgendaItem(AgendaItem agendaItem, String parentId, Boolean position) { |
| 198 | 0 | if (agendaItem == null){ |
| 199 | 0 | throw new IllegalArgumentException("agendaItem is null"); |
| 200 | |
} |
| 201 | 0 | AgendaItem parent = null; |
| 202 | 0 | if (parentId != null){ |
| 203 | 0 | parent = getAgendaItemById(parentId); |
| 204 | 0 | if (parent == null){ |
| 205 | 0 | throw new IllegalStateException("parent agendaItem does not exist in repository. parentId = " + parentId); |
| 206 | |
} |
| 207 | |
} |
| 208 | |
|
| 209 | |
final AgendaItem toCreate; |
| 210 | 0 | if (agendaItem.getId() == null) { |
| 211 | 0 | SequenceAccessorService sas = getSequenceAccessorService(); |
| 212 | 0 | final AgendaItem.Builder builder = AgendaItem.Builder.create(agendaItem); |
| 213 | 0 | final String newId =sas.getNextAvailableSequenceNumber( |
| 214 | |
"KRMS_AGENDA_ITM_S", AgendaItemBo.class).toString(); |
| 215 | 0 | builder.setId(newId); |
| 216 | 0 | toCreate = builder.build(); |
| 217 | 0 | } else { |
| 218 | 0 | toCreate = agendaItem; |
| 219 | |
} |
| 220 | 0 | createAgendaItem(toCreate); |
| 221 | |
|
| 222 | |
|
| 223 | 0 | if (parentId != null) { |
| 224 | 0 | final AgendaItem.Builder builder = AgendaItem.Builder.create(parent); |
| 225 | 0 | if (position == null){ |
| 226 | 0 | builder.setAlwaysId( toCreate.getId() ); |
| 227 | 0 | } else if (position.booleanValue()){ |
| 228 | 0 | builder.setWhenTrueId( toCreate.getId() ); |
| 229 | 0 | } else if (!position.booleanValue()){ |
| 230 | 0 | builder.setWhenFalseId( toCreate.getId() ); |
| 231 | |
} |
| 232 | 0 | final AgendaItem parentToUpdate = builder.build(); |
| 233 | 0 | updateAgendaItem( parentToUpdate ); |
| 234 | |
} |
| 235 | 0 | } |
| 236 | |
|
| 237 | |
|
| 238 | |
|
| 239 | |
|
| 240 | |
@Override |
| 241 | |
public AgendaItem getAgendaItemById(String id) { |
| 242 | 0 | if (StringUtils.isBlank(id)){ |
| 243 | 0 | throw new IllegalArgumentException("agenda item id is null"); |
| 244 | |
} |
| 245 | 0 | AgendaItemBo bo = businessObjectService.findBySinglePrimaryKey(AgendaItemBo.class, id); |
| 246 | 0 | return AgendaItemBo.to(bo); |
| 247 | |
} |
| 248 | |
|
| 249 | |
|
| 250 | |
|
| 251 | |
|
| 252 | |
|
| 253 | |
|
| 254 | |
public void setBusinessObjectService(final BusinessObjectService businessObjectService) { |
| 255 | 12 | this.businessObjectService = businessObjectService; |
| 256 | 12 | } |
| 257 | |
|
| 258 | |
protected BusinessObjectService getBusinessObjectService() { |
| 259 | 2 | if ( businessObjectService == null ) { |
| 260 | 0 | businessObjectService = KRADServiceLocator.getBusinessObjectService(); |
| 261 | |
} |
| 262 | 2 | return businessObjectService; |
| 263 | |
} |
| 264 | |
|
| 265 | |
|
| 266 | |
|
| 267 | |
|
| 268 | |
|
| 269 | |
|
| 270 | |
public void setSequenceAccessorService(final SequenceAccessorService sequenceAccessorService) { |
| 271 | 0 | this.sequenceAccessorService = sequenceAccessorService; |
| 272 | 0 | } |
| 273 | |
|
| 274 | |
protected SequenceAccessorService getSequenceAccessorService() { |
| 275 | 0 | if ( sequenceAccessorService == null ) { |
| 276 | 0 | sequenceAccessorService = KRADServiceLocator.getSequenceAccessorService(); |
| 277 | |
} |
| 278 | 0 | return sequenceAccessorService; |
| 279 | |
} |
| 280 | |
|
| 281 | |
protected KrmsAttributeDefinitionService getAttributeDefinitionService() { |
| 282 | 2 | if (attributeDefinitionService == null) { |
| 283 | 0 | attributeDefinitionService = KrmsRepositoryServiceLocator.getKrmsAttributeDefinitionService(); |
| 284 | |
} |
| 285 | 2 | return attributeDefinitionService; |
| 286 | |
} |
| 287 | |
|
| 288 | |
public void setAttributeDefinitionService(KrmsAttributeDefinitionService attributeDefinitionService) { |
| 289 | 2 | this.attributeDefinitionService = attributeDefinitionService; |
| 290 | 2 | } |
| 291 | |
|
| 292 | |
|
| 293 | |
|
| 294 | |
|
| 295 | |
|
| 296 | |
|
| 297 | |
|
| 298 | |
public Set<AgendaDefinition> convertListOfBosToImmutables(final Collection<AgendaBo> agendaBos) { |
| 299 | 2 | Set<AgendaDefinition> agendas = new HashSet<AgendaDefinition>(); |
| 300 | 2 | if (agendaBos != null){ |
| 301 | 1 | for (AgendaBo bo : agendaBos) { |
| 302 | 1 | AgendaDefinition agenda = to(bo); |
| 303 | 1 | agendas.add(agenda); |
| 304 | 1 | } |
| 305 | |
} |
| 306 | 2 | return Collections.unmodifiableSet(agendas); |
| 307 | |
} |
| 308 | |
|
| 309 | |
|
| 310 | |
|
| 311 | |
|
| 312 | |
|
| 313 | |
|
| 314 | |
@Override |
| 315 | |
public AgendaDefinition to(AgendaBo bo) { |
| 316 | 11 | if (bo == null) { return null; } |
| 317 | 8 | return org.kuali.rice.krms.api.repository.agenda.AgendaDefinition.Builder.create(bo).build(); |
| 318 | |
} |
| 319 | |
|
| 320 | |
|
| 321 | |
|
| 322 | |
|
| 323 | |
|
| 324 | |
|
| 325 | |
|
| 326 | |
@Override |
| 327 | |
public AgendaBo from(AgendaDefinition im) { |
| 328 | 2 | if (im == null) { return null; } |
| 329 | |
|
| 330 | 2 | AgendaBo bo = new AgendaBo(); |
| 331 | 2 | bo.setId(im.getId()); |
| 332 | 2 | bo.setName( im.getName() ); |
| 333 | 2 | bo.setTypeId( im.getTypeId() ); |
| 334 | 2 | bo.setContextId( im.getContextId() ); |
| 335 | 2 | bo.setFirstItemId( im.getFirstItemId() ); |
| 336 | 2 | bo.setVersionNumber( im.getVersionNumber() ); |
| 337 | 2 | Set<AgendaAttributeBo> attributes = buildAgendaAttributeBo(im); |
| 338 | |
|
| 339 | 2 | bo.setAttributeBos(attributes); |
| 340 | |
|
| 341 | 2 | return bo; |
| 342 | |
} |
| 343 | |
|
| 344 | |
private Set<AgendaAttributeBo> buildAgendaAttributeBo(AgendaDefinition im) { |
| 345 | 2 | Set<AgendaAttributeBo> attributes = new HashSet<AgendaAttributeBo>(); |
| 346 | |
|
| 347 | 2 | ContextBo context = getBusinessObjectService().findBySinglePrimaryKey(ContextBo.class, im.getContextId()); |
| 348 | |
|
| 349 | |
|
| 350 | 2 | Map<String, KrmsAttributeDefinition> attributeDefinitionMap = new HashMap<String, KrmsAttributeDefinition>(); |
| 351 | |
|
| 352 | 2 | List<KrmsAttributeDefinition> attributeDefinitions = |
| 353 | |
getAttributeDefinitionService().findAttributeDefinitionsByType(im.getTypeId()); |
| 354 | |
|
| 355 | 2 | for (KrmsAttributeDefinition attributeDefinition : attributeDefinitions) { |
| 356 | 4 | attributeDefinitionMap.put(attributeDefinition.getName(), attributeDefinition); |
| 357 | |
} |
| 358 | |
|
| 359 | |
|
| 360 | 2 | for (Entry<String,String> entry : im.getAttributes().entrySet()){ |
| 361 | 4 | KrmsAttributeDefinition attrDef = attributeDefinitionMap.get(entry.getKey()); |
| 362 | |
|
| 363 | 4 | if (attrDef != null) { |
| 364 | 4 | AgendaAttributeBo attributeBo = new AgendaAttributeBo(); |
| 365 | 4 | attributeBo.setAgendaId( im.getId() ); |
| 366 | 4 | attributeBo.setAttributeDefinitionId(attrDef.getId()); |
| 367 | 4 | attributeBo.setValue(entry.getValue()); |
| 368 | 4 | attributeBo.setAttributeDefinition(KrmsAttributeDefinitionBo.from(attrDef)); |
| 369 | 4 | attributes.add( attributeBo ); |
| 370 | 4 | } else { |
| 371 | 0 | throw new RiceIllegalStateException("there is no attribute definition with the name '" + |
| 372 | |
entry.getKey() + "' that is valid for the agenda type with id = '" + im.getTypeId() +"'"); |
| 373 | |
} |
| 374 | 4 | } |
| 375 | 2 | return attributes; |
| 376 | |
} |
| 377 | |
|
| 378 | |
} |