Exemplo n.º 1
0
  /**
   * Get a map of the sites custom properties
   *
   * @return map of names and values
   */
  public ScriptableQNameMap<String, CustomProperty> getCustomProperties() {
    if (this.customProperties == null) {
      // create the custom properties map
      ScriptNode siteNode = new ScriptNode(this.siteInfo.getNodeRef(), this.serviceRegistry);
      // set the scope, for use when converting props to javascript objects
      siteNode.setScope(scope);
      this.customProperties =
          new ContentAwareScriptableQNameMap<String, CustomProperty>(
              siteNode, this.serviceRegistry);

      Map<QName, Serializable> props = siteInfo.getCustomProperties();
      for (QName qname : props.keySet()) {
        // get the property value
        Serializable propValue = props.get(qname);

        // convert the value
        NodeValueConverter valueConverter = siteNode.new NodeValueConverter();
        Serializable value = valueConverter.convertValueForScript(qname, propValue);

        // get the type and label information from the dictionary
        String title = null;
        String type = null;
        PropertyDefinition propDef = this.serviceRegistry.getDictionaryService().getProperty(qname);
        if (propDef != null) {
          type = propDef.getDataType().getName().toString();
          title = propDef.getTitle(this.serviceRegistry.getDictionaryService());
        }

        // create the custom property and add to the map
        CustomProperty customProp = new CustomProperty(qname.toString(), value, type, title);
        this.customProperties.put(qname.toString(), customProp);
      }
    }
    return this.customProperties;
  }
  public Map<PropertyDefinition, Object> generateDatasProperties(
      ClassDefinition type, Collection<PropertyDefinition> properties) throws Exception {
    Map<PropertyDefinition, Object> datasProperties = new HashMap<PropertyDefinition, Object>();

    Collection<PropertyDefinition> unicityProperties =
        generatorServices.getUnicityProperties(type, properties);

    Collection<PropertyDefinition> tempProperties = new ArrayList<PropertyDefinition>();
    tempProperties.addAll(properties);
    tempProperties.removeAll(unicityProperties);
    for (PropertyDefinition propertyDefinition : tempProperties) {
      if (!propertyDefinition.getName().getNamespaceURI().contains("alfresco")) {
        startAttributeIndex = 0;
        datasProperties.put(propertyDefinition, generateDatasProperty(propertyDefinition));
      }
    }

    if (unicityProperties.size() > 0) {
      startAttributeIndex = savedStartIndexAttribute.intValue();
      Map<PropertyDefinition, Object> dataUnicityProperties =
          generateDataForUnicityProperties(type, unicityProperties);
      if (dataUnicityProperties.size() > 0) {
        datasProperties.putAll(dataUnicityProperties);
      }
    }

    return datasProperties;
  }
 static String generateRandomString(String defaultValue, PropertyDefinition property) {
   String data = null;
   if (scenario.equals("random")) {
     if (defaultValue != null) {
       data = defaultValue + "_" + Integer.valueOf(randomGenerator.nextInt()).toString();
     } else {
       String[] parts = property.getName().toString().split("_");
       String propertyName = parts[parts.length - 1];
       data =
           propertyName.substring(0, propertyName.length() / 2)
               + "_"
               + Integer.valueOf(randomGenerator.nextInt()).toString();
     }
   } else if (scenario.equals("incremental")) {
     if (defaultValue != null) {
       data = defaultValue + "_" + Integer.valueOf(index.get(typeRef).get(property)).toString();
     } else {
       String[] parts = property.getName().toString().split("_");
       String propertyName = parts[parts.length - 1];
       data =
           propertyName.substring(0, propertyName.length() / 2)
               + "_"
               + Integer.valueOf(index.get(typeRef).get(property)).toString();
     }
   }
   return data;
 }
 private Map<PropertyDefinition, Object> generateDataForUnicityProperties(
     ClassDefinition type, Collection<PropertyDefinition> unicityProperties) throws Exception {
   Collection<Serial> dataProperties = new ArrayList<Serial>();
   boolean same = true;
   while (same) {
     dataProperties.clear();
     for (PropertyDefinition propertyDefinition : unicityProperties) {
       dataProperties.add(
           new Serial(
               type.getName().toString(),
               propertyDefinition.getName().toString(),
               generateDatasProperty(propertyDefinition)));
     }
     if (serializedData.size() > 0 && unicityProperties.size() > 0) {
       for (Serial dataProperty : dataProperties) {
         boolean contains = false;
         for (Serial serializedDataProperty : serializedData) {
           if (dataProperty.getType().equals(serializedDataProperty.getType())
               && dataProperty.getProperty().equals(serializedDataProperty.getProperty())
               && dataProperty
                   .getData()
                   .toString()
                   .equals(serializedDataProperty.getData().toString())) {
             contains = true;
           }
         }
         same &= contains;
       }
     } else {
       same = false;
     }
   }
   Map<PropertyDefinition, Object> finalDataProperties = new HashMap<PropertyDefinition, Object>();
   for (Serial dataProperty : dataProperties) {
     PropertyDefinition property =
         service.getProperty(QName.createQName(dataProperty.getProperty()));
     finalDataProperties.put(property, dataProperty.getData());
   }
   return finalDataProperties;
 }
Exemplo n.º 5
0
  /* (non-Javadoc)
   * @see org.alfresco.service.cmr.view.Exporter#value(org.alfresco.service.cmr.repository.NodeRef, org.alfresco.service.namespace.QName, java.io.Serializable)
   */
  public void value(NodeRef nodeRef, QName property, Object value, int index) {
    try {
      // determine data type of value
      QName valueDataType = null;
      PropertyDefinition propDef = dictionaryService.getProperty(property);
      DataTypeDefinition dataTypeDef = (propDef == null) ? null : propDef.getDataType();
      if (dataTypeDef == null || dataTypeDef.getName().equals(DataTypeDefinition.ANY)) {
        dataTypeDef = (value == null) ? null : dictionaryService.getDataType(value.getClass());
        if (dataTypeDef != null) {
          valueDataType = dataTypeDef.getName();
        }
      }

      // convert node references to paths
      if (value instanceof NodeRef && referenceType.equals(ReferenceType.PATHREF)) {
        NodeRef valueNodeRef = (NodeRef) value;
        if (nodeRef.getStoreRef().equals(valueNodeRef.getStoreRef())) {
          Path nodeRefPath = createPath(context.getExportOf(), nodeRef, valueNodeRef);
          value = (nodeRefPath == null) ? null : nodeRefPath.toPrefixString(namespaceService);
        }
      }

      // output value wrapper if value is null or property data type is ANY or value is part of
      // collection
      if (value == null || valueDataType != null || index != -1) {
        AttributesImpl attrs = new AttributesImpl();
        if (value == null) {
          attrs.addAttribute(
              NamespaceService.REPOSITORY_VIEW_PREFIX,
              ISNULL_LOCALNAME,
              ISNULL_QNAME.toPrefixString(),
              null,
              "true");
        }
        if (valueDataType != null) {
          attrs.addAttribute(
              NamespaceService.REPOSITORY_VIEW_PREFIX,
              DATATYPE_LOCALNAME,
              DATATYPE_QNAME.toPrefixString(),
              null,
              toPrefixString(valueDataType));
        }
        contentHandler.startElement(
            NamespaceService.REPOSITORY_VIEW_PREFIX,
            VALUE_LOCALNAME,
            toPrefixString(VALUE_QNAME),
            attrs);
      }

      // output value
      String strValue = (String) DefaultTypeConverter.INSTANCE.convert(String.class, value);
      if (strValue != null) {
        for (int i = 0; i < strValue.length(); i++) {
          char[] temp = new char[] {strValue.charAt(i)};
          contentHandler.characters(temp, 0, 1);
        }
      }

      // output value wrapper if property data type is any
      if (value == null || valueDataType != null || index != -1) {
        contentHandler.endElement(
            NamespaceService.REPOSITORY_VIEW_PREFIX, VALUE_LOCALNAME, toPrefixString(VALUE_QNAME));
      }
    } catch (SAXException e) {
      throw new ExporterException(
          "Failed to process value event - nodeRef "
              + nodeRef
              + "; property "
              + toPrefixString(property)
              + "; value "
              + value,
          e);
    }
  }
Exemplo n.º 6
0
  /*
   *  (non-Javadoc)
   * @see org.xml.sax.ContentHandler#startElement(java.lang.String, java.lang.String, java.lang.String, org.xml.sax.Attributes)
   */
  public void startElement(String uri, String localName, String qName, Attributes atts)
      throws SAXException {
    try {
      // construct qname for element
      QName elementName = decodeQName(QName.createQName(qName, importResolver));

      // setup parent context
      ParentContext parentContext = null;
      if (contextStack.empty()) {
        // create root parent context
        parentContext = new ParentContext(elementName, dictionaryService, importer);
      } else {
        // create parent context
        NodeContext parentNode = (NodeContext) contextStack.peek();
        parentContext = new ParentContext(elementName, parentNode);
      }

      // create node context
      NodeContext node = new NodeContext(elementName, parentContext, null);
      node.setChildName(elementName.toPrefixString(importResolver));
      contextStack.push(node);

      // process node properties
      for (int i = 0; i < atts.getLength(); i++) {
        QName propName = decodeQName(QName.createQName(atts.getURI(i), atts.getLocalName(i)));
        String value = atts.getValue(i);

        //
        // process "well-known" properties
        //

        if (propName.equals(JCRPrimaryTypeProperty.PROPERTY_NAME)) {
          // primary type
          QName primaryTypeQName = QName.createQName(value, importResolver);
          TypeDefinition typeDef = dictionaryService.getType(primaryTypeQName);
          if (typeDef == null) {
            throw new InvalidTypeException(primaryTypeQName);
          }
          node.setTypeDefinition(typeDef);
        } else if (propName.equals(JCRMixinTypesProperty.PROPERTY_NAME)) {
          // aspects
          String[] aspects = value.split(" ");
          for (String aspect : aspects) {
            // ignore JCR specific aspects
            QName aspectQName = QName.createQName(aspect, importResolver);
            if (!(JCRNamespace.JCR_URI.equals(aspectQName.getNamespaceURI())
                || JCRNamespace.MIX_URI.equals(aspectQName.getNamespaceURI()))) {
              AspectDefinition aspectDef = dictionaryService.getAspect(aspectQName);
              if (aspectDef == null) {
                throw new InvalidTypeException(aspectQName);
              }
              node.addAspect(aspectDef);
            }
          }
        } else if (JCRUUIDProperty.PROPERTY_NAME.equals(propName)) {
          node.setUUID(value);
        }

        //
        // Note: ignore JCR specific properties
        //

        else if (JCRNamespace.JCR_URI.equals(propName.getNamespaceURI())) {
        }

        //
        // process all other properties
        //

        else {
          // determine type of property
          PropertyDefinition propDef = dictionaryService.getProperty(propName);
          if (propDef == null) {
            throw new ImporterException(
                "Property " + propName + " is not known to the repository data dictionary");
          }
          DataTypeDefinition dataTypeDef = propDef.getDataType();

          // extract values from node xml attribute
          String[] propValues = null;
          PropertyContext propertyContext =
              new PropertyContext(elementName, node, propName, dataTypeDef.getName());
          if (dataTypeDef.getName().equals(DataTypeDefinition.CONTENT)) {
            // Note: we only support single valued content properties
            propValues = new String[] {value};
          } else {
            // attempt to split multi-value properties
            propValues = value.split(" ");
          }

          // extract values appropriately
          for (String propValue : propValues) {
            propertyContext.startValue();
            propertyContext.appendCharacters(propValue.toCharArray(), 0, propValue.length());
            propertyContext.endValue();
          }

          // add each value to the node
          if (propertyContext.isMultiValue()) {
            node.addPropertyCollection(propName);
          }
          List<StringBuffer> nodeValues = propertyContext.getValues();
          for (StringBuffer nodeValue : nodeValues) {
            // first, cast value to appropriate type (using JCR converters)
            Serializable objVal =
                (Serializable)
                    session.getTypeConverter().convert(dataTypeDef, nodeValue.toString());
            String strValue = DefaultTypeConverter.INSTANCE.convert(String.class, objVal);
            node.addProperty(propName, strValue);
          }
        }
      }

      // import node
      NodeRef nodeRef = node.getImporter().importNode(node);
      node.setNodeRef(nodeRef);
    } catch (Exception e) {
      throw new SAXException("Failed to process element " + qName, e);
    }
  }
 private Object generateDatasProperty(PropertyDefinition propertyDefinition) throws Exception {
   if (scenario.equals("incremental")) {
     if (numOfSame.get(typeRef).get(propertyDefinition) == null) {
       Integer value =
           Integer.valueOf(
               RandomMethods.nextIntInInterval(
                   startAttributeIndex, startAttributeIndex + numOfTypes + 1));
       numOfSame.get(typeRef).put(propertyDefinition, value);
       index.get(typeRef).put(propertyDefinition, Integer.valueOf(startAttributeIndex));
     } else {
       Integer value =
           Integer.valueOf(numOfSame.get(typeRef).get(propertyDefinition).intValue() - 1);
       if (value.intValue() >= startAttributeIndex) {
         numOfSame.get(typeRef).put(propertyDefinition, value);
         Integer indexValue =
             Integer.valueOf(index.get(typeRef).get(propertyDefinition).intValue() + 1);
         index.get(typeRef).put(propertyDefinition, indexValue);
       } else {
         Integer newValue =
             Integer.valueOf(
                 RandomMethods.nextIntInInterval(
                     startAttributeIndex, startAttributeIndex + numOfTypes + 1));
         numOfSame.get(typeRef).put(propertyDefinition, newValue);
         index.get(typeRef).put(propertyDefinition, Integer.valueOf(startAttributeIndex));
       }
     }
   } else if (scenario.equals("random")) {
     numOfSame.get(typeRef).put(propertyDefinition, Integer.valueOf(0));
   }
   Object randomData = new Object();
   String defaultValue = propertyDefinition.getDefaultValue();
   QName dataTypeOfProperty = propertyDefinition.getDataType().getName();
   List<ConstraintDefinition> constraints = propertyDefinition.getConstraints();
   if (constraints.size()
       == 1) { // we suppose there is a one-to-one correspondence between property and enumeration
     for (ConstraintDefinition constraint : constraints) {
       randomData = RandomMethods.getRandomlyValue(constraint);
     }
   } else {
     if (numOfSame
         .get(typeRef)
         .get(propertyDefinition)
         .equals(Integer.valueOf(startAttributeIndex))) {
       randomData =
           RandomMethods.generateDataByDataTypeProperty(
               dataTypeOfProperty, defaultValue, propertyDefinition);
     } else if (!numOfSame
             .get(typeRef)
             .get(propertyDefinition)
             .equals(Integer.valueOf(startAttributeIndex))
         && sameData.get(typeRef).get(propertyDefinition) == null) {
       randomData =
           RandomMethods.generateDataByDataTypeProperty(
               dataTypeOfProperty, defaultValue, propertyDefinition);
       sameData.get(typeRef).put(propertyDefinition, randomData);
     } else if (!numOfSame
             .get(typeRef)
             .get(propertyDefinition)
             .equals(Integer.valueOf(startAttributeIndex))
         && sameData.get(typeRef).get(propertyDefinition) != null) {
       randomData = sameData.get(typeRef).get(propertyDefinition);
     }
   }
   return randomData;
 }
  private CategoryPaths getCategoryPaths(
      NodeRef nodeRef, Set<QName> aspects, Map<QName, Serializable> properties) {
    ArrayList<Pair<Path, QName>> categoryPaths = new ArrayList<Pair<Path, QName>>();
    ArrayList<ChildAssociationRef> categoryParents = new ArrayList<ChildAssociationRef>();

    nodeDAO.setCheckNodeConsistency();
    for (QName classRef : aspects) {
      AspectDefinition aspDef = dictionaryService.getAspect(classRef);
      if (!isCategorised(aspDef)) {
        continue;
      }
      LinkedList<Pair<Path, QName>> aspectPaths = new LinkedList<Pair<Path, QName>>();
      for (PropertyDefinition propDef : aspDef.getProperties().values()) {
        if (!propDef.getDataType().getName().equals(DataTypeDefinition.CATEGORY)) {
          // The property is not a category
          continue;
        }
        // Don't try to iterate if the property is null
        Serializable propVal = properties.get(propDef.getName());
        if (propVal == null) {
          continue;
        }
        for (NodeRef catRef : DefaultTypeConverter.INSTANCE.getCollection(NodeRef.class, propVal)) {
          if (catRef == null) {
            continue;
          }
          // can be running in context of System user, hence use input nodeRef
          catRef = tenantService.getName(nodeRef, catRef);

          try {
            Pair<Long, NodeRef> pair = nodeDAO.getNodePair(catRef);
            if (pair != null) {
              for (Path path : nodeDAO.getPaths(pair, false)) {
                aspectPaths.add(new Pair<Path, QName>(path, aspDef.getName()));
              }
            }
          } catch (InvalidNodeRefException e) {
            // If the category does not exists we move on the next
          }
        }
      }
      categoryPaths.addAll(aspectPaths);
    }
    // Add member final element
    for (Pair<Path, QName> pair : categoryPaths) {
      if (pair.getFirst().last() instanceof Path.ChildAssocElement) {
        Path.ChildAssocElement cae = (Path.ChildAssocElement) pair.getFirst().last();
        ChildAssociationRef assocRef = cae.getRef();
        ChildAssociationRef categoryParentRef =
            new ChildAssociationRef(
                assocRef.getTypeQName(),
                assocRef.getChildRef(),
                QName.createQName("member"),
                nodeRef);
        pair.getFirst().append(new Path.ChildAssocElement(categoryParentRef));
        categoryParents.add(categoryParentRef);
      }
    }

    return new CategoryPaths(categoryPaths, categoryParents);
  }