use of org.structr.core.app.App in project structr by structr.
the class ODFExporter method exportImage.
public static void exportImage(final ODFExporter thisNode, final String uuid) {
final File output = thisNode.getResultDocument();
try {
final App app = StructrApp.getInstance();
final Image result = app.nodeQuery(Image.class).and(GraphObject.id, uuid).getFirst();
String imageName = result.getProperty(new StringProperty("name"));
String contentType = result.getProperty(new StringProperty("contentType"));
String templateImagePath = null;
OdfDocument doc = OdfDocument.loadDocument(output.getFileOnDisk().getAbsolutePath());
NodeList nodes = doc.getContentRoot().getElementsByTagName(ODF_IMAGE_PARENT_NAME);
for (int i = 0; i < nodes.getLength(); i++) {
Node currentNode = nodes.item(i);
NamedNodeMap attrs = currentNode.getAttributes();
Node fieldName = attrs.getNamedItem(ODF_IMAGE_ATTRIBUTE_PARENT_IMAGE_NAME);
if (fieldName != null && fieldName.getTextContent().equals(imageName)) {
NamedNodeMap childAttrs = currentNode.getFirstChild().getAttributes();
Node filePath = childAttrs.getNamedItem(ODF_IMAGE_ATTRIBUTE_FILE_PATH);
templateImagePath = filePath.getTextContent();
filePath.setTextContent(ODF_IMAGE_DIRECTORY + imageName);
}
}
OdfPackage pkg = doc.getPackage();
if (templateImagePath != null && templateImagePath.length() > 0) {
pkg.remove(templateImagePath);
}
pkg.insert(new URI(result.getFileOnDisk().getAbsolutePath()), ODF_IMAGE_DIRECTORY + imageName, contentType);
pkg.save(output.getFileOnDisk().getAbsolutePath());
pkg.close();
doc.close();
} catch (Exception e) {
logger.error("Error while exporting image to document", e);
}
}
use of org.structr.core.app.App in project structr by structr.
the class ODSExporter method exportAttributes.
public static void exportAttributes(final ODSExporter thisNode, final String uuid) throws FrameworkException {
final SecurityContext securityContext = thisNode.getSecurityContext();
final File output = thisNode.getResultDocument();
final VirtualType transformation = thisNode.getTransformationProvider();
try {
final App app = StructrApp.getInstance();
final Result result = app.nodeQuery(AbstractNode.class).and(GraphObject.id, uuid).getResult();
final Result transformedResult = transformation.transformOutput(securityContext, AbstractNode.class, result);
Map<String, Object> nodeProperties = new HashMap<>();
GraphObjectMap node = (GraphObjectMap) transformedResult.get(0);
node.getPropertyKeys(null).forEach(p -> nodeProperties.put(p.dbName(), node.getProperty(p)));
OdfSpreadsheetDocument spreadsheet = OdfSpreadsheetDocument.loadDocument(output.getFileOnDisk().getAbsolutePath());
OdfTable sheet = spreadsheet.getTableList().get(0);
Iterator<Entry<String, Object>> it = nodeProperties.entrySet().iterator();
while (it.hasNext()) {
Entry<String, Object> currentEntry = it.next();
String address = currentEntry.getKey();
Object val = currentEntry.getValue();
if (val instanceof Collection) {
Collection col = (Collection) val;
writeCollectionToCells(sheet, sheet.getCellByPosition(address), col);
} else if (val instanceof String[]) {
String[] arr = (String[]) val;
List<String> list = new ArrayList<>(Arrays.asList(arr));
writeCollectionToCells(sheet, sheet.getCellByPosition(address), list);
} else {
writeObjectToCell(sheet.getCellByPosition(address), val);
}
}
spreadsheet.save(output.getFileOnDisk().getAbsolutePath());
spreadsheet.close();
} catch (Exception e) {
logger.error("Error while exporting to ODS", e);
}
}
use of org.structr.core.app.App in project structr by structr.
the class ODTExporter method exportAttributes.
static void exportAttributes(final ODTExporter thisNode, final String uuid) throws FrameworkException {
final SecurityContext securityContext = thisNode.getSecurityContext();
final File output = thisNode.getResultDocument();
final VirtualType transformation = thisNode.getTransformationProvider();
try {
final App app = StructrApp.getInstance();
final Result result = app.nodeQuery(AbstractNode.class).and(GraphObject.id, uuid).getResult();
final Result transformedResult = transformation.transformOutput(securityContext, AbstractNode.class, result);
Map<String, Object> nodeProperties = new HashMap<>();
GraphObjectMap node = (GraphObjectMap) transformedResult.get(0);
node.getPropertyKeys(null).forEach(p -> nodeProperties.put(p.dbName(), node.getProperty(p)));
TextDocument text = TextDocument.loadDocument(output.getFileOnDisk().getAbsolutePath());
NodeList nodes = text.getContentRoot().getElementsByTagName(ODT_FIELD_TAG_NAME);
for (int i = 0; i < nodes.getLength(); i++) {
Node currentNode = nodes.item(i);
NamedNodeMap attrs = currentNode.getAttributes();
Node fieldName = attrs.getNamedItem(ODT_FIELD_ATTRIBUTE_NAME);
Object nodeFieldValue = nodeProperties.get(fieldName.getNodeValue());
Node currentContent = attrs.getNamedItem(ODT_FIELD_ATTRIBUTE_VALUE);
if (nodeFieldValue != null) {
if (nodeFieldValue instanceof String[]) {
String[] arr = (String[]) nodeFieldValue;
List<String> list = new ArrayList<>(Arrays.asList(arr));
StringBuilder sb = new StringBuilder();
list.forEach(s -> sb.append(s + "\n"));
currentContent.setNodeValue(sb.toString());
} else if (nodeFieldValue instanceof Collection) {
Collection col = (Collection) nodeFieldValue;
StringBuilder sb = new StringBuilder();
col.forEach(s -> sb.append(s + "\n"));
currentContent.setNodeValue(sb.toString());
} else {
currentContent.setNodeValue(nodeFieldValue.toString());
}
}
}
text.save(output.getFileOnDisk().getAbsolutePath());
text.close();
} catch (Exception e) {
logger.error("Error while exporting to ODT", e);
}
}
use of org.structr.core.app.App in project structr by structr.
the class StructrODFModuleTest method createTestNode.
protected <T extends AbstractNode> T createTestNode(final Class<T> type, final PropertyMap props, final Principal owner) throws FrameworkException {
final App backendApp = StructrApp.getInstance(SecurityContext.getInstance(owner, AccessMode.Backend));
try (final Tx tx = backendApp.tx()) {
final T result = backendApp.create(type, props);
tx.success();
return result;
}
}
use of org.structr.core.app.App in project structr by structr.
the class FrameGrabberProcess method processExited.
@Override
public Image processExited(int exitCode) {
final App app = StructrApp.getInstance(securityContext);
if (exitCode == 0) {
try (final Tx tx = app.tx()) {
// move converted file into place
final java.io.File diskFile = new java.io.File(outputFileName + fileExtension);
final java.io.File dstFile = new java.io.File(outputFileName);
if (diskFile.exists()) {
Files.move(diskFile.toPath(), dstFile.toPath(), StandardCopyOption.REPLACE_EXISTING);
FileHelper.updateMetadata(newFile);
// create link between the two videos
inputFile.setProperty(StructrApp.key(VideoFile.class, "posterImage"), newFile);
}
tx.success();
} catch (FrameworkException | IOException fex) {
logger.warn("", fex);
}
} else {
// delete file, conversion has failed
try (final Tx tx = app.tx()) {
app.delete(newFile);
tx.success();
} catch (FrameworkException fex) {
logger.warn("", fex);
}
}
return newFile;
}
Aggregations