cancel
Showing results for 
Search instead for 
Did you mean: 

Adding an aspect is not working for me

cpawinsal
Champ in-the-making
Champ in-the-making
Hi,

I am having no problem adding aspects to content at the time I create it. However, I have a method that should allow aspects to be added to types after the type has been created. But, it's not. Please help me understand what I'm doing wrong:


   public void setAspect(String uuid, Aspecter aspect) throws Exception{
      String aspectName = aspect.getAspectName();      
      Map<String, List<String>> propMap = aspect.getAspectMap();
      Reference ref = new Reference(new Store(Constants.WORKSPACE_STORE, "SpacesStore"), uuid,
            getFilePath());
      Predicate pred = new Predicate(new Reference[]{ref}, null, null);
      NamedValue[] nv = new NamedValue[propMap.size()];
      Set s = propMap.keySet();
      Iterator<String> it = s.iterator();
      int pos = 0;
      while(it.hasNext()) {
         String propName = it.next();
         List<String> propValues = propMap.get(propName);         
         NamedValue named = new NamedValue();
         String[] values = new String[propValues.size()];
         for(int i = 0; i < propValues.size(); i++) {            
            values[i] = propValues.get(i);            
         }
         named.setName(Constants.createQNameString(nameSpace, propName));
         named.setValues(values);
         nv[pos] = named;
         pos = pos + 1;
      }
      CMLAddAspect aspects = new CMLAddAspect(Constants.createQNameString(
            nameSpace, aspectName), nv, pred, null);            
      CML cml = new CML();      
      cml.setAddAspect(new CMLAddAspect[]{aspects});      
      WebServiceFactory.getRepositoryService().update(cml);
   }

When I run this method, I get no complaints from the compiler… but I also don't get any aspects added.

Thanks for your help.
1 REPLY 1

cpawinsal
Champ in-the-making
Champ in-the-making
Finally fixed it. Was a problem with setting my named values (tried following the API and using a String[] to set my props and the qName to set my prop Strings but clearly I was not doing something right). When I modified the code as follows, it all works…


   public void setAspect(String uuid, Aspecter aspect) throws Exception{
      String aspectName = aspect.getAspectName();      
      Map<String, List<String>> propMap = aspect.getAspectMap();
      Reference ref = new Reference(new Store(Constants.WORKSPACE_STORE, "SpacesStore"), uuid,
            getFilePath());
      Predicate pred = new Predicate(new Reference[]{ref}, null, null);      
      Set s = propMap.keySet();
      Iterator<String> it = s.iterator();
      List<NamedValue> valList = new ArrayList<NamedValue>();
      while(it.hasNext()) {
         String propName = it.next();
         List<String> propValues = propMap.get(propName);         
         for(int i = 0; i < propValues.size(); i++) {            
            NamedValue n = Utils.createNamedValue(
                     Constants.createQNameString(getNameSpace(), propName), propValues.get(i));
            valList.add(n);
         }         
      }
      NamedValue[] nv = new NamedValue[valList.size()];
      nv = valList.toArray(nv);
      CMLAddAspect aspects = new CMLAddAspect(Constants.createQNameString(
            nameSpace, aspectName), nv, pred, null);      
      CML cml = new CML();   
      cml.setAddAspect(new CMLAddAspect[]{aspects});
      //cml.setAddAspect(new CMLAddAspect[]{aspects});      
      WebServiceFactory.getRepositoryService().update(cml);
   }
   

Sorry for using up valuable forum space with an easy to solve problem. Smiley Happy I'll leave all this in the off chance this helps someone else.