Java Code Examples for org.alfresco.service.namespace.QName#NAMESPACE_PREFIX

The following examples show how to use org.alfresco.service.namespace.QName#NAMESPACE_PREFIX . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ScriptNode.java    From alfresco-repository with GNU Lesser General Public License v3.0 6 votes vote down vote up
/**
 * @return The array of aspects applied to this node as short prefix qname strings
 */
public Scriptable getAspectsShort()
{
    final NamespaceService ns = this.services.getNamespaceService();
    final Map<String, String> cache = new HashMap<String, String>();
    final Set<QName> aspects = getAspectsSet();
    final Object[] result = new Object[aspects.size()];
    int count = 0;
    for (final QName qname : aspects)
    {
        String prefix = cache.get(qname.getNamespaceURI());
        if (prefix == null)
        {
            // first request for this namespace prefix, get and cache result
            Collection<String> prefixes = ns.getPrefixes(qname.getNamespaceURI());
            prefix = prefixes.size() != 0 ? prefixes.iterator().next() : "";
            cache.put(qname.getNamespaceURI(), prefix);
        }
        result[count++] = prefix + QName.NAMESPACE_PREFIX + qname.getLocalName();
    }
    return Context.getCurrentContext().newArray(this.scope, result);
}
 
Example 2
Source File: JSONConversionComponent.java    From alfresco-repository with GNU Lesser General Public License v3.0 6 votes vote down vote up
/**
 * Convert a qname to a string - either full or short prefixed named.
 * 
 * @param qname QName
 * @param isShortName boolean
 * @return qname string.
 */
private String nameToString(final QName qname, final boolean isShortName)
{
    String result;
    if (isShortName)
    {
        final Map<String, String> cache = namespacePrefixCache.get();
        String prefix = cache.get(qname.getNamespaceURI());
        if (prefix == null)
        {
            // first request for this namespace prefix, get and cache result
            Collection<String> prefixes = this.namespaceService.getPrefixes(qname.getNamespaceURI());
            prefix = prefixes.size() != 0 ? prefixes.iterator().next() : "";
            cache.put(qname.getNamespaceURI(), prefix);
        }
        result = prefix + QName.NAMESPACE_PREFIX + qname.getLocalName();
    }
    else
    {
        result = qname.toString();
    }
    return result;
}
 
Example 3
Source File: CustomModelImportTest.java    From alfresco-remote-api with GNU Lesser General Public License v3.0 4 votes vote down vote up
public void testUploadModel_UnsupportedModelElements() throws Exception
{
    // Note: here we only test a couple of not-supported model elements to check for the correct status code.
    // This test should be removed when we implement the required support

    long timestamp = System.currentTimeMillis();
    final String modelName = getClass().getSimpleName() + timestamp;
    final String prefix = "prefix"+timestamp;
    final String uri = "uriNamespace"+timestamp;
    final String aspectName = prefix + QName.NAMESPACE_PREFIX + "testAspec";
    final String typeName = prefix + QName.NAMESPACE_PREFIX + "testType";
    final String associationName = prefix + QName.NAMESPACE_PREFIX + "testAssociation";

    M2Model model = M2Model.createModel(prefix + QName.NAMESPACE_PREFIX + modelName);
    model.createNamespace(uri, prefix);
    model.setAuthor("John Doe");
    model.createAspect(aspectName);
    model.createImport(NamespaceService.CONTENT_MODEL_1_0_URI, NamespaceService.CONTENT_MODEL_PREFIX);

    M2Type type = model.createType(typeName);
    // Add 'association' not supported yet.
    M2Association association = type.createAssociation(associationName);
    association.setSourceMandatory(false);
    association.setSourceMany(false);
    association.setTargetMandatory(false);
    association.setTargetClassName("cm:content");

    ByteArrayOutputStream xml = new ByteArrayOutputStream();
    model.toXML(xml);
    ZipEntryContext context = new ZipEntryContext(modelName + ".xml", xml.toByteArray());
    File zipFile = createZip(context);

    PostRequest postRequest = buildMultipartPostRequest(zipFile);
    sendRequest(postRequest, 409); // <associations> element is not supported yet

    type.removeAssociation(associationName);
    // Add 'mandatory-aspect' not supported yet.
    type.addMandatoryAspect(aspectName);
    xml = new ByteArrayOutputStream();
    model.toXML(xml);
    context = new ZipEntryContext(modelName + ".xml", xml.toByteArray());
    zipFile = createZip(context);

    postRequest = buildMultipartPostRequest(zipFile);
    sendRequest(postRequest, 409); // <mandatory-aspects> element is not supported yet
}
 
Example 4
Source File: TestCustomTypeAspect.java    From alfresco-remote-api with GNU Lesser General Public License v3.0 4 votes vote down vote up
@Test
//SHA-679
public void testCustomModelTypesAspectsDependencies() throws Exception
{
    setRequestContext(customModelAdmin);
    
    String modelNameOne = "testModel" + System.currentTimeMillis();
    Pair<String, String> namespacePair = getTestNamespaceUriPrefixPair();
    // Create the model as a Model Administrator
    createCustomModel(modelNameOne, namespacePair, ModelStatus.DRAFT, null, "Mark Moe");

    // Add type
    String typeBaseName = "testTypeBase" + System.currentTimeMillis();
    final String typeBaseNameWithPrefix = namespacePair.getSecond() + QName.NAMESPACE_PREFIX + typeBaseName;
    createTypeAspect(CustomType.class, modelNameOne, typeBaseName, "test typeBase title", "test typeBase Desc", "cm:content");

    // Activate the model
    CustomModel modelOneStatusPayload = new CustomModel();
    modelOneStatusPayload.setStatus(ModelStatus.ACTIVE);
    put("cmm", modelNameOne, RestApiUtil.toJsonAsString(modelOneStatusPayload), SELECT_STATUS_QS, 200);

    HttpResponse response = getSingle("cmm", modelNameOne, 200);
    CustomModel returnedModel = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), CustomModel.class);
    assertEquals(ModelStatus.ACTIVE, returnedModel.getStatus());

    // Add another type with 'typeBaseName' as its parent
    String typeName2 = "testTypeChild" + System.currentTimeMillis();
    createTypeAspect(CustomType.class, modelNameOne, typeName2, "test typeChild title", "test typeChild Desc", typeBaseNameWithPrefix);

    Paging paging = getPaging(0, Integer.MAX_VALUE);
    response = getAll("cmm/" + modelNameOne + "/types", paging, 200);
    List<CustomType> returnedTypes = RestApiUtil.parseRestApiEntries(response.getJsonResponse(), CustomType.class);
    assertEquals(2, returnedTypes.size());

    // Create another model
    String modelNameTwo = "testModelTwo" + System.currentTimeMillis();
    Pair<String, String> modelTwoNamespacePair = getTestNamespaceUriPrefixPair();
    createCustomModel(modelNameTwo, modelTwoNamespacePair, ModelStatus.DRAFT, null, "Admin");

    // Add a type with 'typeBaseName' from the modelOne as its parent
    String modelTwoTypeName = "testModelTwoChild" + System.currentTimeMillis();
    createTypeAspect(CustomType.class, modelNameTwo, modelTwoTypeName, "test model two type child title", null, typeBaseNameWithPrefix);

    // Try to deactivate modelOne
    modelOneStatusPayload = new CustomModel();
    modelOneStatusPayload.setStatus(ModelStatus.DRAFT);
    put("cmm", modelNameOne, RestApiUtil.toJsonAsString(modelOneStatusPayload), SELECT_STATUS_QS, 409); // ModelTwo depends on ModelOne

    // Activate modelTwo
    CustomModel modelTwoStatusPayload = new CustomModel();
    modelTwoStatusPayload.setStatus(ModelStatus.ACTIVE);
    put("cmm", modelNameTwo, RestApiUtil.toJsonAsString(modelTwoStatusPayload), SELECT_STATUS_QS, 200);
  
    // Try to deactivate modelOne again. The dependent model is Active now, however, the result should be the same.
    put("cmm", modelNameOne, RestApiUtil.toJsonAsString(modelOneStatusPayload), SELECT_STATUS_QS, 409); // ModelTwo depends on ModelOne

    // Deactivate modelTwo
    modelTwoStatusPayload = new CustomModel();
    modelTwoStatusPayload.setStatus(ModelStatus.DRAFT);
    put("cmm", modelNameTwo, RestApiUtil.toJsonAsString(modelTwoStatusPayload), SELECT_STATUS_QS, 200);

    // Delete the modelTwo's type as a Model Administrator
    delete("cmm/" + modelNameTwo + "/types", modelTwoTypeName, 204);

    // Try to deactivate modelOne again. There is no dependency
    put("cmm", modelNameOne, RestApiUtil.toJsonAsString(modelOneStatusPayload), SELECT_STATUS_QS, 200);

}
 
Example 5
Source File: TestCustomModel.java    From alfresco-remote-api with GNU Lesser General Public License v3.0 4 votes vote down vote up
@Test
//SHA-726
public void testUpdateModel_WithAspectsAndTypes() throws Exception
{
    setRequestContext(customModelAdmin);
    
    String modelName = "testModel" + System.currentTimeMillis();
    Pair<String, String> namespacePair = getTestNamespaceUriPrefixPair();
    // Create the model as a Model Administrator
    createCustomModel(modelName, namespacePair, ModelStatus.DRAFT);

    // Add type
    String typeBaseName = "testTypeBase" + System.currentTimeMillis();
    final String typeBaseNameWithPrefix = namespacePair.getSecond() + QName.NAMESPACE_PREFIX + typeBaseName;
    createTypeAspect(CustomType.class, modelName, typeBaseName, "test typeBase title", null, "cm:content");

    // Add aspect
    final String aspectName = "testAspect" + System.currentTimeMillis();
    final String aspectNameWithPrefix = namespacePair.getSecond() + QName.NAMESPACE_PREFIX + aspectName;
    createTypeAspect(CustomAspect.class, modelName, aspectName, null, null, null);

    // Activate the model
    CustomModel modelOneStatusPayload = new CustomModel();
    modelOneStatusPayload.setStatus(ModelStatus.ACTIVE);
    put("cmm", modelName, RestApiUtil.toJsonAsString(modelOneStatusPayload), SELECT_STATUS_QS, 200);

    // Add another type with 'typeBaseName' as its parent
    String childTypeName = "testTypeChild" + System.currentTimeMillis();
    createTypeAspect(CustomType.class, modelName, childTypeName, "test typeChild title", "test typeChild Desc", typeBaseNameWithPrefix);

    // Add another aspect with 'aspectName' as its parent
    final String childAspectName = "testChildAspect" + System.currentTimeMillis();
    createTypeAspect(CustomAspect.class, modelName, childAspectName, "test child aspect title", null, aspectNameWithPrefix);

     // Deactivate the model
    modelOneStatusPayload = new CustomModel();
    modelOneStatusPayload.setStatus(ModelStatus.DRAFT);
    put("cmm", modelName, RestApiUtil.toJsonAsString(modelOneStatusPayload), SELECT_STATUS_QS, 200);

    // Test update the namespace prefix
    CustomModel updatePayload = new CustomModel();
    String modifiedPrefix = namespacePair.getSecond() + "Modified";
    updatePayload.setNamespacePrefix(modifiedPrefix);
    updatePayload.setNamespaceUri(namespacePair.getFirst());
    HttpResponse response = put("cmm", modelName, RestApiUtil.toJsonAsString(updatePayload), null, 200);
    CustomModel returnedModel = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), CustomModel.class);
    assertEquals(modifiedPrefix, returnedModel.getNamespacePrefix());
    assertEquals("The namespace URI shouldn't have changed.", namespacePair.getFirst(), returnedModel.getNamespaceUri());

    // Test update the namespace URI
    updatePayload = new CustomModel();
    updatePayload.setNamespacePrefix(modifiedPrefix);
    String modifiedURI = namespacePair.getFirst() + "Modified";
    updatePayload.setNamespaceUri(modifiedURI);
    response = put("cmm", modelName, RestApiUtil.toJsonAsString(updatePayload), null, 200);
    returnedModel = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), CustomModel.class);
    assertEquals(modifiedURI, returnedModel.getNamespaceUri());
    assertEquals("The namespace prefix shouldn't have changed.", modifiedPrefix, returnedModel.getNamespacePrefix());

    // Retrieve the child type
    response = getSingle("cmm/" + modelName + "/types", childTypeName,  200);
    CustomType returnedChildType = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), CustomType.class);
    final String newTypeParentName = modifiedPrefix + QName.NAMESPACE_PREFIX + typeBaseName;
    assertEquals("The parent name prefix should have been updated.", newTypeParentName, returnedChildType.getParentName());

    // Retrieve the child aspect
    response = getSingle("cmm/" + modelName + "/aspects", childAspectName,  200);
    CustomAspect returnedChildAspect = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), CustomAspect.class);
    final String newAspectParentName = modifiedPrefix + QName.NAMESPACE_PREFIX + aspectName;
    assertEquals("The parent name prefix should have been updated.", newAspectParentName, returnedChildAspect.getParentName());
}