Search in sources :

Example 76 with PropertyMap

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

the class SearchAndSortingTest method test09SearchByEmptyStringField.

@Test
public void test09SearchByEmptyStringField() {
    try {
        PropertyMap props = new PropertyMap();
        AbstractNode node = createTestNode(TestOne.class, props);
        try (final Tx tx = app.tx()) {
            Result result = app.nodeQuery(TestOne.class).and(TestOne.aString, null).includeDeletedAndHidden().getResult();
            assertTrue(result.size() == 1);
            assertTrue(result.get(0).equals(node));
        }
    } catch (FrameworkException ex) {
        logger.warn("", ex);
        logger.error(ex.toString());
        fail("Unexpected exception");
    }
}
Also used : PropertyMap(org.structr.core.property.PropertyMap) Tx(org.structr.core.graph.Tx) FrameworkException(org.structr.common.error.FrameworkException) AbstractNode(org.structr.core.entity.AbstractNode) TestOne(org.structr.core.entity.TestOne) Result(org.structr.core.Result) Test(org.junit.Test)

Example 77 with PropertyMap

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

the class SearchAndSortingTest method test11SearchByEmptyIntField.

@Test
public void test11SearchByEmptyIntField() {
    try {
        PropertyMap props = new PropertyMap();
        AbstractNode node = createTestNode(TestOne.class, props);
        try (final Tx tx = app.tx()) {
            Result result = app.nodeQuery(TestOne.class).and(TestOne.anInt, null).includeDeletedAndHidden().getResult();
            assertTrue(result.size() == 1);
            assertTrue(result.get(0).equals(node));
        }
    } catch (Throwable ex) {
        logger.warn("", ex);
        logger.error(ex.toString());
        fail("Unexpected exception");
    }
}
Also used : PropertyMap(org.structr.core.property.PropertyMap) Tx(org.structr.core.graph.Tx) AbstractNode(org.structr.core.entity.AbstractNode) TestOne(org.structr.core.entity.TestOne) Result(org.structr.core.Result) Test(org.junit.Test)

Example 78 with PropertyMap

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

the class SchemaImporter method analyzeSchema.

public void analyzeSchema() {
    final App app = StructrApp.getInstance();
    final FileBasedHashLongMap<NodeInfo> nodeIdMap = new FileBasedHashLongMap<>(userHome + File.separator + ".structrSchemaAnalyzer");
    final DatabaseService graphDb = app.getDatabaseService();
    final ConfigurationProvider configuration = Services.getInstance().getConfigurationProvider();
    final Set<NodeInfo> nodeTypes = new LinkedHashSet<>();
    final Set<RelationshipInfo> relationships = new LinkedHashSet<>();
    final Map<String, SchemaNode> schemaNodes = new LinkedHashMap<>();
    final Map<String, List<TypeInfo>> typeInfoTypeMap = new LinkedHashMap<>();
    final List<TypeInfo> reducedTypeInfos = new LinkedList<>();
    final List<TypeInfo> typeInfos = new LinkedList<>();
    Iterator<Relationship> relIterator = null;
    Iterator<Node> nodeIterator = null;
    info("Fetching all nodes iterator..");
    try (final Tx tx = app.tx()) {
        nodeIterator = Iterables.filter(new StructrAndSpatialPredicate(false, false, true), graphDb.getAllNodes()).iterator();
        tx.success();
    } catch (FrameworkException fex) {
        logger.warn("", fex);
    }
    info("Starting to analyze nodes..");
    bulkGraphOperation(SecurityContext.getSuperUserInstance(), nodeIterator, 100000, "Analyzing nodes", new BulkGraphOperation<Node>() {

        @Override
        public void handleGraphObject(final SecurityContext securityContext, final Node node) throws FrameworkException {
            final NodeInfo nodeInfo = new NodeInfo(node);
            // hashcode of nodeInfo is derived from its property and type signature!
            nodeTypes.add(nodeInfo);
            // add node ID to our new test datastructure
            nodeIdMap.add(nodeInfo, node.getId());
        }
    });
    info("Identifying common base classes..");
    try (final Tx tx = app.tx(true, false, false)) {
        // nodeTypes now contains all existing node types and their property sets
        identifyCommonBaseClasses(app, nodeTypes, nodeIdMap, typeInfos);
        tx.success();
    } catch (FrameworkException fex) {
        logger.warn("", fex);
    }
    info("Collecting type information..");
    try (final Tx tx = app.tx(true, false, false)) {
        // group type infos by type
        collectTypeInfos(typeInfos, typeInfoTypeMap);
        tx.success();
    } catch (FrameworkException fex) {
        logger.warn("", fex);
    }
    info("Aggregating type information..");
    try (final Tx tx = app.tx(true, false, false)) {
        // reduce type infos with more than one type
        reduceTypeInfos(typeInfoTypeMap, reducedTypeInfos);
        tx.success();
    } catch (FrameworkException fex) {
        logger.warn("", fex);
    }
    info("Identifying property sets..");
    try (final Tx tx = app.tx(true, false, false)) {
        // intersect property sets of type infos
        intersectPropertySets(reducedTypeInfos);
        tx.success();
    } catch (FrameworkException fex) {
        logger.warn("", fex);
    }
    info("Sorting result..");
    try (final Tx tx = app.tx(true, false, false)) {
        // sort type infos
        Collections.sort(reducedTypeInfos, new HierarchyComparator(false));
        tx.success();
    } catch (FrameworkException fex) {
        logger.warn("", fex);
    }
    final Map<String, TypeInfo> reducedTypeInfoMap = new LinkedHashMap<>();
    for (final TypeInfo info : reducedTypeInfos) {
        final String type = info.getPrimaryType();
        // map TypeInfo to type for later use
        reducedTypeInfoMap.put(type, info);
        info("Starting with setting of type and ID for type {}", type);
        bulkGraphOperation(SecurityContext.getSuperUserInstance(), info.getNodeIds().iterator(), 10000, "Setting type and ID", new BulkGraphOperation<Long>() {

            @Override
            public void handleGraphObject(SecurityContext securityContext, Long nodeId) throws FrameworkException {
                final Node node = graphDb.getNodeById(nodeId);
                node.setProperty(GraphObject.id.dbName(), NodeServiceCommand.getNextUuid());
                node.setProperty(GraphObject.type.dbName(), type);
            }
        });
    }
    info("Fetching all relationships iterator..");
    try (final Tx tx = app.tx(true, false, false)) {
        relIterator = Iterables.filter(new StructrAndSpatialPredicate(false, false, true), graphDb.getAllRelationships()).iterator();
        tx.success();
    } catch (FrameworkException fex) {
        logger.warn("", fex);
    }
    info("Starting with analyzing relationships..");
    bulkGraphOperation(SecurityContext.getSuperUserInstance(), relIterator, 10000, "Analyzing relationships", new BulkGraphOperation<Relationship>() {

        @Override
        public void handleGraphObject(SecurityContext securityContext, Relationship rel) throws FrameworkException {
            final Node startNode = rel.getStartNode();
            final Node endNode = rel.getEndNode();
            // make sure node has been successfully identified above
            if (startNode.hasProperty("type") && endNode.hasProperty("type")) {
                final String relationshipType = rel.getType().name();
                final String startNodeType = (String) startNode.getProperty("type");
                final String endNodeType = (String) endNode.getProperty("type");
                relationships.add(new RelationshipInfo(startNodeType, endNodeType, relationshipType));
                // create combined type on imported relationship
                if (startNodeType != null && endNodeType != null) {
                    final String combinedType = getCombinedType(startNodeType, relationshipType, endNodeType);
                    logger.debug("Combined relationship type {} found for rel type {}, start node type {}, end node type {}", new Object[] { combinedType, relationshipType, startNodeType, endNodeType });
                    rel.setProperty(GraphObject.type.dbName(), combinedType);
                }
                // create ID on imported relationship
                rel.setProperty(GraphObject.id.dbName(), NodeServiceCommand.getNextUuid());
            }
        }
    });
    info("Grouping relationships..");
    // group relationships by type
    final Map<String, List<RelationshipInfo>> relTypeInfoMap = new LinkedHashMap<>();
    for (final RelationshipInfo relInfo : relationships) {
        // final String relType         = relInfo.getRelType();
        final String combinedType = getCombinedType(relInfo.getStartNodeType(), relInfo.getRelType(), relInfo.getEndNodeType());
        List<RelationshipInfo> infos = relTypeInfoMap.get(combinedType);
        if (infos == null) {
            infos = new LinkedList<>();
            relTypeInfoMap.put(combinedType, infos);
        }
        infos.add(relInfo);
    }
    info("Aggregating relationship information..");
    final List<RelationshipInfo> reducedRelationshipInfos = new ArrayList<>();
    if (Settings.InheritanceDetection.getValue()) {
        // reduce relationship infos into one
        for (final List<RelationshipInfo> infos : relTypeInfoMap.values()) {
            reducedRelationshipInfos.addAll(reduceNodeTypes(infos, reducedTypeInfoMap));
        }
    } else {
        reducedRelationshipInfos.addAll(relationships);
    }
    info("Starting with schema node creation..");
    bulkGraphOperation(SecurityContext.getSuperUserInstance(), reducedTypeInfos.iterator(), 100000, "Creating schema nodes", new BulkGraphOperation<TypeInfo>() {

        @Override
        public void handleGraphObject(SecurityContext securityContext, TypeInfo typeInfo) throws FrameworkException {
            final String type = typeInfo.getPrimaryType();
            if (!"ReferenceNode".equals(type)) {
                final Map<String, Class> props = typeInfo.getPropertySet();
                final PropertyMap propertyMap = new PropertyMap();
                // add properties
                for (final Map.Entry<String, Class> propertyEntry : props.entrySet()) {
                    final String propertyName = propertyEntry.getKey();
                    final Class propertyType = propertyEntry.getValue();
                    // handle array types differently
                    String propertyTypeName = propertyType.getSimpleName();
                    if (propertyType.isArray()) {
                        // remove "[]" from the end and append "Array" to match the appropriate parser
                        propertyTypeName = propertyTypeName.substring(0, propertyTypeName.length() - 2).concat("Array");
                    }
                    propertyMap.put(new StringProperty("_".concat(propertyName)), propertyTypeName);
                }
                // set node type which is in "name" property
                propertyMap.put(AbstractNode.name, type);
                // check if there is an existing Structr entity with the same type
                // and make the dynamic class extend the existing class if yes.
                final Class existingType = configuration.getNodeEntityClass(type);
                if (existingType != null) {
                    propertyMap.put(SchemaNode.extendsClass, existingType.getName());
                } else if (!typeInfo.getOtherTypes().isEmpty()) {
                    // only the first supertype is supported
                    propertyMap.put(SchemaNode.extendsClass, typeInfo.getSuperclass(reducedTypeInfoMap));
                }
                final SchemaNode existingNode = app.nodeQuery(SchemaNode.class).andName(type).getFirst();
                if (existingNode != null) {
                    for (final Entry<PropertyKey, Object> entry : propertyMap.entrySet()) {
                        existingNode.setProperty(entry.getKey(), entry.getValue());
                    }
                    schemaNodes.put(type, existingNode);
                } else {
                    // create schema node
                    schemaNodes.put(type, app.create(SchemaNode.class, propertyMap));
                }
            }
        }
    });
    info("Starting with schema relationship creation..");
    bulkGraphOperation(SecurityContext.getSuperUserInstance(), reducedRelationshipInfos.iterator(), 100000, "Creating schema relationships", new BulkGraphOperation<RelationshipInfo>() {

        @Override
        public void handleGraphObject(SecurityContext securityContext, RelationshipInfo template) throws FrameworkException {
            final String startNodeType = template.getStartNodeType();
            final String endNodeType = template.getEndNodeType();
            if (startNodeType != null && endNodeType != null) {
                final SchemaNode startNode = schemaNodes.get(startNodeType);
                final SchemaNode endNode = schemaNodes.get(endNodeType);
                if (startNode != null && endNode != null) {
                    final String relationshipType = template.getRelType();
                    final PropertyMap propertyMap = new PropertyMap();
                    propertyMap.put(SchemaRelationshipNode.sourceId, startNode.getUuid());
                    propertyMap.put(SchemaRelationshipNode.targetId, endNode.getUuid());
                    propertyMap.put(SchemaRelationshipNode.relationshipType, relationshipType);
                    app.create(SchemaRelationshipNode.class, propertyMap);
                } else {
                    info("Unable to create schema relationship node for {} -> {}, no schema nodes found", startNodeType, endNodeType);
                }
            }
        }
    });
    info("Starting with index rebuild..");
    // rebuild index
    app.command(BulkRebuildIndexCommand.class).execute(Collections.EMPTY_MAP);
}
Also used : StructrApp(org.structr.core.app.StructrApp) App(org.structr.core.app.App) LinkedHashSet(java.util.LinkedHashSet) ConfigurationProvider(org.structr.schema.ConfigurationProvider) Node(org.structr.api.graph.Node) AbstractNode(org.structr.core.entity.AbstractNode) SchemaRelationshipNode(org.structr.core.entity.SchemaRelationshipNode) SchemaNode(org.structr.core.entity.SchemaNode) ArrayList(java.util.ArrayList) StructrAndSpatialPredicate(org.structr.common.StructrAndSpatialPredicate) StringProperty(org.structr.core.property.StringProperty) LinkedHashMap(java.util.LinkedHashMap) Entry(java.util.Map.Entry) SchemaRelationshipNode(org.structr.core.entity.SchemaRelationshipNode) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) List(java.util.List) Tx(org.structr.core.graph.Tx) FrameworkException(org.structr.common.error.FrameworkException) DatabaseService(org.structr.api.DatabaseService) LinkedList(java.util.LinkedList) BulkRebuildIndexCommand(org.structr.core.graph.BulkRebuildIndexCommand) SchemaNode(org.structr.core.entity.SchemaNode) PropertyMap(org.structr.core.property.PropertyMap) Relationship(org.structr.api.graph.Relationship) SecurityContext(org.structr.common.SecurityContext) GraphObject(org.structr.core.GraphObject) LinkedHashMap(java.util.LinkedHashMap) PropertyMap(org.structr.core.property.PropertyMap) Map(java.util.Map)

Example 79 with PropertyMap

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

the class TypeResource method createNode.

public NodeInterface createNode(final Map<String, Object> propertySet) throws FrameworkException {
    if (entityClass != null) {
        // experimental: instruct deserialization strategies to set properties on related nodes
        securityContext.setAttribute("setNestedProperties", true);
        final App app = StructrApp.getInstance(securityContext);
        final PropertyMap properties = PropertyMap.inputTypeToJavaType(securityContext, entityClass, propertySet);
        return app.create(entityClass, properties);
    }
    throw new NotFoundException("Type " + rawType + " does not exist");
}
Also used : StructrApp(org.structr.core.app.StructrApp) App(org.structr.core.app.App) PropertyMap(org.structr.core.property.PropertyMap) NotFoundException(org.structr.rest.exception.NotFoundException)

Example 80 with PropertyMap

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

the class AbstractNode method getDynamicProperties.

@Override
public PropertyMap getDynamicProperties() {
    final PropertyMap propertyMap = new PropertyMap();
    final Class type = getClass();
    for (final PropertyKey key : StructrApp.getConfiguration().getPropertySet(type, PropertyView.All)) {
        // include all dynamic keys in definition
        if (key.isDynamic() || key.isCMISProperty()) {
            // only include primitives here
            final PropertyType dataType = key.getDataType();
            if (dataType != null) {
                propertyMap.put(key, getProperty(key));
            }
        }
    }
    return propertyMap;
}
Also used : PropertyMap(org.structr.core.property.PropertyMap) PropertyType(org.apache.chemistry.opencmis.commons.enums.PropertyType) PropertyKey(org.structr.core.property.PropertyKey)

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