use of org.structr.core.app.Query in project structr by structr.
the class GraphQLQuery method getEntities.
public Iterable<GraphObject> getEntities(final SecurityContext securityContext) throws FrameworkException {
final Class type = StructrApp.getConfiguration().getNodeEntityClass(fieldName);
final Query query = StructrApp.getInstance(securityContext).nodeQuery(type);
final QueryConfig config = getConfig(getRootPath());
config.configureQuery(query);
return query.getAsList();
}
use of org.structr.core.app.Query in project structr by structr.
the class PrivilegedFindFunction method apply.
@Override
public Object apply(final ActionContext ctx, final Object caller, Object[] sources) throws FrameworkException {
if (sources != null) {
final SecurityContext securityContext = SecurityContext.getSuperUserInstance();
final ConfigurationProvider config = StructrApp.getConfiguration();
final Query query = StructrApp.getInstance(securityContext).nodeQuery().sort(GraphObject.createdDate).order(false);
// the type to query for
Class type = null;
if (sources.length >= 1 && sources[0] != null) {
final String typeString = sources[0].toString();
type = config.getNodeEntityClass(typeString);
if (type != null) {
query.andTypes(type);
} else {
logger.warn("Error in find_privileged(): type \"{}\" not found.", typeString);
return "Error in find_privileged(): type " + typeString + " not found.";
}
}
// exit gracefully instead of crashing..
if (type == null) {
logger.warn("Error in find_privileged(): no type specified. Parameters: {}", getParametersAsString(sources));
return "Error in find_privileged(): no type specified.";
}
// experimental: disable result count, prevents instantiation
// of large collections just for counting all the objects..
securityContext.ignoreResultCount(true);
// extension for native javascript objects
if (sources.length == 2 && sources[1] instanceof Map) {
query.and(PropertyMap.inputTypeToJavaType(securityContext, type, (Map) sources[1]));
} else if (sources.length == 2) {
if (sources[1] == null) {
throw new IllegalArgumentException();
}
// special case: second parameter is a UUID
final PropertyKey key = StructrApp.key(type, "id");
query.and(key, sources[1].toString());
final int resultCount = query.getResult().size();
switch(resultCount) {
case 1:
return query.getFirst();
case 0:
return null;
default:
throw new FrameworkException(400, "Multiple Objects found for id! [" + sources[1].toString() + "]");
}
} else {
final int parameter_count = sources.length;
if (parameter_count % 2 == 0) {
throw new FrameworkException(400, "Invalid number of parameters: " + parameter_count + ". Should be uneven: " + ERROR_MESSAGE_PRIVILEGEDFIND);
}
for (int c = 1; c < parameter_count; c += 2) {
final PropertyKey key = StructrApp.key(type, sources[c].toString());
if (key != null) {
final PropertyConverter inputConverter = key.inputConverter(securityContext);
Object value = sources[c + 1];
if (inputConverter != null) {
value = inputConverter.convert(value);
}
query.and(key, value);
}
}
}
return query.getAsList();
}
return "";
}
use of org.structr.core.app.Query in project structr by structr.
the class SearchAndSortingTest method testPaging.
// ----- private methods -----
private void testPaging(final Class type, final int pageSize, final int page, final int number, final int offset, final boolean includeDeletedAndHidden, final boolean publicOnly, final PropertyKey sortKey, final boolean sortDesc) throws FrameworkException {
final Query query = app.nodeQuery(type).sort(sortKey).order(sortDesc).page(page).pageSize(pageSize);
if (includeDeletedAndHidden) {
query.includeDeletedAndHidden();
}
final Result result = query.getResult();
logger.info("===================================================\nRaw result size: {}, expected: {} (page size: {}, page: {})", new Object[] { result.getRawResultCount(), number, pageSize, page });
assertTrue(result.getRawResultCount() == ((pageSize == 0 || page == 0) ? 0 : number));
long expectedResultCount = (pageSize == 0 || page == 0) ? 0 : Math.min(number, pageSize);
int startIndex = (Math.max(page, 1) - 1) * pageSize;
logger.info("Result size: {}, expected: {}, start index: {}", new Object[] { result.size(), expectedResultCount, startIndex });
assertTrue(result.size() == expectedResultCount);
for (int j = 0; j < expectedResultCount; j++) {
String expectedName = "TestOne-" + (offset + j + startIndex);
String gotName = result.get(j).getProperty(AbstractNode.name);
System.out.println(expectedName + ", got: " + gotName);
assertEquals(expectedName, gotName);
}
}
use of org.structr.core.app.Query in project structr by structr.
the class ListCommand method processMessage.
@Override
public void processMessage(final WebSocketMessage webSocketData) {
final SecurityContext securityContext = getWebSocket().getSecurityContext();
final Map<String, Object> nodeData = webSocketData.getNodeData();
final String rawType = (String) nodeData.get("type");
final String properties = (String) webSocketData.getNodeData().get("properties");
final boolean rootOnly = Boolean.TRUE.equals((Boolean) nodeData.get("rootOnly"));
Class type = SchemaHelper.getEntityClassForRawType(rawType);
if (type == null) {
getWebSocket().send(MessageBuilder.status().code(404).message("Type " + rawType + " not found").build(), true);
return;
}
if (properties != null) {
securityContext.setCustomView(StringUtils.split(properties, ","));
}
final String sortOrder = webSocketData.getSortOrder();
final String sortKey = webSocketData.getSortKey();
final int pageSize = webSocketData.getPageSize();
final int page = webSocketData.getPage();
final PropertyKey sortProperty = StructrApp.key(type, sortKey);
final Query query = StructrApp.getInstance(securityContext).nodeQuery(type).sort(sortProperty).order("desc".equals(sortOrder)).page(page).pageSize(pageSize);
if (File.class.isAssignableFrom(type)) {
if (rootOnly) {
query.and(StructrApp.key(File.class, "hasParent"), false);
}
// inverted as isThumbnail is not necessarily present in all objects inheriting from FileBase
query.not().and(StructrApp.key(Image.class, "isThumbnail"), true);
}
// important
if (Folder.class.isAssignableFrom(type) && rootOnly) {
query.and(StructrApp.key(Folder.class, "hasParent"), false);
}
try {
// do search
final Result result = query.getResult();
// save raw result count
// filteredResults.size();
int resultCountBeforePaging = result.getRawResultCount();
// set full result list
webSocketData.setResult(result.getResults());
webSocketData.setRawResultCount(resultCountBeforePaging);
// send only over local connection
getWebSocket().send(webSocketData, true);
} catch (FrameworkException fex) {
logger.warn("Exception occured", fex);
getWebSocket().send(MessageBuilder.status().code(fex.getStatus()).message(fex.getMessage()).build(), true);
}
}
use of org.structr.core.app.Query in project structr by structr.
the class ListUnattachedNodesCommand method getUnattachedNodes.
/**
* Return list of nodes which are not attached to a page and have no
* parent element (no incoming CONTAINS rel)
*
* @param app
* @param securityContext
* @param webSocketData
* @return
* @throws FrameworkException
*/
protected static List<NodeInterface> getUnattachedNodes(final App app, final SecurityContext securityContext, final WebSocketMessage webSocketData) throws FrameworkException {
final String sortOrder = webSocketData.getSortOrder();
final String sortKey = webSocketData.getSortKey();
Query query;
if (sortKey != null) {
final PropertyKey sortProperty = StructrApp.key(DOMNode.class, sortKey);
query = StructrApp.getInstance(securityContext).nodeQuery().includeDeletedAndHidden().sort(sortProperty).order("desc".equals(sortOrder));
} else {
query = StructrApp.getInstance(securityContext).nodeQuery().includeDeletedAndHidden();
}
query.orTypes(DOMElement.class);
query.orType(Content.class);
query.orType(Template.class);
// do search
final List<NodeInterface> filteredResults = new LinkedList();
List<? extends GraphObject> resultList = null;
try (final Tx tx = app.tx()) {
resultList = query.getAsList();
tx.success();
} catch (FrameworkException fex) {
logger.warn("Exception occured", fex);
}
if (resultList != null) {
// determine which of the nodes have no incoming CONTAINS relationships and no page id
for (GraphObject obj : resultList) {
if (obj instanceof DOMNode) {
DOMNode node = (DOMNode) obj;
if (!node.hasIncomingRelationships(node.getChildLinkType()) && node.getOwnerDocument() == null && !(node instanceof ShadowDocument)) {
filteredResults.add(node);
}
}
}
}
return filteredResults;
}
Aggregations