use of org.structr.core.GraphObject in project structr by structr.
the class ToExcelFunction method writeExcel.
public Workbook writeExcel(final List list, final String propertyView, final List<String> properties, final boolean includeHeader, final boolean localizeHeader, final String headerLocalizationDomain, final Locale locale) throws IOException {
final Workbook workbook = new XSSFWorkbook();
final XSSFSheet sheet = (XSSFSheet) workbook.createSheet();
int rowCount = 0;
int cellCount = 0;
XSSFRow currentRow = null;
XSSFCell cell = null;
if (includeHeader) {
currentRow = (XSSFRow) sheet.createRow(rowCount++);
cellCount = 0;
if (propertyView != null) {
final Object obj = list.get(0);
if (obj instanceof GraphObject) {
for (PropertyKey key : ((GraphObject) obj).getPropertyKeys(propertyView)) {
cell = (XSSFCell) currentRow.createCell(cellCount++);
String value = key.dbName();
if (localizeHeader) {
try {
value = LocalizeFunction.getLocalization(locale, value, headerLocalizationDomain);
} catch (FrameworkException fex) {
logger.warn("to_excel(): Exception", fex);
}
}
cell.setCellValue(value);
}
} else {
cell = (XSSFCell) currentRow.createCell(cellCount++);
cell.setCellValue("Error: Object is not of type GraphObject, can not determine properties of view for header row");
}
} else if (properties != null) {
for (final String colName : properties) {
cell = (XSSFCell) currentRow.createCell(cellCount++);
String value = colName;
if (localizeHeader) {
try {
value = LocalizeFunction.getLocalization(locale, value, headerLocalizationDomain);
} catch (FrameworkException fex) {
logger.warn("to_excel(): Exception", fex);
}
}
cell.setCellValue(value);
}
}
}
for (final Object obj : list) {
currentRow = (XSSFRow) sheet.createRow(rowCount++);
cellCount = 0;
if (propertyView != null) {
if (obj instanceof GraphObject) {
for (PropertyKey key : ((GraphObject) obj).getPropertyKeys(propertyView)) {
final Object value = ((GraphObject) obj).getProperty(key);
cell = (XSSFCell) currentRow.createCell(cellCount++);
cell.setCellValue(escapeForExcel(value));
}
} else {
cell = (XSSFCell) currentRow.createCell(cellCount++);
cell.setCellValue("Error: Object is not of type GraphObject, can not determine properties of object");
}
} else if (properties != null) {
if (obj instanceof GraphObject) {
final GraphObject castedObj = (GraphObject) obj;
for (final String colName : properties) {
final PropertyKey key = StructrApp.key(obj.getClass(), colName);
final Object value = castedObj.getProperty(key);
cell = (XSSFCell) currentRow.createCell(cellCount++);
cell.setCellValue(escapeForExcel(value));
}
} else if (obj instanceof Map) {
final Map castedObj = (Map) obj;
for (final String colName : properties) {
final Object value = castedObj.get(colName);
cell = (XSSFCell) currentRow.createCell(cellCount++);
cell.setCellValue(escapeForExcel(value));
}
}
}
}
return workbook;
}
use of org.structr.core.GraphObject in project structr by structr.
the class ToCsvFunction method apply.
@Override
public Object apply(ActionContext ctx, Object caller, Object[] sources) throws FrameworkException {
try {
if (arrayHasMinLengthAndMaxLengthAndAllElementsNotNull(sources, 2, 8)) {
if (!(sources[0] instanceof List)) {
logParameterError(caller, sources, ctx.isJavaScriptContext());
return "ERROR: First parameter must be a collection!".concat(usage(ctx.isJavaScriptContext()));
}
final List<GraphObject> nodes = (List) sources[0];
String delimiterChar = ";";
String quoteChar = "\"";
String recordSeparator = "\n";
boolean includeHeader = true;
boolean localizeHeader = false;
String headerLocalizationDomain = null;
String propertyView = null;
List<String> properties = null;
// we are using size() instead of isEmpty() because NativeArray.isEmpty() always returns true
if (nodes.size() == 0) {
logger.warn("to_csv(): Can not create CSV if no nodes are given!");
logParameterError(caller, sources, ctx.isJavaScriptContext());
return "";
}
switch(sources.length) {
case 8:
headerLocalizationDomain = (String) sources[7];
case 7:
localizeHeader = (Boolean) sources[6];
case 6:
includeHeader = (Boolean) sources[5];
case 5:
recordSeparator = (String) sources[4];
case 4:
quoteChar = (String) sources[3];
case 3:
delimiterChar = (String) sources[2];
case 2:
{
if (sources[1] instanceof String) {
// view is given
propertyView = (String) sources[1];
} else if (sources[1] instanceof List) {
// named properties are given
properties = (List) sources[1];
// we are using size() instead of isEmpty() because NativeArray.isEmpty() always returns true
if (properties.size() == 0) {
logger.warn("to_csv(): Can not create CSV if list of properties is empty!");
logParameterError(caller, sources, ctx.isJavaScriptContext());
return "";
}
} else {
logParameterError(caller, sources, ctx.isJavaScriptContext());
return "ERROR: Second parameter must be a collection of property names or a single property view!".concat(usage(ctx.isJavaScriptContext()));
}
}
}
try {
final StringWriter writer = new StringWriter();
writeCsv(nodes, writer, propertyView, properties, quoteChar.charAt(0), delimiterChar.charAt(0), recordSeparator, includeHeader, localizeHeader, headerLocalizationDomain, ctx.getLocale());
return writer.toString();
} catch (Throwable t) {
logger.warn("to_csv(): Exception occurred", t);
return "";
}
} else {
logParameterError(caller, sources, ctx.isJavaScriptContext());
return usage(ctx.isJavaScriptContext());
}
} catch (final IllegalArgumentException e) {
logParameterError(caller, sources, ctx.isJavaScriptContext());
return usage(ctx.isJavaScriptContext());
}
}
use of org.structr.core.GraphObject in project structr by structr.
the class ListRemoteSyncablesCommand method processMessage.
@Override
public void processMessage(final WebSocketMessage webSocketData) {
final Map<String, Object> properties = webSocketData.getNodeData();
final String username = (String) properties.get("username");
final String password = (String) properties.get("password");
final String host = (String) properties.get("host");
final String key = (String) properties.get("key");
final String type = (String) properties.get("type");
final Long port = (Long) properties.get("port");
if (host != null && port != null && username != null && password != null && key != null) {
final App app = StructrApp.getInstance();
try (final Tx tx = app.tx()) {
final StructrWebSocket webSocket = getWebSocket();
final List<SyncableInfo> syncables = CloudService.doRemote(webSocket.getSecurityContext(), new SingleTransmission<>(new ListSyncables(type)), new HostInfo(username, password, host, port.intValue()), new WebsocketProgressListener(getWebSocket(), key, callback));
if (syncables != null) {
final List<GraphObject> result = new LinkedList<>();
for (final SyncableInfo info : syncables) {
final GraphObjectMap map = new GraphObjectMap();
map.put(GraphObject.id, info.getId());
map.put(NodeInterface.name, info.getName());
map.put(File.size, info.getSize());
map.put(GraphObject.type, info.getType());
map.put(GraphObject.visibleToPublicUsers, info.isVisibleToPublicUsers());
map.put(GraphObject.visibleToAuthenticatedUsers, info.isVisibleToAuthenticatedUsers());
map.put(GraphObject.lastModifiedDate, info.getLastModified());
// check for existance
map.put(isSynchronized, isSynchronized(info));
result.add(map);
}
webSocketData.setResult(result);
webSocket.send(webSocketData, true);
}
tx.success();
} catch (FrameworkException fex) {
getWebSocket().send(MessageBuilder.status().code(400).message(fex.getMessage()).build(), true);
}
} else {
getWebSocket().send(MessageBuilder.status().code(400).message("The PULL command needs sourceId, username, password, host, port and key!").build(), true);
}
}
use of org.structr.core.GraphObject in project structr by structr.
the class DOMNode method renderNodeList.
public static void renderNodeList(final DOMNode node, final SecurityContext securityContext, final RenderContext renderContext, final int depth, final String dataKey) throws FrameworkException {
final Iterable<GraphObject> listSource = renderContext.getListSource();
if (listSource != null) {
for (final GraphObject dataObject : listSource) {
// make current data object available in renderContext
renderContext.putDataObject(dataKey, dataObject);
node.renderContent(renderContext, depth + 1);
}
renderContext.clearDataObject(dataKey);
}
}
use of org.structr.core.GraphObject in project structr by structr.
the class IdRequestParameterGraphDataSource method getData.
@Override
public Iterable<GraphObject> getData(final RenderContext renderContext, final DOMNode referenceNode) throws FrameworkException {
final SecurityContext securityContext = renderContext.getSecurityContext();
if (securityContext != null && securityContext.getRequest() != null) {
String nodeId = securityContext.getRequest().getParameter(parameterName);
if (nodeId != null) {
AbstractNode node = (AbstractNode) StructrApp.getInstance(securityContext).getNodeById(nodeId);
if (node != null) {
List<GraphObject> graphData = new LinkedList<>();
graphData.add(node);
return graphData;
}
}
}
return null;
}
Aggregations