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