Search in sources :

Example 26 with ConfigurationProvider

use of org.structr.schema.ConfigurationProvider in project structr by structr.

the class Page method createElement.

static Element createElement(final Page page, final String tag, final boolean suppressException) {
    final String elementType = StringUtils.capitalize(tag);
    final App app = StructrApp.getInstance(page.getSecurityContext());
    String c = Content.class.getSimpleName();
    // Avoid creating an (invalid) 'Content' DOMElement
    if (elementType == null || c.equals(elementType)) {
        logger.warn("Blocked attempt to create a DOMElement of type {}", c);
        return null;
    }
    try {
        // final Class entityClass = Class.forName("org.structr.web.entity.html." + elementType);
        final Class entityClass = Class.forName("org.structr.web.entity.html." + elementType);
        if (entityClass != null) {
            final ConfigurationProvider config = StructrApp.getConfiguration();
            final DOMElement element = (DOMElement) app.create(entityClass, new NodeAttribute(StructrApp.key(entityClass, "tag"), tag), new NodeAttribute(StructrApp.key(entityClass, "hideOnDetail"), false), new NodeAttribute(StructrApp.key(entityClass, "hideOnIndex"), false));
            element.doAdopt(page);
            return element;
        }
    } catch (Throwable t) {
        if (!suppressException) {
            logger.error("Unable to instantiate element of type " + elementType, t);
        }
    }
    return null;
}
Also used : StructrApp(org.structr.core.app.StructrApp) App(org.structr.core.app.App) NodeAttribute(org.structr.core.graph.NodeAttribute) ConfigurationProvider(org.structr.schema.ConfigurationProvider)

Example 27 with ConfigurationProvider

use of org.structr.schema.ConfigurationProvider in project structr by structr.

the class CreateArchiveFunction method apply.

@Override
public Object apply(ActionContext ctx, Object caller, Object[] sources) throws FrameworkException {
    if (!(sources[1] instanceof File || sources[1] instanceof Folder || sources[1] instanceof Collection || sources.length < 2)) {
        logParameterError(caller, sources, ctx.isJavaScriptContext());
        return usage(ctx.isJavaScriptContext());
    }
    final ConfigurationProvider config = StructrApp.getConfiguration();
    try {
        java.io.File newArchive = java.io.File.createTempFile(sources[0].toString(), "zip");
        ZipArchiveOutputStream zaps = new ZipArchiveOutputStream(newArchive);
        zaps.setEncoding("UTF8");
        zaps.setUseLanguageEncodingFlag(true);
        zaps.setCreateUnicodeExtraFields(ZipArchiveOutputStream.UnicodeExtraFieldPolicy.ALWAYS);
        zaps.setFallbackToUTF8(true);
        if (sources[1] instanceof File) {
            File file = (File) sources[1];
            addFileToZipArchive(file.getProperty(AbstractFile.name), file, zaps);
        } else if (sources[1] instanceof Folder) {
            Folder folder = (Folder) sources[1];
            addFilesToArchive(folder.getProperty(Folder.name) + "/", folder.getFiles(), zaps);
            addFoldersToArchive(folder.getProperty(Folder.name) + "/", folder.getFolders(), zaps);
        } else if (sources[1] instanceof Collection) {
            for (Object fileOrFolder : (Collection) sources[1]) {
                if (fileOrFolder instanceof File) {
                    File file = (File) fileOrFolder;
                    addFileToZipArchive(file.getProperty(AbstractFile.name), file, zaps);
                } else if (fileOrFolder instanceof Folder) {
                    Folder folder = (Folder) fileOrFolder;
                    addFilesToArchive(folder.getProperty(Folder.name) + "/", folder.getFiles(), zaps);
                    addFoldersToArchive(folder.getProperty(Folder.name) + "/", folder.getFolders(), zaps);
                } else {
                    logParameterError(caller, sources, ctx.isJavaScriptContext());
                    return usage(ctx.isJavaScriptContext());
                }
            }
        } else {
            logParameterError(caller, sources, ctx.isJavaScriptContext());
            return usage(ctx.isJavaScriptContext());
        }
        zaps.close();
        Class archiveClass = null;
        if (sources.length > 2) {
            archiveClass = config.getNodeEntityClass(sources[2].toString());
        }
        if (archiveClass == null) {
            archiveClass = org.structr.web.entity.File.class;
        }
        try (final FileInputStream fis = new FileInputStream(newArchive)) {
            return FileHelper.createFile(ctx.getSecurityContext(), fis, "application/zip", archiveClass, sources[0].toString() + ".zip");
        }
    } catch (IOException e) {
        logException(caller, e, sources);
    }
    return null;
}
Also used : ConfigurationProvider(org.structr.schema.ConfigurationProvider) ZipArchiveOutputStream(org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream) IOException(java.io.IOException) Folder(org.structr.web.entity.Folder) FileInputStream(java.io.FileInputStream) Collection(java.util.Collection) AbstractFile(org.structr.web.entity.AbstractFile) File(org.structr.web.entity.File)

Example 28 with ConfigurationProvider

use of org.structr.schema.ConfigurationProvider in project structr by structr.

the class SearchCommand method typeAndAllSupertypes.

public static Set<Class> typeAndAllSupertypes(final Class type) {
    final ConfigurationProvider configuration = StructrApp.getConfiguration();
    final Set<Class> allSupertypes = new LinkedHashSet<>();
    Class localType = type;
    while (localType != null && !localType.equals(Object.class)) {
        allSupertypes.add(localType);
        allSupertypes.addAll(configuration.getInterfacesForType(localType));
        localType = localType.getSuperclass();
    }
    // remove base types
    allSupertypes.removeAll(baseTypes);
    return allSupertypes;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) ConfigurationProvider(org.structr.schema.ConfigurationProvider)

Example 29 with ConfigurationProvider

use of org.structr.schema.ConfigurationProvider in project structr by structr.

the class SearchCommand method getAllSubtypesAsStringSet.

public static synchronized Set<String> getAllSubtypesAsStringSet(final String type) {
    Set<String> allSubtypes = subtypeMapForType.get(type);
    if (allSubtypes == null) {
        logger.debug("Subtype map cache miss.");
        allSubtypes = new LinkedHashSet<>();
        subtypeMapForType.put(type, allSubtypes);
        final ConfigurationProvider configuration = StructrApp.getConfiguration();
        final Map<String, Class<? extends NodeInterface>> nodeEntities = configuration.getNodeEntities();
        final Map<String, Class<? extends RelationshipInterface>> relEntities = configuration.getRelationshipEntities();
        // add type first (this is neccesary because two class objects of the same dynamic type node are not equal
        // to each other and not assignable, if the schema node was modified in the meantime)
        allSubtypes.add(type);
        // scan all node entities for subtypes
        for (final Map.Entry<String, Class<? extends NodeInterface>> entity : nodeEntities.entrySet()) {
            final Class entityType = entity.getValue();
            final Set<Class> ancestors = typeAndAllSupertypes(entityType);
            for (final Class superClass : ancestors) {
                final String superClasSimpleName = superClass.getSimpleName();
                final String superClassFullName = superClass.getName();
                if ((superClassFullName.startsWith("org.structr.") || superClassFullName.startsWith("com.structr.")) && superClasSimpleName.equals(type)) {
                    allSubtypes.add(entityType.getSimpleName());
                }
            }
        }
        // scan all relationship entities for subtypes
        for (final Map.Entry<String, Class<? extends RelationshipInterface>> entity : relEntities.entrySet()) {
            final Class entityType = entity.getValue();
            final Set<Class> ancestors = typeAndAllSupertypes(entityType);
            for (final Class superClass : ancestors) {
                final String superClasSimpleName = superClass.getSimpleName();
                final String superClassFullName = superClass.getName();
                if ((superClassFullName.startsWith("org.structr.") || superClassFullName.startsWith("com.structr.")) && superClasSimpleName.equals(type)) {
                    allSubtypes.add(entityType.getSimpleName());
                }
            }
        }
    } else {
        logger.debug("Subtype map cache hit.");
    }
    return Collections.unmodifiableSet(allSubtypes);
}
Also used : ConfigurationProvider(org.structr.schema.ConfigurationProvider) RelationshipInterface(org.structr.core.graph.RelationshipInterface) LinkedHashMap(java.util.LinkedHashMap) PropertyMap(org.structr.core.property.PropertyMap) Map(java.util.Map) NodeInterface(org.structr.core.graph.NodeInterface)

Example 30 with ConfigurationProvider

use of org.structr.schema.ConfigurationProvider in project structr by structr.

the class Resource method extractSearchableAttributes.

protected void extractSearchableAttributes(final SecurityContext securityContext, final Class type, final HttpServletRequest request, final Query query) throws FrameworkException {
    if (type != null && request != null && !request.getParameterMap().isEmpty()) {
        final boolean exactSearch = !(parseInteger(request.getParameter(JsonRestServlet.REQUEST_PARAMETER_LOOSE_SEARCH)) == 1);
        final ConfigurationProvider conf = Services.getInstance().getConfigurationProvider();
        final List<PropertyKey> searchKeys = new LinkedList<>();
        for (final String name : request.getParameterMap().keySet()) {
            final PropertyKey key = StructrApp.getConfiguration().getPropertyKeyForJSONName(type, getFirstPartOfString(name), false);
            if (key != null) {
                // add to list of searchable keys
                searchKeys.add(key);
            } else if (!JsonRestServlet.commonRequestParameters.contains(name)) {
                // exclude common request parameters here (should not throw exception)
                throw new FrameworkException(400, "Unknown search key " + name);
            }
        }
        // sort list of search keys according to their desired order
        // so that querying search attributes can use other attributes
        // to refine their partial results.
        Collections.sort(searchKeys, new PropertyKeyProcessingOrderComparator());
        for (final PropertyKey key : searchKeys) {
            // hand list of search attributes over to key
            key.extractSearchableAttribute(securityContext, request, exactSearch, query);
        }
    }
}
Also used : FrameworkException(org.structr.common.error.FrameworkException) ConfigurationProvider(org.structr.schema.ConfigurationProvider) PropertyKey(org.structr.core.property.PropertyKey) LinkedList(java.util.LinkedList)

Aggregations

ConfigurationProvider (org.structr.schema.ConfigurationProvider)50 PropertyKey (org.structr.core.property.PropertyKey)27 FrameworkException (org.structr.common.error.FrameworkException)25 GraphObject (org.structr.core.GraphObject)17 Tx (org.structr.core.graph.Tx)15 LinkedList (java.util.LinkedList)14 PropertyMap (org.structr.core.property.PropertyMap)14 NodeInterface (org.structr.core.graph.NodeInterface)12 Test (org.junit.Test)11 Map (java.util.Map)10 PropertyConverter (org.structr.core.converter.PropertyConverter)10 NodeAttribute (org.structr.core.graph.NodeAttribute)10 SecurityContext (org.structr.common.SecurityContext)9 App (org.structr.core.app.App)9 StructrApp (org.structr.core.app.StructrApp)9 SchemaNode (org.structr.core.entity.SchemaNode)8 File (org.structr.web.entity.File)8 LinkedHashMap (java.util.LinkedHashMap)7 Gson (com.google.gson.Gson)6 GsonBuilder (com.google.gson.GsonBuilder)6