Search in sources :

Example 26 with PropertyMap

use of org.structr.core.property.PropertyMap in project structr by structr.

the class DeployCommand method importListData.

private <T extends NodeInterface> void importListData(final Class<T> type, final List<Map<String, Object>> data, final PropertyMap... additionalData) throws FrameworkException {
    final SecurityContext context = SecurityContext.getSuperUserInstance();
    context.setDoTransactionNotifications(false);
    final App app = StructrApp.getInstance(context);
    try (final Tx tx = app.tx()) {
        for (final T toDelete : app.nodeQuery(type).getAsList()) {
            app.delete(toDelete);
        }
        for (final Map<String, Object> entry : data) {
            final PropertyMap map = PropertyMap.inputTypeToJavaType(context, type, entry);
            // allow caller to insert additional data for better creation performance
            for (final PropertyMap add : additionalData) {
                map.putAll(add);
            }
            app.create(type, map);
        }
        tx.success();
    } catch (FrameworkException fex) {
        logger.error("Unable to import {}, aborting with {}", type.getSimpleName(), fex.getMessage());
        fex.printStackTrace();
        throw fex;
    }
}
Also used : App(org.structr.core.app.App) StructrApp(org.structr.core.app.StructrApp) PropertyMap(org.structr.core.property.PropertyMap) Tx(org.structr.core.graph.Tx) FrameworkException(org.structr.common.error.FrameworkException) SecurityContext(org.structr.common.SecurityContext)

Example 27 with PropertyMap

use of org.structr.core.property.PropertyMap in project structr by structr.

the class DeployCommand method doImport.

// ----- private methods -----
private void doImport(final Map<String, Object> attributes) throws FrameworkException {
    final long startTime = System.currentTimeMillis();
    customHeaders.put("start", new Date(startTime).toString());
    final String path = (String) attributes.get("source");
    final SecurityContext ctx = SecurityContext.getSuperUserInstance();
    final App app = StructrApp.getInstance(ctx);
    ctx.setDoTransactionNotifications(false);
    ctx.disableEnsureCardinality();
    ctx.disableModificationOfAccessTime();
    final Map<String, Object> componentsConf = new HashMap<>();
    final Map<String, Object> templatesConf = new HashMap<>();
    final Map<String, Object> pagesConf = new HashMap<>();
    final Map<String, Object> filesConf = new HashMap<>();
    if (StringUtils.isBlank(path)) {
        throw new FrameworkException(422, "Please provide 'source' attribute for deployment source directory path.");
    }
    final Path source = Paths.get(path);
    if (!Files.exists(source)) {
        throw new FrameworkException(422, "Source path " + path + " does not exist.");
    }
    if (!Files.isDirectory(source)) {
        throw new FrameworkException(422, "Source path " + path + " is not a directory.");
    }
    final Map<String, Object> broadcastData = new HashMap();
    broadcastData.put("type", DEPLOYMENT_IMPORT_STATUS);
    broadcastData.put("subtype", DEPLOYMENT_STATUS_BEGIN);
    broadcastData.put("start", startTime);
    broadcastData.put("source", source);
    TransactionCommand.simpleBroadcastGenericMessage(broadcastData);
    // apply configuration
    final Path preDeployConf = source.resolve("pre-deploy.conf");
    if (Files.exists(preDeployConf)) {
        try (final Tx tx = app.tx()) {
            final String confSource = new String(Files.readAllBytes(preDeployConf), Charset.forName("utf-8")).trim();
            if (confSource.length() > 0) {
                info("Applying pre-deployment configuration from {}", preDeployConf);
                publishDeploymentProgressMessage(DEPLOYMENT_IMPORT_STATUS, "Applying pre-deployment configuration");
                Scripting.evaluate(new ActionContext(ctx), null, confSource, "pre-deploy.conf");
            } else {
                info("Ignoring empty pre-deployment configuration {}", preDeployConf);
            }
            tx.success();
        } catch (Throwable t) {
            logger.warn("", t);
            publishDeploymentWarningMessage("Exception caught while importing pre-deploy.conf", t.toString());
        }
    }
    // backup previous value of change log setting
    // temporary disable creation of change log
    final boolean changeLogEnabled = Settings.ChangelogEnabled.getValue();
    Settings.ChangelogEnabled.setValue(false);
    // read grants.json
    publishDeploymentProgressMessage(DEPLOYMENT_IMPORT_STATUS, "Importing resource access grants");
    final Path grantsConf = source.resolve("security/grants.json");
    if (Files.exists(grantsConf)) {
        info("Reading {}", grantsConf);
        importListData(ResourceAccess.class, readConfigList(grantsConf));
    }
    // read schema-methods.json
    final Path schemaMethodsConf = source.resolve("schema-methods.json");
    if (Files.exists(schemaMethodsConf)) {
        info("Reading {}", schemaMethodsConf);
        final String title = "Deprecation warning";
        final String text = "Found file 'schema-methods.json'. Newer versions store global schema methods in the schema snapshot file. Recreate the export with the current version to avoid compatibility issues. Support for importing this file will be dropped in future versions.";
        info(title + ": " + text);
        publishDeploymentWarningMessage(title, text);
        importListData(SchemaMethod.class, readConfigList(schemaMethodsConf));
    }
    // read mail-templates.json
    final Path mailTemplatesConf = source.resolve("mail-templates.json");
    if (Files.exists(mailTemplatesConf)) {
        info("Reading {}", mailTemplatesConf);
        publishDeploymentProgressMessage(DEPLOYMENT_IMPORT_STATUS, "Importing mail templates");
        importListData(MailTemplate.class, readConfigList(mailTemplatesConf));
    }
    // read widgets.json
    final Path widgetsConf = source.resolve("widgets.json");
    if (Files.exists(widgetsConf)) {
        info("Reading {}", widgetsConf);
        publishDeploymentProgressMessage(DEPLOYMENT_IMPORT_STATUS, "Importing widgets");
        importListData(Widget.class, readConfigList(widgetsConf));
    }
    // read localizations.json
    final Path localizationsConf = source.resolve("localizations.json");
    if (Files.exists(localizationsConf)) {
        final PropertyMap additionalData = new PropertyMap();
        // Question: shouldn't this be true? No, 'imported' is a flag for legacy-localization which
        // have been imported from a legacy-system which was replaced by structr.
        // it is a way to differentiate between new and old localization strings
        additionalData.put(StructrApp.key(Localization.class, "imported"), false);
        info("Reading {}", localizationsConf);
        publishDeploymentProgressMessage(DEPLOYMENT_IMPORT_STATUS, "Importing localizations");
        importListData(Localization.class, readConfigList(localizationsConf), additionalData);
    }
    // read files.conf
    final Path filesConfFile = source.resolve("files.json");
    if (Files.exists(filesConfFile)) {
        info("Reading {}", filesConfFile);
        filesConf.putAll(readConfigMap(filesConfFile));
    }
    // read pages.conf
    final Path pagesConfFile = source.resolve("pages.json");
    if (Files.exists(pagesConfFile)) {
        info("Reading {}", pagesConfFile);
        pagesConf.putAll(readConfigMap(pagesConfFile));
    }
    // read components.conf
    final Path componentsConfFile = source.resolve("components.json");
    if (Files.exists(componentsConfFile)) {
        info("Reading {}", componentsConfFile);
        componentsConf.putAll(readConfigMap(componentsConfFile));
    }
    // read templates.conf
    final Path templatesConfFile = source.resolve("templates.json");
    if (Files.exists(templatesConfFile)) {
        info("Reading {}", templatesConfFile);
        templatesConf.putAll(readConfigMap(templatesConfFile));
    }
    // import schema
    final Path schema = source.resolve("schema");
    if (Files.exists(schema)) {
        try {
            info("Importing data from schema/ directory");
            publishDeploymentProgressMessage(DEPLOYMENT_IMPORT_STATUS, "Importing schema");
            Files.walkFileTree(schema, new SchemaImportVisitor(schema));
        } catch (IOException ioex) {
            logger.warn("Exception while importing schema", ioex);
        }
    }
    // import files
    final Path files = source.resolve("files");
    if (Files.exists(files)) {
        try {
            info("Importing files (unchanged files will be skipped)");
            publishDeploymentProgressMessage(DEPLOYMENT_IMPORT_STATUS, "Importing files");
            FileImportVisitor fiv = new FileImportVisitor(files, filesConf);
            Files.walkFileTree(files, fiv);
            fiv.handleDeferredFiles();
        } catch (IOException ioex) {
            logger.warn("Exception while importing files", ioex);
        }
    }
    for (StructrModule module : StructrApp.getConfiguration().getModules().values()) {
        if (module.hasDeploymentData()) {
            info("Importing deployment data for module {}", module.getName());
            publishDeploymentProgressMessage(DEPLOYMENT_IMPORT_STATUS, "Importing deployment data for module " + module.getName());
            final Path moduleFolder = source.resolve("modules/" + module.getName() + "/");
            module.importDeploymentData(moduleFolder, getGson());
        }
    }
    // construct paths
    final Path templates = source.resolve("templates");
    final Path components = source.resolve("components");
    final Path pages = source.resolve("pages");
    // an empty directory was specified accidentially).
    if (Files.exists(templates) && Files.exists(components) && Files.exists(pages)) {
        try (final Tx tx = app.tx()) {
            final String tenantIdentifier = app.getDatabaseService().getTenantIdentifier();
            info("Removing pages, templates and components");
            publishDeploymentProgressMessage(DEPLOYMENT_IMPORT_STATUS, "Removing pages, templates and components");
            if (tenantIdentifier != null) {
                app.cypher("MATCH (n:" + tenantIdentifier + ":DOMNode) DETACH DELETE n", null);
            } else {
                app.cypher("MATCH (n:DOMNode) DETACH DELETE n", null);
            }
            FlushCachesCommand.flushAll();
            tx.success();
        }
    } else {
        logger.info("Import directory does not seem to contain pages, templates or components, NOT removing any data.");
    }
    // import templates, must be done before pages so the templates exist
    if (Files.exists(templates)) {
        try {
            info("Importing templates");
            publishDeploymentProgressMessage(DEPLOYMENT_IMPORT_STATUS, "Importing templates");
            Files.walkFileTree(templates, new TemplateImportVisitor(templatesConf));
        } catch (IOException ioex) {
            logger.warn("Exception while importing templates", ioex);
        }
    }
    // import components, must be done before pages so the shared components exist
    if (Files.exists(components)) {
        try {
            info("Importing shared components");
            publishDeploymentProgressMessage(DEPLOYMENT_IMPORT_STATUS, "Importing shared components");
            Files.walkFileTree(components, new ComponentImportVisitor(componentsConf));
        } catch (IOException ioex) {
            logger.warn("Exception while importing shared components", ioex);
        }
    }
    // import pages
    if (Files.exists(pages)) {
        try {
            info("Importing pages");
            publishDeploymentProgressMessage(DEPLOYMENT_IMPORT_STATUS, "Importing pages");
            Files.walkFileTree(pages, new PageImportVisitor(pages, pagesConf));
        } catch (IOException ioex) {
            logger.warn("Exception while importing pages", ioex);
        }
    }
    try (final Tx tx = app.tx()) {
        deferredPageLinks.forEach((String linkableUUID, String pagePath) -> {
            try {
                final DOMNode page = StructrApp.getInstance().get(DOMNode.class, linkableUUID);
                final Linkable linkedPage = StructrApp.getInstance().nodeQuery(Linkable.class).and(StructrApp.key(Page.class, "path"), pagePath).or(Page.name, pagePath).getFirst();
                ((LinkSource) page).setLinkable(linkedPage);
            } catch (FrameworkException ex) {
            }
        });
        deferredPageLinks.clear();
        tx.success();
    }
    // apply configuration
    final Path postDeployConf = source.resolve("post-deploy.conf");
    if (Files.exists(postDeployConf)) {
        try (final Tx tx = app.tx()) {
            final String confSource = new String(Files.readAllBytes(postDeployConf), Charset.forName("utf-8")).trim();
            if (confSource.length() > 0) {
                info("Applying post-deployment configuration from {}", postDeployConf);
                publishDeploymentProgressMessage(DEPLOYMENT_IMPORT_STATUS, "Applying post-deployment configuration");
                Scripting.evaluate(new ActionContext(ctx), null, confSource, "post-deploy.conf");
            } else {
                info("Ignoring empty post-deployment configuration {}", postDeployConf);
            }
            tx.success();
        } catch (Throwable t) {
            logger.warn("", t);
            publishDeploymentWarningMessage("Exception caught while importing post-deploy.conf", t.toString());
        }
    }
    // restore saved value
    Settings.ChangelogEnabled.setValue(changeLogEnabled);
    final long endTime = System.currentTimeMillis();
    DecimalFormat decimalFormat = new DecimalFormat("0.00", DecimalFormatSymbols.getInstance(Locale.ENGLISH));
    final String duration = decimalFormat.format(((endTime - startTime) / 1000.0)) + "s";
    customHeaders.put("end", new Date(endTime).toString());
    customHeaders.put("duration", duration);
    info("Import from {} done. (Took {})", source.toString(), duration);
    broadcastData.put("subtype", DEPLOYMENT_STATUS_END);
    broadcastData.put("end", endTime);
    broadcastData.put("duration", duration);
    TransactionCommand.simpleBroadcastGenericMessage(broadcastData);
}
Also used : App(org.structr.core.app.App) StructrApp(org.structr.core.app.StructrApp) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) LinkSource(org.structr.web.entity.LinkSource) FileImportVisitor(org.structr.web.maintenance.deploy.FileImportVisitor) DecimalFormat(java.text.DecimalFormat) Page(org.structr.web.entity.dom.Page) PageImportVisitor(org.structr.web.maintenance.deploy.PageImportVisitor) SchemaImportVisitor(org.structr.web.maintenance.deploy.SchemaImportVisitor) Localization(org.structr.core.entity.Localization) DOMNode(org.structr.web.entity.dom.DOMNode) Path(java.nio.file.Path) FrameworkException(org.structr.common.error.FrameworkException) Tx(org.structr.core.graph.Tx) ComponentImportVisitor(org.structr.web.maintenance.deploy.ComponentImportVisitor) IOException(java.io.IOException) ActionContext(org.structr.schema.action.ActionContext) Date(java.util.Date) StructrModule(org.structr.module.StructrModule) PropertyMap(org.structr.core.property.PropertyMap) SecurityContext(org.structr.common.SecurityContext) TemplateImportVisitor(org.structr.web.maintenance.deploy.TemplateImportVisitor) Linkable(org.structr.web.entity.Linkable)

Example 28 with PropertyMap

use of org.structr.core.property.PropertyMap in project structr by structr.

the class FileImportVisitor method handleDeferredFiles.

public void handleDeferredFiles() {
    final Class<Relation> relType = StructrApp.getConfiguration().getRelationshipEntityClass("AbstractMinifiedFileMINIFICATIONFile");
    final PropertyKey<Integer> positionKey = StructrApp.key(relType, "position");
    if (!this.deferredFiles.isEmpty()) {
        for (File file : this.deferredFiles) {
            try (final Tx tx = app.tx(true, false, false)) {
                // set properties from files.json
                final PropertyMap fileProperties = getPropertiesForFileOrFolder(file.getPath());
                final PropertyKey<Map<String, String>> sourcesPropertyKey = new GenericProperty("minificationSources");
                Map<String, String> sourcesConfig = fileProperties.get(sourcesPropertyKey);
                fileProperties.remove(sourcesPropertyKey);
                file.unlockSystemPropertiesOnce();
                file.setProperties(securityContext, fileProperties);
                for (String positionString : sourcesConfig.keySet()) {
                    final Integer position = Integer.parseInt(positionString);
                    final String sourcePath = sourcesConfig.get(positionString);
                    final AbstractFile source = FileHelper.getFileByAbsolutePath(securityContext, sourcePath);
                    if (source != null) {
                        app.create(app.get(AbstractMinifiedFile.class, file.getUuid()), (File) source, relType, new PropertyMap(positionKey, position));
                    } else {
                        logger.warn("Source file {} for minified file {} at position {} not found - please verify that it is included in the export", sourcePath, file.getPath(), positionString);
                    }
                }
                tx.success();
            } catch (FrameworkException fxe) {
            }
        }
    }
}
Also used : AbstractFile(org.structr.web.entity.AbstractFile) Tx(org.structr.core.graph.Tx) FrameworkException(org.structr.common.error.FrameworkException) Relation(org.structr.core.entity.Relation) PropertyMap(org.structr.core.property.PropertyMap) GenericProperty(org.structr.core.property.GenericProperty) AbstractMinifiedFile(org.structr.web.entity.AbstractMinifiedFile) AbstractFile(org.structr.web.entity.AbstractFile) AbstractMinifiedFile(org.structr.web.entity.AbstractMinifiedFile) File(org.structr.web.entity.File) HashMap(java.util.HashMap) PropertyMap(org.structr.core.property.PropertyMap) Map(java.util.Map)

Example 29 with PropertyMap

use of org.structr.core.property.PropertyMap in project structr by structr.

the class DOMNode method cloneNode.

public static Node cloneNode(final DOMNode thisNode, boolean deep) {
    final SecurityContext securityContext = thisNode.getSecurityContext();
    if (deep) {
        return cloneAndAppendChildren(securityContext, thisNode);
    } else {
        final PropertyMap properties = new PropertyMap();
        for (Iterator<PropertyKey> it = thisNode.getPropertyKeys(PropertyView.Ui).iterator(); it.hasNext(); ) {
            final PropertyKey key = it.next();
            // skip blacklisted properties
            if (cloneBlacklist.contains(key.jsonName())) {
                continue;
            }
            if (!key.isUnvalidated()) {
                properties.put(key, thisNode.getProperty(key));
            }
        }
        // htmlView is necessary for the cloning of DOM nodes - otherwise some properties won't be cloned
        for (Iterator<PropertyKey> it = thisNode.getPropertyKeys(PropertyView.Html).iterator(); it.hasNext(); ) {
            final PropertyKey key = it.next();
            // skip blacklisted properties
            if (cloneBlacklist.contains(key.jsonName())) {
                continue;
            }
            if (!key.isUnvalidated()) {
                properties.put(key, thisNode.getProperty(key));
            }
        }
        if (thisNode instanceof LinkSource) {
            final LinkSource linkSourceElement = (LinkSource) thisNode;
            properties.put(StructrApp.key(LinkSource.class, "linkable"), linkSourceElement.getLinkable());
        }
        final App app = StructrApp.getInstance(securityContext);
        try {
            return app.create(thisNode.getClass(), properties);
        } catch (FrameworkException ex) {
            ex.printStackTrace();
            throw new DOMException(DOMException.INVALID_STATE_ERR, ex.toString());
        }
    }
}
Also used : App(org.structr.core.app.App) StructrApp(org.structr.core.app.StructrApp) DOMException(org.w3c.dom.DOMException) PropertyMap(org.structr.core.property.PropertyMap) FrameworkException(org.structr.common.error.FrameworkException) LinkSource(org.structr.web.entity.LinkSource) SecurityContext(org.structr.common.SecurityContext) PropertyKey(org.structr.core.property.PropertyKey)

Example 30 with PropertyMap

use of org.structr.core.property.PropertyMap in project structr by structr.

the class Page method createNewPage.

/**
 * Creates a new Page entity with the given name in the database.
 *
 * @param securityContext the security context to use
 * @param uuid the UUID of the new page or null
 * @param name the name of the new page, defaults to "page"
 * "ownerDocument" if not set
 *
 * @return the new ownerDocument
 * @throws FrameworkException
 */
public static Page createNewPage(final SecurityContext securityContext, final String uuid, final String name) throws FrameworkException {
    final PropertyKey<String> contentTypeKey = StructrApp.key(Page.class, "contentType");
    final PropertyKey<Boolean> hideOnDetailKey = StructrApp.key(Page.class, "hideOnDetail");
    final PropertyKey<Boolean> hideOnIndexKey = StructrApp.key(Page.class, "hideOnIndex");
    final PropertyKey<Boolean> enableBasicAuthKey = StructrApp.key(Page.class, "enableBasicAuth");
    final App app = StructrApp.getInstance(securityContext);
    final PropertyMap properties = new PropertyMap();
    // set default values for properties on creation to avoid them
    // being set separately when indexing later
    properties.put(AbstractNode.name, name != null ? name : "page");
    properties.put(AbstractNode.type, Page.class.getSimpleName());
    properties.put(contentTypeKey, "text/html");
    properties.put(hideOnDetailKey, false);
    properties.put(hideOnIndexKey, false);
    properties.put(enableBasicAuthKey, false);
    if (uuid != null) {
        properties.put(Page.id, uuid);
    }
    return app.create(Page.class, properties);
}
Also used : StructrApp(org.structr.core.app.StructrApp) App(org.structr.core.app.App) PropertyMap(org.structr.core.property.PropertyMap)

Aggregations

PropertyMap (org.structr.core.property.PropertyMap)233 FrameworkException (org.structr.common.error.FrameworkException)100 Tx (org.structr.core.graph.Tx)93 Test (org.junit.Test)60 App (org.structr.core.app.App)34 StructrApp (org.structr.core.app.StructrApp)34 PropertyKey (org.structr.core.property.PropertyKey)34 LinkedList (java.util.LinkedList)28 NodeInterface (org.structr.core.graph.NodeInterface)25 SchemaProperty (org.structr.core.entity.SchemaProperty)23 SecurityContext (org.structr.common.SecurityContext)22 StructrUiTest (org.structr.web.StructrUiTest)21 GraphObject (org.structr.core.GraphObject)20 Result (org.structr.core.Result)19 File (org.structr.web.entity.File)19 DOMNode (org.structr.web.entity.dom.DOMNode)19 TestOne (org.structr.core.entity.TestOne)17 AbstractNode (org.structr.core.entity.AbstractNode)16 Folder (org.structr.web.entity.Folder)15 Page (org.structr.web.entity.dom.Page)15