Search in sources :

Example 41 with Result

use of org.structr.core.Result in project structr by structr.

the class TypeResource method doGet.

@Override
public Result doGet(final PropertyKey sortKey, final boolean sortDescending, final int pageSize, final int page) throws FrameworkException {
    boolean includeDeletedAndHidden = true;
    boolean publicOnly = false;
    PropertyKey actualSortKey = sortKey;
    boolean actualSortOrder = sortDescending;
    if (rawType != null) {
        if (entityClass == null) {
            throw new NotFoundException("Type " + rawType + " does not exist");
        }
        collectSearchAttributes(query);
        // default sort key & order
        if (actualSortKey == null) {
            try {
                GraphObject templateEntity = ((GraphObject) entityClass.newInstance());
                PropertyKey sortKeyProperty = templateEntity.getDefaultSortKey();
                actualSortOrder = GraphObjectComparator.DESCENDING.equals(templateEntity.getDefaultSortOrder());
                if (sortKeyProperty != null) {
                    actualSortKey = sortKeyProperty;
                } else {
                    actualSortKey = AbstractNode.name;
                }
            } catch (Throwable t) {
                // fallback to name
                actualSortKey = AbstractNode.name;
            }
        }
        if (virtualType != null) {
            final Result untransformedResult = query.includeDeletedAndHidden(includeDeletedAndHidden).publicOnly(publicOnly).sort(actualSortKey).order(actualSortOrder).getResult();
            final Result result = virtualType.transformOutput(securityContext, entityClass, untransformedResult);
            return PagingHelper.subResult(result, pageSize, page);
        } else {
            return query.includeDeletedAndHidden(includeDeletedAndHidden).publicOnly(publicOnly).sort(actualSortKey).order(actualSortOrder).pageSize(pageSize).page(page).getResult();
        }
    } else {
        logger.warn("type was null");
    }
    List emptyList = Collections.emptyList();
    return new Result(emptyList, null, isCollectionResource(), isPrimitiveArray());
}
Also used : NotFoundException(org.structr.rest.exception.NotFoundException) List(java.util.List) GraphObject(org.structr.core.GraphObject) PropertyKey(org.structr.core.property.PropertyKey) Result(org.structr.core.Result) RestMethodResult(org.structr.rest.RestMethodResult)

Example 42 with Result

use of org.structr.core.Result in project structr by structr.

the class SearchAndSortingTest method test01SearchSingleNodeByName.

@Test
public void test01SearchSingleNodeByName() {
    try {
        PropertyMap props = new PropertyMap();
        final PropertyKey key = AbstractNode.name;
        final String name = "89w3hkl sdfghsdkljth";
        props.put(key, name);
        final AbstractNode node = createTestNode(TestOne.class, props);
        Result result = null;
        try (final Tx tx = app.tx()) {
            result = app.nodeQuery(TestOne.class).andName(name).includeDeletedAndHidden().getResult();
            assertTrue(result.size() == 1);
            assertTrue(result.get(0).equals(node));
        }
        // Change name attribute and search again
        final String name2 = "klppptzoehi gösoiu tzüw0e9hg";
        try (final Tx tx = app.tx()) {
            node.setProperty(key, name2);
            tx.success();
        }
        try (final Tx tx = app.tx()) {
            result = app.nodeQuery(TestOne.class).andName(name2).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) PropertyKey(org.structr.core.property.PropertyKey) Result(org.structr.core.Result) Test(org.junit.Test)

Example 43 with Result

use of org.structr.core.Result in project structr by structr.

the class SearchAndSortingTest method test06SortByDateWitNullValues.

@Test
public void test06SortByDateWitNullValues() {
    try {
        Class type = TestOne.class;
        int number = 20;
        final List<NodeInterface> nodes = this.createTestNodes(type, number);
        final int offset = 10;
        Collections.shuffle(nodes, new Random(System.nanoTime()));
        int i = offset;
        String name;
        try (final Tx tx = app.tx()) {
            for (NodeInterface node : nodes) {
                name = Integer.toString(i);
                i++;
                node.setProperty(AbstractNode.name, "TestOne-" + name);
                if ((i % 2) != 0) {
                    node.setProperty(TestOne.aDate, new Date());
                    System.out.println("TestOne-" + name + ": indexed with date");
                } else {
                    node.setProperty(TestOne.aDate, null);
                    System.out.println("TestOne-" + name + ": null date");
                }
                // slow down execution speed to make sure distinct changes fall in different milliseconds
                try {
                    Thread.sleep(2);
                } catch (Throwable t) {
                }
            }
            tx.success();
        }
        try (final Tx tx = app.tx()) {
            Result result = app.nodeQuery(type).getResult();
            assertEquals(number, result.size());
            PropertyKey sortKey = TestOne.aDate;
            boolean sortDesc = true;
            int pageSize = 10;
            int page = 1;
            result = app.nodeQuery(type).sort(sortKey).order(sortDesc).page(page).pageSize(pageSize).getResult();
            logger.info("Raw result size: {}, expected: {}", new Object[] { result.getRawResultCount(), number });
            assertTrue(result.getRawResultCount() == number);
            logger.info("Result size: {}, expected: {}", new Object[] { result.size(), pageSize });
            assertTrue(result.size() == Math.min(number, pageSize));
            for (int j = 0; j < Math.min(result.size(), pageSize); j++) {
                String expectedName = "TestOne-" + (30 - (j + 1) * 2);
                String gotName = result.get(j).getProperty(AbstractNode.name);
                System.out.println(j + ": " + expectedName + ", got: " + gotName);
                assertEquals(expectedName, gotName);
            }
            tx.success();
        }
    } catch (FrameworkException ex) {
        logger.error(ex.toString());
        fail("Unexpected exception");
    }
}
Also used : Tx(org.structr.core.graph.Tx) FrameworkException(org.structr.common.error.FrameworkException) Date(java.util.Date) Result(org.structr.core.Result) Random(java.util.Random) TestOne(org.structr.core.entity.TestOne) NodeInterface(org.structr.core.graph.NodeInterface) PropertyKey(org.structr.core.property.PropertyKey) Test(org.junit.Test)

Example 44 with Result

use of org.structr.core.Result in project structr by structr.

the class SearchAndSortingTest method test06PagingVisibility.

@Test
public void test06PagingVisibility() {
    Principal tester1 = null;
    Principal tester2 = null;
    try (final Tx tx = app.tx()) {
        // create non-admin user
        tester1 = app.create(Principal.class, "tester1");
        tester2 = app.create(Principal.class, "tester2");
        tx.success();
    } catch (FrameworkException fex) {
        logger.warn("", fex);
        fail("Unexpected exception");
    }
    try {
        final SecurityContext tester1Context = SecurityContext.getInstance(tester1, AccessMode.Backend);
        final SecurityContext tester2Context = SecurityContext.getInstance(tester2, AccessMode.Backend);
        final App tester1App = StructrApp.getInstance(tester1Context);
        final App tester2App = StructrApp.getInstance(tester2Context);
        final Class type = TestOne.class;
        final int number = 1000;
        final List<NodeInterface> allNodes = this.createTestNodes(type, number);
        final List<NodeInterface> tester1Nodes = new LinkedList<>();
        final List<NodeInterface> tester2Nodes = new LinkedList<>();
        final int offset = 0;
        try (final Tx tx = app.tx()) {
            int i = offset;
            for (NodeInterface node : allNodes) {
                // System.out.println("Node ID: " + node.getNodeId());
                String _name = "TestOne-" + StringUtils.leftPad(Integer.toString(i), 5, "0");
                final double rand = Math.random();
                if (rand < 0.3) {
                    node.setProperty(NodeInterface.owner, tester1);
                    tester1Nodes.add(node);
                } else if (rand < 0.6) {
                    node.setProperty(NodeInterface.owner, tester2);
                    tester2Nodes.add(node);
                }
                i++;
                node.setProperty(AbstractNode.name, _name);
            }
            tx.success();
        }
        final int tester1NodeCount = tester1Nodes.size();
        final int tester2NodeCount = tester2Nodes.size();
        try (final Tx tx = app.tx()) {
            final PropertyKey sortKey = AbstractNode.name;
            final boolean sortDesc = false;
            final int pageSize = 10;
            final int page = 22;
            final Result result = tester1App.nodeQuery(type).sort(sortKey).order(sortDesc).pageSize(pageSize).page(page).getResult();
            assertEquals("Invalid paging result count with non-superuser security context", tester1NodeCount, (int) result.getRawResultCount());
            tx.success();
        }
        try (final Tx tx = app.tx()) {
            final PropertyKey sortKey = AbstractNode.name;
            final boolean sortDesc = false;
            final int pageSize = 10;
            final int page = 22;
            final Result result = tester2App.nodeQuery(type).sort(sortKey).order(sortDesc).pageSize(pageSize).page(page).getResult();
            assertEquals("Invalid paging result count with non-superuser security context", tester2NodeCount, (int) result.getRawResultCount());
            tx.success();
        }
    } catch (FrameworkException ex) {
        logger.error(ex.toString());
        fail("Unexpected exception");
    }
}
Also used : StructrApp(org.structr.core.app.StructrApp) App(org.structr.core.app.App) Tx(org.structr.core.graph.Tx) FrameworkException(org.structr.common.error.FrameworkException) LinkedList(java.util.LinkedList) Result(org.structr.core.Result) TestOne(org.structr.core.entity.TestOne) Principal(org.structr.core.entity.Principal) NodeInterface(org.structr.core.graph.NodeInterface) PropertyKey(org.structr.core.property.PropertyKey) Test(org.junit.Test)

Example 45 with Result

use of org.structr.core.Result in project structr by structr.

the class SearchAndSortingTest method test01FirstPage.

@Test
public void test01FirstPage() {
    try {
        Class type = TestOne.class;
        int number = 43;
        // create nodes
        this.createTestNodes(type, number);
        try (final Tx tx = app.tx()) {
            Result result = app.nodeQuery(type).getResult();
            assertTrue(result.size() == number);
            PropertyKey sortKey = AbstractNode.name;
            boolean sortDesc = false;
            int pageSize = 10;
            int page = 1;
            result = app.nodeQuery(type).includeDeletedAndHidden().sort(sortKey).order(sortDesc).page(page).pageSize(pageSize).getResult();
            logger.info("Raw result size: {}, expected: {}", new Object[] { result.getRawResultCount(), number });
            assertTrue(result.getRawResultCount() == number);
            logger.info("Result size: {}, expected: {}", new Object[] { result.size(), pageSize });
            assertTrue(result.size() == pageSize);
        }
    } catch (FrameworkException ex) {
        logger.warn("", ex);
        logger.error(ex.toString());
        fail("Unexpected exception");
    }
}
Also used : Tx(org.structr.core.graph.Tx) FrameworkException(org.structr.common.error.FrameworkException) TestOne(org.structr.core.entity.TestOne) PropertyKey(org.structr.core.property.PropertyKey) Result(org.structr.core.Result) Test(org.junit.Test)

Aggregations

Result (org.structr.core.Result)66 FrameworkException (org.structr.common.error.FrameworkException)45 Tx (org.structr.core.graph.Tx)36 Test (org.junit.Test)34 TestOne (org.structr.core.entity.TestOne)31 PropertyKey (org.structr.core.property.PropertyKey)28 PropertyMap (org.structr.core.property.PropertyMap)21 NodeInterface (org.structr.core.graph.NodeInterface)18 GraphObject (org.structr.core.GraphObject)14 AbstractNode (org.structr.core.entity.AbstractNode)12 RestMethodResult (org.structr.rest.RestMethodResult)12 Random (java.util.Random)11 SecurityContext (org.structr.common.SecurityContext)11 GraphObjectMap (org.structr.core.GraphObjectMap)10 LinkedList (java.util.LinkedList)9 Query (org.structr.core.app.Query)9 App (org.structr.core.app.App)8 StructrApp (org.structr.core.app.StructrApp)8 List (java.util.List)7 Principal (org.structr.core.entity.Principal)7