Search in sources :

Example 31 with PropertyMap

use of org.structr.core.property.PropertyMap in project structr by structr.

the class Content method updateFromNode.

/*

	@Override
	public String getContextName() {
		return "#text";
	}
	*/
public static void updateFromNode(final Content thisContent, final DOMNode newNode) throws FrameworkException {
    if (newNode instanceof Content) {
        final PropertyKey<String> contentKey = StructrApp.key(Content.class, "content");
        thisContent.setProperties(thisContent.getSecurityContext(), new PropertyMap(contentKey, newNode.getProperty(contentKey)));
    }
}
Also used : PropertyMap(org.structr.core.property.PropertyMap)

Example 32 with PropertyMap

use of org.structr.core.property.PropertyMap in project structr by structr.

the class Content method onModification.

/*
	public static final org.structr.common.View uiView                                   = new org.structr.common.View(Content.class, PropertyView.Ui,
		content, contentType, parent, pageId, syncedNodes, sharedComponent, sharedComponentConfiguration, dataKey, restQuery, cypherQuery, xpathQuery, functionQuery,
		hideOnDetail, hideOnIndex, showForLocales, hideForLocales, showConditions, hideConditions, isContent, isDOMNode, isFavoritable
	);

	public static final org.structr.common.View publicView                               = new org.structr.common.View(Content.class, PropertyView.Public,
		content, contentType, parent, pageId, syncedNodes, sharedComponent, sharedComponentConfiguration, dataKey, restQuery, cypherQuery, xpathQuery, functionQuery,
		hideOnDetail, hideOnIndex, showForLocales, hideForLocales, showConditions, hideConditions, isContent, isDOMNode, isFavoritable
	);
	*/
static void onModification(final Content thisContent, final SecurityContext securityContext, final ErrorBuffer errorBuffer, final ModificationQueue modificationQueue) throws FrameworkException {
    final PropertyMap map = new PropertyMap();
    // sync content only
    map.put(StructrApp.key(Content.class, "content"), thisContent.getContent());
    map.put(StructrApp.key(Content.class, "contentType"), thisContent.getContentType());
    map.put(StructrApp.key(Content.class, "name"), thisContent.getProperty(StructrApp.key(Content.class, "name")));
    for (final DOMNode syncedNode : thisContent.getSyncedNodes()) {
        syncedNode.setProperties(securityContext, map);
    }
    final DOMNode sharedComponent = thisContent.getSharedComponent();
    if (sharedComponent != null) {
        sharedComponent.setProperties(sharedComponent.getSecurityContext(), map);
    }
}
Also used : PropertyMap(org.structr.core.property.PropertyMap)

Example 33 with PropertyMap

use of org.structr.core.property.PropertyMap in project structr by structr.

the class CopyFileContentsFunction method apply.

@Override
public Object apply(final ActionContext ctx, final Object caller, Object[] sources) throws FrameworkException {
    if (arrayHasMinLengthAndAllElementsNotNull(sources, 2)) {
        final Object toCopy = sources[0];
        final Object toBeReplaced = sources[1];
        if (toCopy instanceof File && toBeReplaced instanceof File) {
            File nodeToCopy = (File) toCopy;
            File nodeToBeReplaced = (File) toBeReplaced;
            try {
                java.io.File fileToCopy = nodeToCopy.getFileOnDisk();
                if (!fileToCopy.exists()) {
                    logger.warn("Error: Given source file does not exist. Parameters: {}", getParametersAsString(sources));
                    return "Error: Given source file does not exist.";
                }
                java.io.File fileToBeReplaced = nodeToBeReplaced.getFileOnDisk();
                if (!fileToBeReplaced.exists()) {
                    // Call afterCreation method to ensure that the file is properly initialized.
                    nodeToBeReplaced.afterCreation(nodeToBeReplaced.getSecurityContext());
                }
                Files.copy(fileToCopy, fileToBeReplaced);
                final PropertyKey<Integer> versionKey = StructrApp.key(File.class, "version");
                final PropertyKey<Long> checksumKey = StructrApp.key(File.class, "checksum");
                final PropertyKey<Long> sizeKey = StructrApp.key(File.class, "size");
                final PropertyMap changedProperties = new PropertyMap();
                changedProperties.put(checksumKey, FileHelper.getChecksum(fileToBeReplaced));
                changedProperties.put(versionKey, 0);
                changedProperties.put(new StringProperty("contentType"), nodeToCopy.getProperty(new StringProperty("contentType")));
                long fileSize = FileHelper.getSize(fileToBeReplaced);
                if (fileSize > 0) {
                    changedProperties.put(sizeKey, fileSize);
                }
                nodeToBeReplaced.unlockSystemPropertiesOnce();
                nodeToBeReplaced.setProperties(nodeToBeReplaced.getSecurityContext(), changedProperties);
                return nodeToBeReplaced;
            } catch (IOException | FrameworkException ex) {
                logger.error("Error: Could not copy file due to exception.", ex);
                return "Error: Could not copy file due to exception.";
            }
        } else {
            logger.warn("Error: entities are not instances of File. Parameters: {}", getParametersAsString(sources));
            return "Error: entities are not nodes.";
        }
    } else {
        logParameterError(caller, sources, ctx.isJavaScriptContext());
        return usage(ctx.isJavaScriptContext());
    }
}
Also used : FrameworkException(org.structr.common.error.FrameworkException) StringProperty(org.structr.core.property.StringProperty) IOException(java.io.IOException) PropertyMap(org.structr.core.property.PropertyMap) File(org.structr.web.entity.File)

Example 34 with PropertyMap

use of org.structr.core.property.PropertyMap in project structr by structr.

the class LogEventFunction method apply.

@Override
public Object apply(final ActionContext ctx, final Object caller, final Object[] sources) throws FrameworkException {
    if (arrayHasMinLengthAndMaxLengthAndAllElementsNotNull(sources, 2, 4)) {
        final String action = sources[0].toString();
        final String message = sources[1].toString();
        final LogEvent logEvent = StructrApp.getInstance().create(LogEvent.class, new NodeAttribute(LogEvent.actionProperty, action), new NodeAttribute(LogEvent.messageProperty, message), new NodeAttribute(LogEvent.timestampProperty, new Date()));
        switch(sources.length) {
            case 4:
                final String object = sources[3].toString();
                logEvent.setProperties(logEvent.getSecurityContext(), new PropertyMap(LogEvent.objectProperty, object));
            case 3:
                final String subject = sources[2].toString();
                logEvent.setProperties(logEvent.getSecurityContext(), new PropertyMap(LogEvent.subjectProperty, subject));
                break;
        }
        return logEvent;
    } else if (sources.length == 1 && sources[0] instanceof Map) {
        // support javascript objects here
        final Map map = (Map) sources[0];
        final String action = DOMNode.objectToString(map.get("action"));
        final String message = DOMNode.objectToString(map.get("message"));
        final String subject = DOMNode.objectToString(map.get("subject"));
        final String object = DOMNode.objectToString(map.get("object"));
        return StructrApp.getInstance().create(LogEvent.class, new NodeAttribute(LogEvent.actionProperty, action), new NodeAttribute(LogEvent.messageProperty, message), new NodeAttribute(LogEvent.timestampProperty, new Date()), new NodeAttribute(LogEvent.subjectProperty, subject), new NodeAttribute(LogEvent.objectProperty, object));
    } else {
        logParameterError(caller, sources, ctx.isJavaScriptContext());
    }
    return "";
}
Also used : NodeAttribute(org.structr.core.graph.NodeAttribute) PropertyMap(org.structr.core.property.PropertyMap) LogEvent(org.structr.rest.logging.entity.LogEvent) PropertyMap(org.structr.core.property.PropertyMap) Map(java.util.Map) Date(java.util.Date)

Example 35 with PropertyMap

use of org.structr.core.property.PropertyMap in project structr by structr.

the class TypeAndValueDeserializationStrategy method deserialize.

@Override
public T deserialize(final SecurityContext securityContext, Class<T> type, S source, final Object context) throws FrameworkException {
    final App app = StructrApp.getInstance(securityContext);
    Result<T> result = Result.EMPTY_RESULT;
    // default to UUID
    if (propertyKey == null) {
        propertyKey = GraphObject.id;
    }
    // create and fill input map with source object
    Map<String, Object> sourceMap = new LinkedHashMap<>();
    sourceMap.put(propertyKey.jsonName(), source);
    // try to convert input type to java type in order to create object correctly
    PropertyMap convertedSourceMap = PropertyMap.inputTypeToJavaType(securityContext, type, sourceMap);
    Object convertedSource = convertedSourceMap.get(propertyKey);
    if (convertedSource != null) {
        // FIXME: use uuid only here?
        if (convertedSource instanceof Map) {
            Object value = ((Map<String, Object>) convertedSource).get(propertyKey.jsonName());
            if (value != null) {
                result = app.nodeQuery(type).and(propertyKey, value.toString()).getResult();
            }
        } else if (convertedSource instanceof GraphObject) {
            final GraphObject obj = (GraphObject) convertedSource;
            result = app.nodeQuery(type).and(propertyKey, obj.getProperty(propertyKey)).getResult();
        } else {
            result = app.nodeQuery(type).and(propertyKey, convertedSource).getResult();
        }
    }
    // just check for existance
    int resultCount = result.size();
    switch(resultCount) {
        case 0:
            if ((convertedSource != null) && createIfNotExisting) {
                // create node and return it
                T newNode = app.create(type);
                if (newNode != null) {
                    newNode.setProperty(propertyKey, convertedSource);
                    return newNode;
                }
            } else {
                logger.warn("Unable to create node of type {} for property {}", new Object[] { type.getSimpleName(), propertyKey.jsonName() });
            }
            break;
        case 1:
            T obj = result.get(0);
            // if(!type.getSimpleName().equals(node.getType())) {
            if (!type.isAssignableFrom(obj.getClass())) {
                throw new FrameworkException(422, "Node type mismatch", new TypeToken(obj.getClass(), propertyKey, type.getSimpleName()));
            }
            if (!convertedSourceMap.isEmpty()) {
                // set properties on related node?
                setProperties(securityContext, obj, convertedSourceMap);
            }
            return obj;
    }
    if (convertedSource != null) {
        PropertyMap attributes = new PropertyMap();
        attributes.put(propertyKey, convertedSource);
        attributes.put(AbstractNode.type, type.getSimpleName());
        throw new FrameworkException(404, "No node found for given properties", new PropertiesNotFoundToken(type.getSimpleName(), null, attributes));
    }
    return null;
}
Also used : StructrApp(org.structr.core.app.StructrApp) App(org.structr.core.app.App) FrameworkException(org.structr.common.error.FrameworkException) PropertiesNotFoundToken(org.structr.common.error.PropertiesNotFoundToken) PropertyMap(org.structr.core.property.PropertyMap) TypeToken(org.structr.common.error.TypeToken) PropertyMap(org.structr.core.property.PropertyMap)

Aggregations

PropertyMap (org.structr.core.property.PropertyMap)233 FrameworkException (org.structr.common.error.FrameworkException)100 Tx (org.structr.core.graph.Tx)93 Test (org.junit.Test)60 App (org.structr.core.app.App)34 StructrApp (org.structr.core.app.StructrApp)34 PropertyKey (org.structr.core.property.PropertyKey)34 LinkedList (java.util.LinkedList)28 NodeInterface (org.structr.core.graph.NodeInterface)25 SchemaProperty (org.structr.core.entity.SchemaProperty)23 SecurityContext (org.structr.common.SecurityContext)22 StructrUiTest (org.structr.web.StructrUiTest)21 GraphObject (org.structr.core.GraphObject)20 Result (org.structr.core.Result)19 File (org.structr.web.entity.File)19 DOMNode (org.structr.web.entity.dom.DOMNode)19 TestOne (org.structr.core.entity.TestOne)17 AbstractNode (org.structr.core.entity.AbstractNode)16 Folder (org.structr.web.entity.Folder)15 Page (org.structr.web.entity.dom.Page)15