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());
}
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");
}
}
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");
}
}
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");
}
}
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");
}
}
Aggregations