use of org.structr.core.property.PropertyMap in project structr by structr.
the class StructrDataFeedsModuleTest method createTestNode.
protected <T extends AbstractNode> T createTestNode(final Class<T> type, final String name) throws FrameworkException {
final PropertyMap map = new PropertyMap();
map.put(AbstractNode.name, name);
return (T) createTestNode(type, map);
}
use of org.structr.core.property.PropertyMap in project structr by structr.
the class CsvFunctionsTest method testCsvFunctions.
@Test
public void testCsvFunctions() {
List<CsvTestOne> csvTestOnes = null;
CsvTestTwo csvTestTwo = null;
int index = 0;
try (final Tx tx = app.tx()) {
csvTestOnes = createTestNodes(CsvTestOne.class, 5);
csvTestTwo = createTestNode(CsvTestTwo.class);
final PropertyMap indexLocalizationProperties = new PropertyMap();
final PropertyMap nameLocalizationProperties = new PropertyMap();
final PropertyMap indexLocalizationPropertiesWithDomain = new PropertyMap();
final PropertyMap nameLocalizationPropertiesWithDomain = new PropertyMap();
indexLocalizationProperties.put(StructrApp.key(Localization.class, "name"), "index");
indexLocalizationProperties.put(StructrApp.key(Localization.class, "localizedName"), "Localized INDEX");
indexLocalizationProperties.put(StructrApp.key(Localization.class, "locale"), "en");
nameLocalizationProperties.put(StructrApp.key(Localization.class, "name"), "name");
nameLocalizationProperties.put(StructrApp.key(Localization.class, "localizedName"), "Localized NAME");
nameLocalizationProperties.put(StructrApp.key(Localization.class, "locale"), "en");
indexLocalizationPropertiesWithDomain.put(StructrApp.key(Localization.class, "name"), "index");
indexLocalizationPropertiesWithDomain.put(StructrApp.key(Localization.class, "localizedName"), "Localized INDEX with DOMAIN");
indexLocalizationPropertiesWithDomain.put(StructrApp.key(Localization.class, "locale"), "en");
indexLocalizationPropertiesWithDomain.put(StructrApp.key(Localization.class, "domain"), "CSV TEST Domain");
nameLocalizationPropertiesWithDomain.put(StructrApp.key(Localization.class, "name"), "name");
nameLocalizationPropertiesWithDomain.put(StructrApp.key(Localization.class, "localizedName"), "Localized NAME with DOMAIN");
nameLocalizationPropertiesWithDomain.put(StructrApp.key(Localization.class, "locale"), "en");
nameLocalizationPropertiesWithDomain.put(StructrApp.key(Localization.class, "domain"), "CSV TEST Domain");
app.create(Localization.class, indexLocalizationProperties);
app.create(Localization.class, nameLocalizationProperties);
app.create(Localization.class, indexLocalizationPropertiesWithDomain);
app.create(Localization.class, nameLocalizationPropertiesWithDomain);
for (final CsvTestOne csvTestOne : csvTestOnes) {
csvTestOne.setProperty(CsvTestOne.name, "CSV Test Node " + StringUtils.leftPad(Integer.toString(index + 1), 4, "0"));
csvTestOne.setProperty(CsvTestOne.index, index + 1);
if (index == 0) {
// set string array on test four
csvTestOne.setProperty(CsvTestOne.stringArrayProperty, new String[] { "one", "two", "three", "four" });
}
if (index == 2) {
// set string array on test four
csvTestOne.setProperty(CsvTestOne.enumProperty, CsvTestEnum.EnumValue2);
}
index++;
}
tx.success();
} catch (FrameworkException fex) {
logger.warn("", fex);
fail("Unexpected exception");
}
try (final Tx tx = app.tx()) {
final ActionContext ctx = new ActionContext(securityContext, null);
ctx.setLocale(Locale.ENGLISH);
/**
* The expected results
*/
final String expectedDefaultCsv = "\"name\";\"index\";\"type\";\"stringArrayProperty\";\"enumProperty\"\n" + "\"CSV Test Node 0001\";\"1\";\"CsvTestOne\";\"[\\\"one\\\", \\\"two\\\", \\\"three\\\", \\\"four\\\"]\";\"\"\n" + "\"CSV Test Node 0002\";\"2\";\"CsvTestOne\";\"\";\"\"\n" + "\"CSV Test Node 0003\";\"3\";\"CsvTestOne\";\"\";\"EnumValue2\"\n" + "\"CSV Test Node 0004\";\"4\";\"CsvTestOne\";\"\";\"\"\n" + "\"CSV Test Node 0005\";\"5\";\"CsvTestOne\";\"\";\"\"\n";
final String expectedCsvWithNameAndIndex = "\"name\";\"index\"\n" + "\"CSV Test Node 0001\";\"1\"\n" + "\"CSV Test Node 0002\";\"2\"\n" + "\"CSV Test Node 0003\";\"3\"\n" + "\"CSV Test Node 0004\";\"4\"\n" + "\"CSV Test Node 0005\";\"5\"\n";
final String expectedCsvWithIndexAndName = "\"index\";\"name\"\n" + "\"1\";\"CSV Test Node 0001\"\n" + "\"2\";\"CSV Test Node 0002\"\n" + "\"3\";\"CSV Test Node 0003\"\n" + "\"4\";\"CSV Test Node 0004\"\n" + "\"5\";\"CSV Test Node 0005\"\n";
final String expectedCsvWithNameAndIndexAndCustomDelimiterPipe = "\"name\"|\"index\"\n" + "\"CSV Test Node 0001\"|\"1\"\n" + "\"CSV Test Node 0002\"|\"2\"\n" + "\"CSV Test Node 0003\"|\"3\"\n" + "\"CSV Test Node 0004\"|\"4\"\n" + "\"CSV Test Node 0005\"|\"5\"\n";
final String expectedCsvWithNameAndIndexAndCustomDelimiterXXX = "\"name\"X\"index\"\n" + "\"CSV Test Node 0001\"X\"1\"\n" + "\"CSV Test Node 0002\"X\"2\"\n" + "\"CSV Test Node 0003\"X\"3\"\n" + "\"CSV Test Node 0004\"X\"4\"\n" + "\"CSV Test Node 0005\"X\"5\"\n";
final String expectedCsvWithIndexAndNameAndSingleQuote = "'index';'name'\n" + "'1';'CSV Test Node 0001'\n" + "'2';'CSV Test Node 0002'\n" + "'3';'CSV Test Node 0003'\n" + "'4';'CSV Test Node 0004'\n" + "'5';'CSV Test Node 0005'\n";
final String expectedCsvWithIndexAndNameAndSingleQuoteAfterRoundTrip = "'index';'name'\n" + "'1';'CSV Test Node 0001'\n" + "'2';'CSV Test Node 0002'\n" + "'3';'CSV Test Node 0003'\n" + "'4';'CSV Test Node 0004'\n" + "'5';'CSV Test Node 0005'\n";
final String expectedCsvWithIndexAndNameAndSingleQuoteAndCRLF = "'index';'name'\r\n" + "'1';'CSV Test Node 0001'\r\n" + "'2';'CSV Test Node 0002'\r\n" + "'3';'CSV Test Node 0003'\r\n" + "'4';'CSV Test Node 0004'\r\n" + "'5';'CSV Test Node 0005'\r\n";
final String expectedCsvWithIndexAndNameAndSingleQuoteAndCRLFNoHeader = "'1';'CSV Test Node 0001'\r\n" + "'2';'CSV Test Node 0002'\r\n" + "'3';'CSV Test Node 0003'\r\n" + "'4';'CSV Test Node 0004'\r\n" + "'5';'CSV Test Node 0005'\r\n";
final String expectedCsvWithIndexAndNameAndSingleQuoteAndCRLFWithExplicitHeader = "'index';'name'\r\n" + "'1';'CSV Test Node 0001'\r\n" + "'2';'CSV Test Node 0002'\r\n" + "'3';'CSV Test Node 0003'\r\n" + "'4';'CSV Test Node 0004'\r\n" + "'5';'CSV Test Node 0005'\r\n";
final String expectedCsvWithIndexAndNameAndSingleQuoteAndCRLFWithLocalizedHeader = "'Localized INDEX';'Localized NAME'\r\n" + "'1';'CSV Test Node 0001'\r\n" + "'2';'CSV Test Node 0002'\r\n" + "'3';'CSV Test Node 0003'\r\n" + "'4';'CSV Test Node 0004'\r\n" + "'5';'CSV Test Node 0005'\r\n";
final String expectedCsvWithIndexAndNameAndSingleQuoteAndCRLFWithLocalizedHeaderWithDomain = "'Localized INDEX with DOMAIN';'Localized NAME with DOMAIN'\r\n" + "'1';'CSV Test Node 0001'\r\n" + "'2';'CSV Test Node 0002'\r\n" + "'3';'CSV Test Node 0003'\r\n" + "'4';'CSV Test Node 0004'\r\n" + "'5';'CSV Test Node 0005'\r\n";
final String expectedCsvForCustomJavaScriptObjects = "\"id\";\"customField\";\"name\"\n" + "\"abcd0001\";\"extra1\";\"my 1st custom object\"\n" + "\"bcde0002\";\"extra2\";\"my 2nd custom object\"\n" + "\"cdef0003\";\"extra3\";\"my 3rd custom object\"\n";
final String expectedCsvForObjectsWithNewlineCharacters = "\"multi\"\n" + "\"Multi\\nLine\\nTest\"\n";
/**
* First everything in StructrScript
*/
assertEquals("Invalid result of default to_csv() call (StructrScript)", expectedDefaultCsv, Scripting.replaceVariables(ctx, csvTestTwo, "${to_csv(find('CsvTestOne'), 'csv')}"));
assertEquals("Invalid result of to_csv() call with only name,index (StructrScript)", expectedCsvWithNameAndIndex, Scripting.replaceVariables(ctx, csvTestTwo, "${to_csv(find('CsvTestOne'), merge('name', 'index'))}"));
assertEquals("Invalid result of to_csv() call with only name,index (StructrScript)", expectedCsvWithIndexAndName, Scripting.replaceVariables(ctx, csvTestTwo, "${to_csv(find('CsvTestOne'), merge('index', 'name'))}"));
assertEquals("Invalid result of to_csv() with delimiterChar = '|'. (StructrScript)", expectedCsvWithNameAndIndexAndCustomDelimiterPipe, Scripting.replaceVariables(ctx, csvTestTwo, "${to_csv(find('CsvTestOne'), merge('name', 'index'), '|')}"));
assertEquals("Invalid result of to_csv() with delimiterChar = 'XXX'. Only first character should be used! (StructrScript)", expectedCsvWithNameAndIndexAndCustomDelimiterXXX, Scripting.replaceVariables(ctx, csvTestTwo, "${to_csv(find('CsvTestOne'), merge('name', 'index'), 'XXX')}"));
assertEquals("Invalid result of to_csv() call with only index,name AND quote character = ' (StructrScript)", expectedCsvWithIndexAndNameAndSingleQuote, Scripting.replaceVariables(ctx, csvTestTwo, "${to_csv(find('CsvTestOne'), merge('index', 'name'), ';', \"'\")}"));
assertEquals("Invalid result of to_csv() call with only index,name AND quote character = ' AFTER round-trip through to_csv, from_csv and to_csv (StructrScript)", expectedCsvWithIndexAndNameAndSingleQuoteAfterRoundTrip, Scripting.replaceVariables(ctx, csvTestTwo, "${to_csv(from_csv(to_csv(find('CsvTestOne'), merge('index', 'name'), ';', \"'\"), ';', \"'\"), merge('index', 'name'), ';', \"'\")}"));
assertEquals("Invalid result of to_csv() call with only index,name, singleQuoted and CRLF as recordSeparator (StructrScript)", expectedCsvWithIndexAndNameAndSingleQuoteAndCRLF, Scripting.replaceVariables(ctx, csvTestTwo, "${to_csv(find('CsvTestOne'), merge('index', 'name'), ';', \"'\", '\\r\\n')}"));
assertEquals("Invalid result of to_csv() call with only index,name, singleQuoted and CRLF as recordSeparator and no header (StructrScript)", expectedCsvWithIndexAndNameAndSingleQuoteAndCRLFNoHeader, Scripting.replaceVariables(ctx, csvTestTwo, "${to_csv(find('CsvTestOne'), merge('index', 'name'), ';', \"'\", '\\r\\n', false)}"));
assertEquals("Invalid result of to_csv() call with only index,name, singleQuoted and CRLF as recordSeparator and explicit header (StructrScript)", expectedCsvWithIndexAndNameAndSingleQuoteAndCRLFWithExplicitHeader, Scripting.replaceVariables(ctx, csvTestTwo, "${to_csv(find('CsvTestOne'), merge('index', 'name'), ';', \"'\", '\\r\\n', true)}"));
assertEquals("Invalid result of to_csv() call with only index,name, singleQuoted and CRLF as recordSeparator and localized header (without domain) (StructrScript)", expectedCsvWithIndexAndNameAndSingleQuoteAndCRLFWithLocalizedHeader, Scripting.replaceVariables(ctx, csvTestTwo, "${to_csv(find('CsvTestOne'), merge('index', 'name'), ';', \"'\", '\\r\\n', true, true)}"));
assertEquals("Invalid result of to_csv() call with only index,name, singleQuoted and CRLF as recordSeparator and localized header (with domain) (StructrScript)", expectedCsvWithIndexAndNameAndSingleQuoteAndCRLFWithLocalizedHeaderWithDomain, Scripting.replaceVariables(ctx, csvTestTwo, "${to_csv(find('CsvTestOne'), merge('index', 'name'), ';', \"'\", '\\r\\n', true, true, 'CSV TEST Domain')}"));
/**
* Then everything again in JavaScript
*/
assertEquals("Invalid result of default Structr.to_csv() call (JavaScript)", expectedDefaultCsv, Scripting.replaceVariables(ctx, csvTestTwo, "${{Structr.print(Structr.to_csv(Structr.find('CsvTestOne'), 'csv'))}}"));
assertEquals("Invalid result of Structr.to_csv() call with only name,index (JavaScript)", expectedCsvWithNameAndIndex, Scripting.replaceVariables(ctx, csvTestTwo, "${{Structr.print(Structr.to_csv(Structr.find('CsvTestOne'), ['name', 'index']))}}"));
assertEquals("Invalid result of Structr.to_csv() call with only name,index (JavaScript)", expectedCsvWithIndexAndName, Scripting.replaceVariables(ctx, csvTestTwo, "${{Structr.print(Structr.to_csv(Structr.find('CsvTestOne'), ['index', 'name']))}}"));
assertEquals("Invalid result of Structr.to_csv() with delimiterChar = '|'. (JavaScript)", expectedCsvWithNameAndIndexAndCustomDelimiterPipe, Scripting.replaceVariables(ctx, csvTestTwo, "${{Structr.print(Structr.to_csv(Structr.find('CsvTestOne'), ['name', 'index'], '|'))}}"));
assertEquals("Invalid result of Structr.to_csv() with delimiterChar = 'XXX'. Only first character should be used! (JavaScript)", expectedCsvWithNameAndIndexAndCustomDelimiterXXX, Scripting.replaceVariables(ctx, csvTestTwo, "${{Structr.print(Structr.to_csv(Structr.find('CsvTestOne'), ['name', 'index'], 'XXX'))}}"));
assertEquals("Invalid result of Structr.to_csv() call with only index,name AND quote character = ' (JavaScript)", expectedCsvWithIndexAndNameAndSingleQuote, Scripting.replaceVariables(ctx, csvTestTwo, "${{Structr.print(Structr.to_csv(Structr.find('CsvTestOne'), ['index', 'name'], ';', \"'\"))}}"));
assertEquals("Invalid result of Structr.to_csv() call with only index,name AND quote character = ' AFTER round-trip through to_csv, from_csv and to_csv (JavaScript)", expectedCsvWithIndexAndNameAndSingleQuoteAfterRoundTrip, Scripting.replaceVariables(ctx, csvTestTwo, "${{Structr.print(Structr.to_csv(Structr.from_csv(Structr.to_csv(Structr.find('CsvTestOne'), ['index', 'name'], ';', \"'\"), ';', \"'\"), ['index', 'name'], ';', \"'\"))}}"));
assertEquals("Invalid result of Structr.to_csv() call with only index,name, singleQuoted and CRLF as recordSeparator (JavaScript)", expectedCsvWithIndexAndNameAndSingleQuoteAndCRLF, Scripting.replaceVariables(ctx, csvTestTwo, "${{Structr.print(Structr.to_csv(Structr.find('CsvTestOne'), ['index', 'name'], ';', \"'\", '\\r\\n'))}}"));
assertEquals("Invalid result of Structr.to_csv() call with only index,name, singleQuoted and CRLF as recordSeparator and no header (JavaScript)", expectedCsvWithIndexAndNameAndSingleQuoteAndCRLFNoHeader, Scripting.replaceVariables(ctx, csvTestTwo, "${{Structr.print(Structr.to_csv(Structr.find('CsvTestOne'), ['index', 'name'], ';', \"'\", '\\r\\n', false))}}"));
assertEquals("Invalid result of Structr.to_csv() call with only index,name, singleQuoted and CRLF as recordSeparator and explicit header (JavaScript)", expectedCsvWithIndexAndNameAndSingleQuoteAndCRLFWithExplicitHeader, Scripting.replaceVariables(ctx, csvTestTwo, "${{Structr.print(Structr.to_csv(Structr.find('CsvTestOne'), ['index', 'name'], ';', \"'\", '\\r\\n', true))}}"));
assertEquals("Invalid result of Structr.to_csv() call with only index,name, singleQuoted and CRLF as recordSeparator and localized header (without domain) (JavaScript)", expectedCsvWithIndexAndNameAndSingleQuoteAndCRLFWithLocalizedHeader, Scripting.replaceVariables(ctx, csvTestTwo, "${{Structr.print(Structr.to_csv(Structr.find('CsvTestOne'), ['index', 'name'], ';', \"'\", '\\r\\n', true, true))}}"));
assertEquals("Invalid result of Structr.to_csv() call with only index,name, singleQuoted and CRLF as recordSeparator and localized header (with domain) (JavaScript)", expectedCsvWithIndexAndNameAndSingleQuoteAndCRLFWithLocalizedHeaderWithDomain, Scripting.replaceVariables(ctx, csvTestTwo, "${{Structr.print(Structr.to_csv(Structr.find('CsvTestOne'), ['index', 'name'], ';', \"'\", '\\r\\n', true, true, 'CSV TEST Domain'))}}"));
assertEquals("Invalid result of Structr.to_csv() call for a collection of custom objects (JavaScript)", expectedCsvForCustomJavaScriptObjects, Scripting.replaceVariables(ctx, csvTestTwo, "${{Structr.print(Structr.to_csv([{id: 'abcd0001', name: 'my 1st custom object', customField: 'extra1'}, {id: 'bcde0002', name: 'my 2nd custom object', customField: 'extra2'}, {id: 'cdef0003', name: 'my 3rd custom object', customField: 'extra3'}], ['id', 'customField', 'name']))}}"));
assertEquals("Invalid result of Structr.to_csv() call for source objects with newlines (JavaScript)", expectedCsvForObjectsWithNewlineCharacters, Scripting.replaceVariables(ctx, csvTestTwo, "${{Structr.print(Structr.to_csv([{multi:'Multi\\nLine\\nTest'}], ['multi']))}}"));
tx.success();
} catch (FrameworkException fex) {
logger.warn("", fex);
fail(fex.getMessage());
}
}
use of org.structr.core.property.PropertyMap in project structr by structr.
the class CMISObjectService method updateProperties.
@Override
public void updateProperties(final String repositoryId, final Holder<String> objectId, final Holder<String> changeToken, final Properties properties, final ExtensionsData extension) {
final App app = StructrApp.getInstance();
final String id = objectId.getValue();
try (final Tx tx = app.tx()) {
final AbstractNode obj = app.get(AbstractNode.class, id);
if (obj != null) {
final PropertyMap propertyMap = PropertyMap.cmisTypeToJavaType(securityContext, obj.getClass(), properties);
if (propertyMap != null) {
obj.setProperties(securityContext, propertyMap);
}
} else {
throw new CmisObjectNotFoundException("Object with ID " + objectId + " does not exist");
}
tx.success();
} catch (FrameworkException fex) {
throw new CmisConstraintException(fex.getMessage(), fex);
}
}
use of org.structr.core.property.PropertyMap in project structr by structr.
the class CMISObjectService method bulkUpdateProperties.
@Override
public List<BulkUpdateObjectIdAndChangeToken> bulkUpdateProperties(final String repositoryId, final List<BulkUpdateObjectIdAndChangeToken> objectIdsAndChangeTokens, final Properties properties, final List<String> addSecondaryTypeIds, final List<String> removeSecondaryTypeIds, final ExtensionsData extension) {
final List<BulkUpdateObjectIdAndChangeToken> result = new LinkedList<>();
final App app = StructrApp.getInstance(securityContext);
try (final Tx tx = app.tx()) {
for (final BulkUpdateObjectIdAndChangeToken token : objectIdsAndChangeTokens) {
final AbstractNode obj = app.get(AbstractNode.class, token.getId());
if (obj != null) {
final PropertyMap propertyMap = PropertyMap.cmisTypeToJavaType(securityContext, obj.getClass(), properties);
if (propertyMap != null) {
obj.setProperties(securityContext, propertyMap);
}
result.add(token);
}
}
tx.success();
} catch (FrameworkException fex) {
throw new CmisConstraintException(fex.getMessage(), fex);
}
return result;
}
use of org.structr.core.property.PropertyMap in project structr by structr.
the class ClosingFileOutputStream method close.
@Override
public void close() throws IOException {
if (closed) {
return;
}
try (Tx tx = StructrApp.getInstance().tx()) {
super.close();
final String _contentType = FileHelper.getContentMimeType(thisFile);
final PropertyMap changedProperties = new PropertyMap();
changedProperties.put(StructrApp.key(File.class, "checksum"), FileHelper.getChecksum(file));
changedProperties.put(StructrApp.key(File.class, "size"), file.length());
changedProperties.put(StructrApp.key(File.class, "contentType"), _contentType);
if (StringUtils.startsWith(_contentType, "image") || ImageHelper.isImageType(thisFile.getName())) {
changedProperties.put(NodeInterface.type, Image.class.getSimpleName());
}
thisFile.unlockSystemPropertiesOnce();
thisFile.setProperties(thisFile.getSecurityContext(), changedProperties);
thisFile.increaseVersion();
if (notifyIndexerAfterClosing) {
thisFile.notifyUploadCompletion();
}
tx.success();
} catch (Throwable ex) {
logger.error("Could not determine or save checksum and size after closing file output stream", ex);
}
closed = true;
}
Aggregations