Search in sources :

Example 1 with ResourceProxy

use of org.apache.sling.ide.transport.ResourceProxy in project sling by apache.

the class VltNodeTypeFactory method initDeclaredPropertyDefinitions.

private void initDeclaredPropertyDefinitions(VltNodeType nt, ResourceProxy child) {
    Map<String, VltPropertyDefinition> pds = new HashMap<>();
    // load propertyDefinition children
    for (ResourceProxy aChild : child.getChildren()) {
        String childName = PathUtil.getName(aChild.getPath());
        if (childName.startsWith("jcr:propertyDefinition")) {
            String jcrName = (String) aChild.getProperties().get("jcr:name");
            if (jcrName != null) {
                VltPropertyDefinition pd = new VltPropertyDefinition();
                pd.setName(jcrName);
                Boolean autoCreated = (Boolean) aChild.getProperties().get("jcr:autoCreated");
                if (autoCreated != null) {
                    pd.setAutoCreated(autoCreated);
                }
                Boolean multiple = (Boolean) aChild.getProperties().get("jcr:multiple");
                if (multiple != null) {
                    pd.setMultiple(multiple);
                }
                Boolean mandatory = (Boolean) aChild.getProperties().get("jcr:mandatory");
                if (mandatory != null) {
                    pd.setMandatory(mandatory);
                }
                Boolean isProtected = (Boolean) aChild.getProperties().get("jcr:protected");
                if (isProtected != null) {
                    pd.setProtected(isProtected);
                }
                final Object object = aChild.getProperties().get("jcr:requiredType");
                if (object != null) {
                    String requiredType = (String) object;
                    if (requiredType != null) {
                        pd.setRequiredType(propertyTypeFromName(requiredType));
                    }
                }
                pds.put(jcrName, pd);
            }
        }
    }
    // load mandatory
    String[] mandatoryProperties = (String[]) child.getProperties().get("rep:mandatoryProperties");
    if (mandatoryProperties != null) {
        for (int i = 0; i < mandatoryProperties.length; i++) {
            String aMandatoryProperty = mandatoryProperties[i];
            VltPropertyDefinition vpd = pds.get(aMandatoryProperty);
            if (vpd == null) {
                vpd = new VltPropertyDefinition();
                vpd.setName(aMandatoryProperty);
                pds.put(aMandatoryProperty, vpd);
            }
            vpd.setMandatory(true);
        }
    }
    // load protected
    String[] protectedProperties = (String[]) child.getProperties().get("rep:protectedProperties");
    if (protectedProperties != null) {
        for (int i = 0; i < protectedProperties.length; i++) {
            String aProtectedProperties = protectedProperties[i];
            VltPropertyDefinition vpd = pds.get(aProtectedProperties);
            if (vpd == null) {
                vpd = new VltPropertyDefinition();
                vpd.setName(aProtectedProperties);
                pds.put(aProtectedProperties, vpd);
            }
            vpd.setProtected(true);
        }
    }
    nt.setDeclaredPropertyDefinitions(pds.values().toArray(new VltPropertyDefinition[pds.size()]));
}
Also used : HashMap(java.util.HashMap) ResourceProxy(org.apache.sling.ide.transport.ResourceProxy)

Example 2 with ResourceProxy

use of org.apache.sling.ide.transport.ResourceProxy in project sling by apache.

the class JcrNode method getPropertyType.

public int getPropertyType(String propertyName) {
    PropertyDefinition pd = getPropertyDefinition(propertyName);
    if (pd != null) {
        return pd.getRequiredType();
    }
    // otherwise use the SerializationManager to read the
    // underlying vault file and derive the propertyType from there
    GenericJcrRootFile u = properties.getUnderlying();
    if (u == null) {
        // no underlying properties file, that's not good
        Activator.getDefault().getPluginLogger().warn("No underlying properties file, cannot derive propertyType (" + propertyName + ") for " + this);
        return -1;
    }
    IFolder contentSyncRoot = ProjectUtil.getSyncDirectory(getProject());
    IFile file = (IFile) u.file;
    try (InputStream contents = file.getContents()) {
        String resourceLocation = file.getFullPath().makeRelativeTo(contentSyncRoot.getFullPath()).toPortableString();
        ResourceProxy resourceProxy = Activator.getDefault().getSerializationManager().readSerializationData(resourceLocation, contents);
        // resourceProxy could be containing a full tree
        // dive into the right position
        String rawValue = properties.getValue(propertyName);
        return PropertyTypeSupport.propertyTypeOfString(rawValue);
    } catch (Exception e) {
        Activator.getDefault().getPluginLogger().warn("Exception occurred during analyzing propertyType (" + propertyName + ") for " + this, e);
    }
    return -1;
}
Also used : IFile(org.eclipse.core.resources.IFile) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) PropertyDefinition(javax.jcr.nodetype.PropertyDefinition) ResourceProxy(org.apache.sling.ide.transport.ResourceProxy) CoreException(org.eclipse.core.runtime.CoreException) XMLParseException(de.pdark.decentxml.XMLParseException) SAXException(org.xml.sax.SAXException) IOException(java.io.IOException) RepositoryException(org.apache.sling.ide.transport.RepositoryException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) IFolder(org.eclipse.core.resources.IFolder)

Example 3 with ResourceProxy

use of org.apache.sling.ide.transport.ResourceProxy in project sling by apache.

the class ResourceChangeCommandFactory method normaliseResourceChildren.

/**
     * Normalises the of the specified <tt>resourceProxy</tt> by comparing the serialization data and the filesystem
     * data
     * 
     * @param serializationFile the file which contains the serialization data
     * @param resourceProxy the resource proxy
     * @param syncDirectory the sync directory
     * @param repository TODO
     * @throws CoreException
     */
private void normaliseResourceChildren(IFile serializationFile, ResourceProxy resourceProxy, IFolder syncDirectory, Repository repository) throws CoreException {
    // TODO - this logic should be moved to the serializationManager
    try {
        SerializationKindManager skm = new SerializationKindManager();
        skm.init(repository);
        String primaryType = (String) resourceProxy.getProperties().get(Repository.JCR_PRIMARY_TYPE);
        List<String> mixinTypesList = getMixinTypes(resourceProxy);
        SerializationKind serializationKind = skm.getSerializationKind(primaryType, mixinTypesList);
        if (serializationKind == SerializationKind.METADATA_FULL) {
            return;
        }
    } catch (RepositoryException e) {
        throw new CoreException(new Status(IStatus.ERROR, Activator.PLUGIN_ID, "Failed creating a " + SerializationDataBuilder.class.getName(), e));
    }
    IPath serializationDirectoryPath = serializationFile.getFullPath().removeLastSegments(1);
    Iterator<ResourceProxy> childIterator = resourceProxy.getChildren().iterator();
    Map<String, IResource> extraChildResources = new HashMap<>();
    for (IResource member : serializationFile.getParent().members()) {
        if (member.equals(serializationFile)) {
            continue;
        }
        extraChildResources.put(member.getName(), member);
    }
    while (childIterator.hasNext()) {
        ResourceProxy child = childIterator.next();
        String childName = PathUtil.getName(child.getPath());
        String osPath = serializationManager.getOsPath(childName);
        // covered children might have a FS representation, depending on their child nodes, so
        // accept a directory which maps to their name
        extraChildResources.remove(osPath);
        // covered children do not need a filesystem representation
        if (resourceProxy.covers(child.getPath())) {
            continue;
        }
        IPath childPath = serializationDirectoryPath.append(osPath);
        IResource childResource = ResourcesPlugin.getWorkspace().getRoot().findMember(childPath);
        if (childResource == null) {
            Activator.getDefault().getPluginLogger().trace("For resource at with serialization data {0} the serialized child resource at {1} does not exist in the filesystem and will be ignored", serializationFile, childPath);
            childIterator.remove();
        }
    }
    for (IResource extraChildResource : extraChildResources.values()) {
        IPath extraChildResourcePath = extraChildResource.getFullPath().makeRelativeTo(syncDirectory.getFullPath()).makeAbsolute();
        resourceProxy.addChild(new ResourceProxy(serializationManager.getRepositoryPath(extraChildResourcePath.toPortableString())));
        Activator.getDefault().getPluginLogger().trace("For resource at with serialization data {0} the found a child resource at {1} which is not listed in the serialized child resources and will be added", serializationFile, extraChildResource);
    }
}
Also used : IStatus(org.eclipse.core.runtime.IStatus) Status(org.eclipse.core.runtime.Status) IPath(org.eclipse.core.runtime.IPath) HashMap(java.util.HashMap) RepositoryException(org.apache.sling.ide.transport.RepositoryException) ResourceProxy(org.apache.sling.ide.transport.ResourceProxy) SerializationKindManager(org.apache.sling.ide.serialization.SerializationKindManager) CoreException(org.eclipse.core.runtime.CoreException) SerializationKind(org.apache.sling.ide.serialization.SerializationKind) IResource(org.eclipse.core.resources.IResource)

Example 4 with ResourceProxy

use of org.apache.sling.ide.transport.ResourceProxy in project sling by apache.

the class ResourceChangeCommandFactory method buildResourceProxy.

private ResourceProxy buildResourceProxy(String resourceLocation, IResource serializationResource, IFolder syncDirectory, String fallbackPrimaryType, Repository repository) throws CoreException, IOException {
    if (serializationResource instanceof IFile) {
        IFile serializationFile = (IFile) serializationResource;
        try (InputStream contents = serializationFile.getContents()) {
            String serializationFilePath = serializationResource.getFullPath().makeRelativeTo(syncDirectory.getFullPath()).toPortableString();
            ResourceProxy resourceProxy = serializationManager.readSerializationData(serializationFilePath, contents);
            normaliseResourceChildren(serializationFile, resourceProxy, syncDirectory, repository);
            return resourceProxy;
        }
    }
    return new ResourceProxy(serializationManager.getRepositoryPath(resourceLocation), Collections.singletonMap(Repository.JCR_PRIMARY_TYPE, (Object) fallbackPrimaryType));
}
Also used : IFile(org.eclipse.core.resources.IFile) InputStream(java.io.InputStream) ResourceProxy(org.apache.sling.ide.transport.ResourceProxy)

Example 5 with ResourceProxy

use of org.apache.sling.ide.transport.ResourceProxy in project sling by apache.

the class ResourceChangeCommandFactory method removeFileCommand.

private Command<?> removeFileCommand(Repository repository, IResource resource) throws CoreException, IOException {
    if (resource.isTeamPrivateMember(IResource.CHECK_ANCESTORS)) {
        Activator.getDefault().getPluginLogger().trace("Skipping team-private resource {0}", resource);
        return null;
    }
    if (ignoredFileNames.contains(resource.getName())) {
        return null;
    }
    IFolder syncDirectory = ProjectUtil.getSyncDirectory(resource.getProject());
    Filter filter = ProjectUtil.loadFilter(syncDirectory.getProject());
    FilterResult filterResult = getFilterResult(resource, null, filter);
    if (filterResult == FilterResult.DENY || filterResult == FilterResult.PREREQUISITE) {
        return null;
    }
    String resourceLocation = getRepositoryPathForDeletedResource(resource, ProjectUtil.getSyncDirectoryFile(resource.getProject()));
    // verify whether a resource being deleted does not signal that the content structure
    // was rearranged under a covering parent aggregate
    IPath serializationFilePath = Path.fromOSString(serializationManager.getSerializationFilePath(resourceLocation, SerializationKind.FOLDER));
    ResourceProxy coveringParentData = findSerializationDataFromCoveringParent(resource, syncDirectory, resourceLocation, serializationFilePath);
    if (coveringParentData != null) {
        Activator.getDefault().getPluginLogger().trace("Found covering resource data ( repository path = {0} ) for resource at {1},  skipping deletion and performing an update instead", coveringParentData.getPath(), resource.getFullPath());
        FileInfo info = createFileInfo(resource);
        return repository.newAddOrUpdateNodeCommand(new CommandContext(filter), info, coveringParentData);
    }
    return repository.newDeleteNodeCommand(serializationManager.getRepositoryPath(resourceLocation));
}
Also used : IPath(org.eclipse.core.runtime.IPath) FileInfo(org.apache.sling.ide.transport.FileInfo) CommandContext(org.apache.sling.ide.transport.CommandContext) Filter(org.apache.sling.ide.filter.Filter) FilterResult(org.apache.sling.ide.filter.FilterResult) ResourceProxy(org.apache.sling.ide.transport.ResourceProxy) IFolder(org.eclipse.core.resources.IFolder)

Aggregations

ResourceProxy (org.apache.sling.ide.transport.ResourceProxy)42 Test (org.junit.Test)14 Node (javax.jcr.Node)8 NodeIterator (javax.jcr.NodeIterator)5 IPath (org.eclipse.core.runtime.IPath)5 IOException (java.io.IOException)4 InputStream (java.io.InputStream)4 HashMap (java.util.HashMap)4 FilterResult (org.apache.sling.ide.filter.FilterResult)4 IFile (org.eclipse.core.resources.IFile)4 IFolder (org.eclipse.core.resources.IFolder)4 Filter (org.apache.sling.ide.filter.Filter)3 RepositoryException (org.apache.sling.ide.transport.RepositoryException)3 JsonReader (com.google.gson.stream.JsonReader)2 JsonToken (com.google.gson.stream.JsonToken)2 File (java.io.File)2 InputStreamReader (java.io.InputStreamReader)2 HashSet (java.util.HashSet)2 LinkedList (java.util.LinkedList)2 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)2