use of org.structr.core.GraphObject in project structr by structr.
the class DOMNode method render.
static void render(final DOMNode thisNode, final RenderContext renderContext, final int depth) throws FrameworkException {
final SecurityContext securityContext = renderContext.getSecurityContext();
if (!securityContext.isVisible(thisNode)) {
return;
}
final GraphObject details = renderContext.getDetailsDataObject();
final boolean detailMode = details != null;
if (detailMode && thisNode.hideOnDetail()) {
return;
}
if (!detailMode && thisNode.hideOnIndex()) {
return;
}
final EditMode editMode = renderContext.getEditMode(securityContext.getUser(false));
if (EditMode.RAW.equals(editMode) || EditMode.WIDGET.equals(editMode) || EditMode.DEPLOYMENT.equals(editMode)) {
thisNode.renderContent(renderContext, depth);
} else {
final String subKey = thisNode.getDataKey();
if (StringUtils.isNotBlank(subKey)) {
final GraphObject currentDataNode = renderContext.getDataObject();
// fetch (optional) list of external data elements
final Iterable<GraphObject> listData = checkListSources(thisNode, securityContext, renderContext);
final PropertyKey propertyKey;
if (thisNode.renderDetails() && detailMode) {
renderContext.setDataObject(details);
renderContext.putDataObject(subKey, details);
thisNode.renderContent(renderContext, depth);
} else {
if (Iterables.isEmpty(listData) && currentDataNode != null) {
// There are two alternative ways of retrieving sub elements:
// First try to get generic properties,
// if that fails, try to create a propertyKey for the subKey
final Object elements = currentDataNode.getProperty(new GenericProperty(subKey));
renderContext.setRelatedProperty(new GenericProperty(subKey));
renderContext.setSourceDataObject(currentDataNode);
if (elements != null) {
if (elements instanceof Iterable) {
for (Object o : (Iterable) elements) {
if (o instanceof GraphObject) {
GraphObject graphObject = (GraphObject) o;
renderContext.putDataObject(subKey, graphObject);
thisNode.renderContent(renderContext, depth);
}
}
}
} else {
propertyKey = StructrApp.getConfiguration().getPropertyKeyForJSONName(currentDataNode.getClass(), subKey, false);
renderContext.setRelatedProperty(propertyKey);
if (propertyKey != null) {
final Object value = currentDataNode.getProperty(propertyKey);
if (value != null) {
if (value instanceof Iterable) {
for (final Object o : ((Iterable) value)) {
if (o instanceof GraphObject) {
renderContext.putDataObject(subKey, (GraphObject) o);
thisNode.renderContent(renderContext, depth);
}
}
}
}
}
}
// reset data node in render context
renderContext.setDataObject(currentDataNode);
renderContext.setRelatedProperty(null);
} else {
renderContext.setListSource(listData);
thisNode.renderNodeList(securityContext, renderContext, depth, subKey);
}
}
} else {
thisNode.renderContent(renderContext, depth);
}
}
}
use of org.structr.core.GraphObject in project structr by structr.
the class ValidationHelper method isValidGloballyUniqueProperty.
public static synchronized boolean isValidGloballyUniqueProperty(final GraphObject object, final PropertyKey key, final ErrorBuffer errorBuffer) {
if (key != null) {
final Object value = object.getProperty(key);
List<? extends GraphObject> result = null;
try {
if (object instanceof NodeInterface) {
result = StructrApp.getInstance().nodeQuery(NodeInterface.class).and(key, value).disableSorting().getAsList();
} else if (object instanceof RelationshipInterface) {
result = StructrApp.getInstance().relationshipQuery(RelationshipInterface.class).and(key, value).disableSorting().getAsList();
} else {
logger.error("GraphObject is neither NodeInterface nor RelationshipInterface");
return false;
}
} catch (FrameworkException fex) {
logger.warn("Unable to fetch list of nodes for uniqueness check", fex);
// handle error
}
if (result != null) {
for (final GraphObject foundNode : result) {
if (foundNode.getId() != object.getId()) {
// validation is aborted when the first validation failure occurs, so
// we can assume that the object currently exmained is the first
// existing object, hence all others get the error message with the
// UUID of the first one.
errorBuffer.add(new UniqueToken(object.getType(), key, object.getUuid()));
// error!
return false;
}
}
}
}
// no error
return true;
}
use of org.structr.core.GraphObject in project structr by structr.
the class SecurityContext method removeForbiddenNodes.
public void removeForbiddenNodes(List<? extends GraphObject> nodes, final boolean includeDeletedAndHidden, final boolean publicOnly) {
boolean readableByUser = false;
for (Iterator<? extends GraphObject> it = nodes.iterator(); it.hasNext(); ) {
GraphObject obj = it.next();
if (obj instanceof AbstractNode) {
AbstractNode n = (AbstractNode) obj;
readableByUser = n.isGranted(Permission.read, this);
if (!(readableByUser && (includeDeletedAndHidden || !n.isDeleted()) && (n.isVisibleToPublicUsers() || !publicOnly))) {
it.remove();
}
}
}
}
use of org.structr.core.GraphObject in project structr by structr.
the class PropertyMap method javaTypeToDatabaseType.
public static Map<String, Object> javaTypeToDatabaseType(SecurityContext securityContext, Class<? extends GraphObject> entity, PropertyMap properties) throws FrameworkException {
Map<String, Object> databaseTypedProperties = new LinkedHashMap<>();
for (Entry<PropertyKey, Object> entry : properties.entrySet()) {
PropertyKey propertyKey = entry.getKey();
PropertyConverter converter = propertyKey.databaseConverter(securityContext);
if (converter != null) {
try {
Object propertyValue = converter.convert(entry.getValue());
databaseTypedProperties.put(propertyKey.jsonName(), propertyValue);
} catch (ClassCastException cce) {
throw new FrameworkException(422, "Invalid JSON input for key " + propertyKey.jsonName() + ", expected a JSON " + propertyKey.typeName() + ".");
}
} else {
databaseTypedProperties.put(propertyKey.jsonName(), entry.getValue());
}
}
return databaseTypedProperties;
}
use of org.structr.core.GraphObject in project structr by structr.
the class PropertyMap method inputTypeToJavaType.
public static PropertyMap inputTypeToJavaType(final SecurityContext securityContext, Class<? extends GraphObject> entity, final Map<String, Object> source) throws FrameworkException {
final String batchType = securityContext.getAttribute("batchType", "__");
if (batchType.equals(source.get("type"))) {
// only to batching if a type is set for which batch is enable
final Integer count = securityContext.getAttribute("objectCount", 0);
final Integer overall = securityContext.getAttribute("overallCount", 0);
securityContext.setAttribute("objectCount", count + 1);
securityContext.setAttribute("overallCount", overall + 1);
if (count == 100) {
final Tx tx = (Tx) securityContext.getAttribute("currentTransaction");
if (tx != null) {
logger.info("Committing batch transaction after {} objects of type {}.", overall, batchType);
// try to commit this batch
tx.success();
tx.close();
// open new transaction and store it in context
securityContext.setAttribute("currentTransaction", StructrApp.getInstance(securityContext).tx());
securityContext.setAttribute("objectCount", 0);
}
}
}
PropertyMap resultMap = new PropertyMap();
if (source != null) {
// caution, source can be null when an empty nested property group is encountered!
for (final Entry<String, Object> entry : source.entrySet()) {
String key = entry.getKey();
Object value = entry.getValue();
if (key != null) {
final PropertyKey propertyKey = StructrApp.getConfiguration().getPropertyKeyForJSONName(entity, key);
if (propertyKey != null) {
final PropertyConverter converter = propertyKey.inputConverter(securityContext);
if (converter != null && value != null && !propertyKey.valueType().isAssignableFrom(value.getClass())) {
try {
// test
converter.setContext(source);
Object propertyValue = converter.convert(value);
resultMap.put(propertyKey, propertyValue);
} catch (ClassCastException cce) {
logger.warn("", cce);
throw new FrameworkException(422, "Invalid JSON input for key " + propertyKey.jsonName() + ", expected a JSON " + propertyKey.typeName() + ".");
}
} else {
resultMap.put(propertyKey, value);
}
}
}
}
}
return resultMap;
}
Aggregations