use of org.structr.core.Result in project structr by structr.
the class MeResource method doGet.
@Override
public Result doGet(PropertyKey sortKey, boolean sortDescending, int pageSize, int page) throws FrameworkException {
Principal user = securityContext.getUser(true);
if (user != null) {
List<GraphObject> resultList = new LinkedList<>();
resultList.add(user);
return new Result(resultList, null, isCollectionResource(), isPrimitiveArray());
} else {
throw new NotAllowedException("No user");
}
}
use of org.structr.core.Result in project structr by structr.
the class SchemaResource method getSchemaOverviewResult.
// ----- public static methods -----
public static Result getSchemaOverviewResult() throws FrameworkException {
final List<GraphObjectMap> resultList = new LinkedList<>();
final ConfigurationProvider config = StructrApp.getConfiguration();
// extract types from ModuleService
final Set<String> nodeEntityKeys = config.getNodeEntities().keySet();
final Set<String> relEntityKeys = config.getRelationshipEntities().keySet();
Set<String> entityKeys = new HashSet<>();
entityKeys.addAll(nodeEntityKeys);
entityKeys.addAll(relEntityKeys);
for (String rawType : entityKeys) {
// create & add schema information
Class type = SchemaHelper.getEntityClassForRawType(rawType);
GraphObjectMap schema = new GraphObjectMap();
resultList.add(schema);
if (type != null) {
String url = "/".concat(rawType);
final boolean isRel = AbstractRelationship.class.isAssignableFrom(type);
schema.setProperty(urlProperty, url);
schema.setProperty(typeProperty, type.getSimpleName());
schema.setProperty(nameProperty, type.getSimpleName());
schema.setProperty(classNameProperty, type.getName());
schema.setProperty(extendsClassNameProperty, type.getSuperclass().getName());
schema.setProperty(isRelProperty, isRel);
schema.setProperty(flagsProperty, SecurityContext.getResourceFlags(rawType));
if (!isRel) {
final List<GraphObjectMap> relatedTo = new LinkedList<>();
final List<GraphObjectMap> relatedFrom = new LinkedList<>();
for (final PropertyKey key : config.getPropertySet(type, PropertyView.All)) {
if (key instanceof RelationProperty) {
final RelationProperty relationProperty = (RelationProperty) key;
final Relation relation = relationProperty.getRelation();
if (!relation.isHidden()) {
switch(relation.getDirectionForType(type)) {
case OUTGOING:
relatedTo.add(relationPropertyToMap(config, relationProperty));
break;
case INCOMING:
relatedFrom.add(relationPropertyToMap(config, relationProperty));
break;
case BOTH:
relatedTo.add(relationPropertyToMap(config, relationProperty));
relatedFrom.add(relationPropertyToMap(config, relationProperty));
break;
}
}
}
}
if (!relatedTo.isEmpty()) {
schema.setProperty(relatedToProperty, relatedTo);
}
if (!relatedFrom.isEmpty()) {
schema.setProperty(relatedFromProperty, relatedFrom);
}
}
}
}
return new Result(resultList, resultList.size(), false, false);
}
use of org.structr.core.Result in project structr by structr.
the class StaticRelationshipResource method doGet.
// ~--- methods --------------------------------------------------------
@Override
public Result doGet(final PropertyKey sortKey, final boolean sortDescending, final int pageSize, final int page) throws FrameworkException {
// ok, source node exists, fetch it
final GraphObject sourceEntity = typedIdResource.getEntity();
if (sourceEntity != null) {
// first try: look through existing relations
if (propertyKey == null) {
if (sourceEntity instanceof NodeInterface) {
if (!typeResource.isNode) {
final NodeInterface source = (NodeInterface) sourceEntity;
final Node sourceNode = source.getNode();
final Class relationshipType = typeResource.entityClass;
final Relation relation = AbstractNode.getRelationshipForType(relationshipType);
final Class destNodeType = relation.getOtherType(typedIdResource.getEntityClass());
final Set partialResult = new LinkedHashSet<>(typeResource.doGet(sortKey, sortDescending, NodeFactory.DEFAULT_PAGE_SIZE, NodeFactory.DEFAULT_PAGE).getResults());
// filter list according to end node type
final Set<GraphObject> set = Iterables.toSet(Iterables.filter(new OtherNodeTypeRelationFilter(securityContext, sourceNode, destNodeType), source.getRelationships(relationshipType)));
// intersect partial result with result list
set.retainAll(partialResult);
final List<GraphObject> finalResult = new LinkedList<>(set);
// sort after merge
applyDefaultSorting(finalResult, sortKey, sortDescending);
// return result
return new Result(PagingHelper.subList(finalResult, pageSize, page), finalResult.size(), isCollectionResource(), isPrimitiveArray());
} else {
// what here?
throw new NotFoundException("Cannot access relationship collection " + typeResource.getRawType());
}
}
} else {
Query query = typeResource.query;
if (query == null) {
query = StructrApp.getInstance(securityContext).nodeQuery();
}
// use search context from type resource
typeResource.collectSearchAttributes(query);
final Predicate<GraphObject> predicate = query.toPredicate();
final Object value = sourceEntity.getProperty(propertyKey, predicate);
if (value != null) {
if (value instanceof Iterable) {
final Set<Object> propertyResults = new LinkedHashSet<>();
Iterator<Object> iter = ((Iterable<Object>) value).iterator();
boolean iterableContainsGraphObject = false;
while (iter.hasNext()) {
Object obj = iter.next();
propertyResults.add(obj);
if (obj != null && !iterableContainsGraphObject) {
if (obj instanceof GraphObject) {
iterableContainsGraphObject = true;
}
}
}
int rawResultCount = propertyResults.size();
if (rawResultCount > 0 && !iterableContainsGraphObject) {
GraphObjectMap gObject = new GraphObjectMap();
gObject.setProperty(new ArrayProperty(this.typeResource.rawType, Object.class), propertyResults.toArray());
Result r = new Result(gObject, true);
r.setRawResultCount(rawResultCount);
return r;
}
final List<GraphObject> finalResult = new LinkedList<>();
propertyResults.forEach(v -> finalResult.add((GraphObject) v));
applyDefaultSorting(finalResult, sortKey, sortDescending);
// return result
Result r = new Result(PagingHelper.subList(finalResult, pageSize, page), finalResult.size(), isCollectionResource(), isPrimitiveArray());
r.setRawResultCount(rawResultCount);
return r;
} else if (value instanceof GraphObject) {
return new Result((GraphObject) value, isPrimitiveArray());
} else {
GraphObjectMap gObject = new GraphObjectMap();
PropertyKey key;
String keyName = this.typeResource.rawType;
int resultCount = 1;
// FIXME: Dynamically resolve all property types and their result count
if (value instanceof String) {
key = new StringProperty(keyName);
} else if (value instanceof Integer) {
key = new IntProperty(keyName);
} else if (value instanceof Long) {
key = new LongProperty(keyName);
} else if (value instanceof Double) {
key = new DoubleProperty(keyName);
} else if (value instanceof Boolean) {
key = new BooleanProperty(keyName);
} else if (value instanceof Date) {
key = new DateProperty(keyName);
} else if (value instanceof String[]) {
key = new ArrayProperty(keyName, String.class);
resultCount = ((String[]) value).length;
} else {
key = new GenericProperty(keyName);
}
gObject.setProperty(key, value);
Result r = new Result(gObject, true);
r.setRawResultCount(resultCount);
return r;
}
}
// check propertyKey to return the right variant of empty result
if (!(propertyKey instanceof StartNode || propertyKey instanceof EndNode)) {
return new Result(Collections.EMPTY_LIST, 1, false, true);
}
}
}
return new Result(Collections.EMPTY_LIST, 0, false, true);
}
use of org.structr.core.Result in project structr by structr.
the class LogResource method histogram.
private Result histogram(final LogState state) throws FrameworkException {
// sort entries before creating the histogram
state.sortEntries();
final String dateFormat = state.aggregate();
final long startTimestamp = state.beginTimestamp();
final long endTimestamp = state.endTimestamp();
final GraphObjectMap result = new GraphObjectMap();
final long interval = findInterval(dateFormat);
final long start = alignDateOnFormat(dateFormat, startTimestamp);
final TreeMap<Long, Map<String, Object>> countMap = toHistogramCountMap(state);
final Set<String> countProperties = getCountProperties(countMap);
for (long current = start; current <= endTimestamp; current += interval) {
final Map<Long, Map<String, Object>> counts = countMap.subMap(current, true, current + interval, false);
final GraphObjectMap sum = new GraphObjectMap();
// whether there are actual values or not)
for (final String key : countProperties) {
sum.put(new IntProperty(key), 0);
}
// evaluate counts
for (final Map<String, Object> count : counts.values()) {
for (final String key : countProperties) {
final IntProperty prop = new IntProperty(key);
Integer sumValue = sum.get(prop);
if (sumValue == null) {
sumValue = 0;
}
Integer entryValue = (Integer) count.get(key);
if (entryValue == null) {
entryValue = 0;
}
sum.put(prop, sumValue + entryValue);
}
}
result.put(new GenericProperty(Long.toString(current)), sum);
}
return new Result(result, false);
}
use of org.structr.core.Result in project structr by structr.
the class LogResource method aggregate.
private Result aggregate(final LogState state) throws FrameworkException {
// sort entries before aggregation
state.sortEntries();
final long startTimestamp = state.beginTimestamp();
final long endTimestamp = state.endTimestamp();
final GraphObjectMap result = new GraphObjectMap();
final long interval = findInterval(state.aggregate());
final long start = alignDateOnFormat(state.aggregate(), startTimestamp);
final TreeMap<Long, Map<String, Object>> countMap = toAggregatedCountMap(state);
final Set<String> countProperties = getCountProperties(countMap);
for (long current = start; current <= endTimestamp; current += interval) {
final Map<Long, Map<String, Object>> counts = countMap.subMap(current, true, current + interval, false);
final GraphObjectMap sum = new GraphObjectMap();
// whether there are actual values or not)
for (final String key : countProperties) {
sum.put(new IntProperty(key), 0);
}
// evaluate counts
for (final Map<String, Object> count : counts.values()) {
for (final String key : countProperties) {
final IntProperty prop = new IntProperty(key);
Integer sumValue = sum.get(prop);
if (sumValue == null) {
sumValue = 0;
}
Integer entryValue = (Integer) count.get(key);
if (entryValue == null) {
entryValue = 0;
}
sum.put(prop, sumValue + entryValue);
}
}
result.put(new GenericProperty(Long.toString(current)), sum);
}
return new Result(result, false);
}
Aggregations