Program: 172083
Project: 172083
Buggy source:
private void setAspectProperties(NodeRef nodeRef,boolean isNameChanging,CmisExtensionElement aspectExtension){
  if (aspectExtension.getChildren() == null) {
    return;
  }
  List<String> aspectsToAdd=new ArrayList<String>();
  List<String> aspectsToRemove=new ArrayList<String>();
  Map<QName,List<Serializable>> aspectProperties=new HashMap<QName,List<Serializable>>();
  for (  CmisExtensionElement extension : aspectExtension.getChildren()) {
    if (!ALFRESCO_EXTENSION_NAMESPACE.equals(extension.getNamespace())) {
      continue;
    }
    if (ASPECTS_TO_ADD.equals(extension.getName()) && (extension.getValue() != null)) {
      aspectsToAdd.add(extension.getValue());
    }
 else     if (ASPECTS_TO_REMOVE.equals(extension.getName()) && (extension.getValue() != null)) {
      aspectsToRemove.add(extension.getValue());
    }
 else     if (PROPERTIES.equals(extension.getName()) && (extension.getChildren() != null)) {
      for (      CmisExtensionElement property : extension.getChildren()) {
        if (!property.getName().startsWith("String_Node_Str")) {
          continue;
        }
        String propertyId=(property.getAttributes() == null ? null : property.getAttributes().get("String_Node_Str"));
        if ((propertyId == null) || (property.getChildren() == null)) {
          continue;
        }
        PropertyType propertyType=PropertyType.STRING;
        DatatypeFactory df=null;
        if (property.getName().equals("String_Node_Str")) {
          propertyType=PropertyType.BOOLEAN;
        }
 else         if (property.getName().equals("String_Node_Str")) {
          propertyType=PropertyType.INTEGER;
        }
 else         if (property.getName().equals("String_Node_Str")) {
          propertyType=PropertyType.DATETIME;
          try {
            df=DatatypeFactory.newInstance();
          }
 catch (          DatatypeConfigurationException e) {
            throw new CmisRuntimeException("String_Node_Str" + e.getMessage(),e);
          }
        }
 else         if (property.getName().equals("String_Node_Str")) {
          propertyType=PropertyType.DECIMAL;
        }
        ArrayList<Serializable> values=new ArrayList<Serializable>();
        if (property.getChildren() != null) {
          try {
            for (            CmisExtensionElement valueElement : property.getChildren()) {
              if ("String_Node_Str".equals(valueElement.getName())) {
switch (propertyType) {
case BOOLEAN:
                  values.add(Boolean.parseBoolean(valueElement.getValue()));
                break;
case DATETIME:
              values.add(df.newXMLGregorianCalendar(valueElement.getValue()).toGregorianCalendar());
            break;
case INTEGER:
          values.add(new BigInteger(valueElement.getValue()));
        break;
case DECIMAL:
      values.add(new BigDecimal(valueElement.getValue()));
    break;
default :
  values.add(valueElement.getValue());
}
}
}
}
 catch (Exception e) {
throw new CmisInvalidArgumentException("String_Node_Str" + propertyId,e);
}
}
aspectProperties.put(QName.createQName(propertyId,namespaceService),values);
}
}
}
String aspectType=null;
try {
for (String aspect : aspectsToRemove) {
aspectType=aspect;
TypeDefinitionWrapper type=getType(aspect);
if (type == null) {
throw new CmisInvalidArgumentException("String_Node_Str" + aspectType);
}
QName typeName=type.getAlfrescoName();
if (typeName.equals(ContentModel.ASPECT_HIDDEN)) {
if (hiddenAspect.isClientControlled(nodeRef) || aspectProperties.containsKey(ContentModel.PROP_CLIENT_CONTROLLED)) {
nodeService.removeAspect(nodeRef,typeName);
}
}
 else {
nodeService.removeAspect(nodeRef,typeName);
}
}
for (String aspect : aspectsToAdd) {
aspectType=aspect;
TypeDefinitionWrapper type=getType(aspect);
if (type == null) {
throw new CmisInvalidArgumentException("String_Node_Str" + aspectType);
}
QName typeName=type.getAlfrescoName();
if (typeName.equals(ContentModel.ASPECT_HIDDEN)) {
if (hiddenAspect.isClientControlled(nodeRef) || aspectProperties.containsKey(ContentModel.PROP_CLIENT_CONTROLLED)) {
nodeService.addAspect(nodeRef,type.getAlfrescoName(),Collections.<QName,Serializable>emptyMap());
}
}
 else {
nodeService.addAspect(nodeRef,type.getAlfrescoName(),Collections.<QName,Serializable>emptyMap());
}
}
}
 catch (InvalidAspectException e) {
throw new CmisInvalidArgumentException("String_Node_Str" + aspectType);
}
catch (InvalidNodeRefException e) {
throw new CmisInvalidArgumentException("String_Node_Str" + nodeRef);
}
for (Map.Entry<QName,List<Serializable>> property : aspectProperties.entrySet()) {
if (property.getValue().isEmpty()) {
if (HiddenAspect.HIDDEN_PROPERTIES.contains(property.getKey())) {
if (hiddenAspect.isClientControlled(nodeRef) || aspectProperties.containsKey(ContentModel.PROP_CLIENT_CONTROLLED)) {
nodeService.removeProperty(nodeRef,property.getKey());
}
}
 else {
nodeService.removeProperty(nodeRef,property.getKey());
}
}
 else {
if (HiddenAspect.HIDDEN_PROPERTIES.contains(property.getKey())) {
if (hiddenAspect.isClientControlled(nodeRef) || aspectProperties.containsKey(ContentModel.PROP_CLIENT_CONTROLLED)) {
nodeService.setProperty(nodeRef,property.getKey(),property.getValue().size() == 1 ? property.getValue().get(0) : (Serializable)property.getValue());
}
}
 else {
nodeService.setProperty(nodeRef,property.getKey(),property.getValue().size() == 1 ? property.getValue().get(0) : (Serializable)property.getValue());
}
}
}
}

Fixed version (ground truth):
private void setAspectProperties(NodeRef nodeRef,boolean isNameChanging,CmisExtensionElement aspectExtension){
  if (aspectExtension.getChildren() == null) {
    return;
  }
  List<String> aspectsToAdd=new ArrayList<String>();
  List<String> aspectsToRemove=new ArrayList<String>();
  Map<QName,List<Serializable>> aspectProperties=new HashMap<QName,List<Serializable>>();
  for (  CmisExtensionElement extension : aspectExtension.getChildren()) {
    if (!ALFRESCO_EXTENSION_NAMESPACE.equals(extension.getNamespace())) {
      continue;
    }
    if (ASPECTS_TO_ADD.equals(extension.getName()) && (extension.getValue() != null)) {
      aspectsToAdd.add(extension.getValue());
    }
 else     if (ASPECTS_TO_REMOVE.equals(extension.getName()) && (extension.getValue() != null)) {
      aspectsToRemove.add(extension.getValue());
    }
 else     if (PROPERTIES.equals(extension.getName()) && (extension.getChildren() != null)) {
      for (      CmisExtensionElement property : extension.getChildren()) {
        if (!property.getName().startsWith("String_Node_Str")) {
          continue;
        }
        String propertyId=(property.getAttributes() == null ? null : property.getAttributes().get("String_Node_Str"));
        if ((propertyId == null) || (property.getChildren() == null)) {
          continue;
        }
        PropertyType propertyType=PropertyType.STRING;
        DatatypeFactory df=null;
        if (property.getName().equals("String_Node_Str")) {
          propertyType=PropertyType.BOOLEAN;
        }
 else         if (property.getName().equals("String_Node_Str")) {
          propertyType=PropertyType.INTEGER;
        }
 else         if (property.getName().equals("String_Node_Str")) {
          propertyType=PropertyType.DATETIME;
          try {
            df=DatatypeFactory.newInstance();
          }
 catch (          DatatypeConfigurationException e) {
            throw new CmisRuntimeException("String_Node_Str" + e.getMessage(),e);
          }
        }
 else         if (property.getName().equals("String_Node_Str")) {
          propertyType=PropertyType.DECIMAL;
        }
        ArrayList<Serializable> values=new ArrayList<Serializable>();
        if (property.getChildren() != null) {
          for (          CmisExtensionElement valueElement : property.getChildren()) {
            if ("String_Node_Str".equals(valueElement.getName())) {
switch (propertyType) {
case BOOLEAN:
                try {
                  values.add(Boolean.parseBoolean(valueElement.getValue()));
                }
 catch (                Exception e) {
                  throw new CmisInvalidArgumentException("String_Node_Str" + propertyId,e);
                }
              break;
case DATETIME:
            try {
              values.add(df.newXMLGregorianCalendar(valueElement.getValue()).toGregorianCalendar());
            }
 catch (            Exception e) {
              throw new CmisInvalidArgumentException("String_Node_Str" + propertyId,e);
            }
          break;
case INTEGER:
        BigInteger value=null;
      try {
        value=new BigInteger(valueElement.getValue());
      }
 catch (      Exception e) {
        throw new CmisInvalidArgumentException("String_Node_Str" + propertyId,e);
      }
    PropertyDefinitionWrapper propDef=cmisDictionaryService.findProperty(propertyId);
  if (propDef == null) {
    throw new CmisInvalidArgumentException("String_Node_Str" + propertyId + "String_Node_Str");
  }
QName propertyQName=propDef.getPropertyAccessor().getMappedProperty();
if (propertyQName == null) {
throw new CmisConstraintException("String_Node_Str" + propertyId + "String_Node_Str");
}
org.alfresco.service.cmr.dictionary.PropertyDefinition def=dictionaryService.getProperty(propertyQName);
QName dataDef=def.getDataType().getName();
if (dataDef.equals(DataTypeDefinition.INT) && (value.compareTo(maxInt) > 0 || value.compareTo(minInt) < 0)) {
throw new CmisConstraintException("String_Node_Str" + propertyId);
}
if (dataDef.equals(DataTypeDefinition.LONG) && (value.compareTo(maxLong) > 0 || value.compareTo(minLong) < 0)) {
throw new CmisConstraintException("String_Node_Str" + propertyId);
}
values.add(value);
break;
case DECIMAL:
try {
values.add(new BigDecimal(valueElement.getValue()));
}
 catch (Exception e) {
throw new CmisInvalidArgumentException("String_Node_Str" + propertyId,e);
}
break;
default :
values.add(valueElement.getValue());
}
}
}
}
aspectProperties.put(QName.createQName(propertyId,namespaceService),values);
}
}
}
String aspectType=null;
try {
for (String aspect : aspectsToRemove) {
aspectType=aspect;
TypeDefinitionWrapper type=getType(aspect);
if (type == null) {
throw new CmisInvalidArgumentException("String_Node_Str" + aspectType);
}
QName typeName=type.getAlfrescoName();
if (typeName.equals(ContentModel.ASPECT_HIDDEN)) {
if (hiddenAspect.isClientControlled(nodeRef) || aspectProperties.containsKey(ContentModel.PROP_CLIENT_CONTROLLED)) {
nodeService.removeAspect(nodeRef,typeName);
}
}
 else {
nodeService.removeAspect(nodeRef,typeName);
}
}
for (String aspect : aspectsToAdd) {
aspectType=aspect;
TypeDefinitionWrapper type=getType(aspect);
if (type == null) {
throw new CmisInvalidArgumentException("String_Node_Str" + aspectType);
}
QName typeName=type.getAlfrescoName();
if (typeName.equals(ContentModel.ASPECT_HIDDEN)) {
if (hiddenAspect.isClientControlled(nodeRef) || aspectProperties.containsKey(ContentModel.PROP_CLIENT_CONTROLLED)) {
nodeService.addAspect(nodeRef,type.getAlfrescoName(),Collections.<QName,Serializable>emptyMap());
}
}
 else {
nodeService.addAspect(nodeRef,type.getAlfrescoName(),Collections.<QName,Serializable>emptyMap());
}
}
}
 catch (InvalidAspectException e) {
throw new CmisInvalidArgumentException("String_Node_Str" + aspectType);
}
catch (InvalidNodeRefException e) {
throw new CmisInvalidArgumentException("String_Node_Str" + nodeRef);
}
for (Map.Entry<QName,List<Serializable>> property : aspectProperties.entrySet()) {
QName propertyQName=property.getKey();
if (property.getValue().isEmpty()) {
if (HiddenAspect.HIDDEN_PROPERTIES.contains(property.getKey())) {
if (hiddenAspect.isClientControlled(nodeRef) || aspectProperties.containsKey(ContentModel.PROP_CLIENT_CONTROLLED)) {
nodeService.removeProperty(nodeRef,propertyQName);
}
}
 else {
nodeService.removeProperty(nodeRef,property.getKey());
}
}
 else {
if (HiddenAspect.HIDDEN_PROPERTIES.contains(property.getKey())) {
if (hiddenAspect.isClientControlled(nodeRef) || aspectProperties.containsKey(ContentModel.PROP_CLIENT_CONTROLLED)) {
nodeService.setProperty(nodeRef,property.getKey(),property.getValue().size() == 1 ? property.getValue().get(0) : (Serializable)property.getValue());
}
}
 else {
Serializable value=(Serializable)property.getValue();
nodeService.setProperty(nodeRef,property.getKey(),property.getValue().size() == 1 ? property.getValue().get(0) : value);
}
}
}
}