Search in sources :

Example 16 with PropertyMap

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

the class SetFunction method apply.

@Override
public Object apply(final ActionContext ctx, final Object caller, final Object[] sources) throws FrameworkException {
    if (arrayHasMinLengthAndAllElementsNotNull(sources, 2)) {
        final SecurityContext securityContext = ctx.getSecurityContext();
        final ConfigurationProvider config = StructrApp.getConfiguration();
        Class type = null;
        PropertyMap propertyMap = null;
        if (sources[0] instanceof GraphObject) {
            final GraphObject source = (GraphObject) sources[0];
            type = source.getEntityType();
        }
        if (type == null) {
            throw new FrameworkException(422, "Can't get type of object '" + sources[0].toString() + "' in set() method!");
        }
        final GraphObject sourceObject = (GraphObject) sources[0];
        if (sources.length == 2 && sources[1] instanceof Map) {
            propertyMap = PropertyMap.inputTypeToJavaType(securityContext, type, (Map) sources[1]);
        } else if (sources.length == 2 && sources[1] instanceof GraphObjectMap) {
            propertyMap = PropertyMap.inputTypeToJavaType(securityContext, type, ((GraphObjectMap) sources[1]).toMap());
        } else if (sources.length == 2 && sources[1] instanceof String) {
            final Gson gson = new GsonBuilder().create();
            final Map<String, Object> values = deserialize(gson, sources[1].toString());
            if (values != null) {
                propertyMap = PropertyMap.inputTypeToJavaType(securityContext, type, values);
            }
        } else if (sources.length > 2) {
            propertyMap = new PropertyMap();
            final int parameter_count = sources.length;
            if (parameter_count % 2 == 0) {
                throw new FrameworkException(400, "Invalid number of parameters: " + parameter_count + ". Should be uneven: " + (ctx.isJavaScriptContext() ? ERROR_MESSAGE_SET_JS : ERROR_MESSAGE_SET));
            }
            for (int c = 1; c < parameter_count; c += 2) {
                final PropertyKey key = config.getPropertyKeyForJSONName(type, sources[c].toString());
                if (key != null) {
                    final PropertyConverter inputConverter = key.inputConverter(securityContext);
                    Object value = sources[c + 1];
                    if (inputConverter != null) {
                        value = inputConverter.convert(value);
                    }
                    propertyMap.put(key, value);
                }
            }
        } else {
            throw new FrameworkException(422, "Invalid use of builtin method set, usage: set(entity, params..)");
        }
        if (propertyMap != null) {
            sourceObject.setProperties(securityContext, propertyMap);
        }
    } else {
        logParameterError(caller, sources, ctx.isJavaScriptContext());
    }
    return "";
}
Also used : FrameworkException(org.structr.common.error.FrameworkException) GsonBuilder(com.google.gson.GsonBuilder) ConfigurationProvider(org.structr.schema.ConfigurationProvider) Gson(com.google.gson.Gson) GraphObject(org.structr.core.GraphObject) PropertyMap(org.structr.core.property.PropertyMap) GraphObjectMap(org.structr.core.GraphObjectMap) SecurityContext(org.structr.common.SecurityContext) PropertyConverter(org.structr.core.converter.PropertyConverter) GraphObject(org.structr.core.GraphObject) PropertyMap(org.structr.core.property.PropertyMap) Map(java.util.Map) GraphObjectMap(org.structr.core.GraphObjectMap) PropertyKey(org.structr.core.property.PropertyKey)

Example 17 with PropertyMap

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

the class JavaParserModule method handlePackageFolder.

private void handlePackageFolder(final Folder folder, final Folder parentFolder) {
    // Folder contains a package-info.java so it must be a package
    String[] parts = folder.getPath().split("src/main/java/");
    if (parts.length > 1) {
        final PropertyMap identifyingProperties = new PropertyMap();
        final PropertyMap allProperties = new PropertyMap();
        // Convert path to package path
        String path = StringUtils.replaceAll(parts[1], "/", ".");
        identifyingProperties.put(Package.name, path);
        allProperties.putAll(identifyingProperties);
        allProperties.put(Package.folder, folder);
        // Check if we are contained in a module:
        // Find the closest ancestor folder which has a module
        Module mod = null;
        Package pkg = null;
        Folder possibleModuleParentFolder = parentFolder;
        // Continue until root folder or a module was found
        while (possibleModuleParentFolder != null && mod == null) {
            try {
                mod = app.nodeQuery(Module.class).and(Module.folder, possibleModuleParentFolder).getFirst();
                pkg = app.nodeQuery(Package.class).and(Module.folder, possibleModuleParentFolder).getFirst();
            } catch (FrameworkException ignore) {
            }
            if (pkg != null) {
                // Parent folder contains a package
                allProperties.put(Package.parent, pkg);
            } else if (mod != null) {
                // Parent folder contains a module
                allProperties.put(Package.module, mod);
                break;
            }
            // Continue while loop
            possibleModuleParentFolder = possibleModuleParentFolder.getParent();
        }
        getOrCreate(Package.class, identifyingProperties, allProperties);
        logger.info("Created or found package '" + path + "' in folder " + folder.getPath());
    }
}
Also used : PropertyMap(org.structr.core.property.PropertyMap) FrameworkException(org.structr.common.error.FrameworkException) Package(org.structr.javaparser.entity.Package) StructrModule(org.structr.module.StructrModule) Module(org.structr.javaparser.entity.Module) Folder(org.structr.web.entity.Folder)

Example 18 with PropertyMap

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

the class JavaParserModule method handleClassOrInterface.

private ClassOrInterface handleClassOrInterface(final TypeDeclaration type, final org.structr.javaparser.entity.Package pkg) {
    final String name = (pkg != null ? pkg.getName() + "." : "") + type.getNameAsString();
    logger.info("Parsing class or interface " + name);
    final PropertyMap identifyingProperties = new PropertyMap();
    identifyingProperties.put(ClassOrInterface.name, name);
    if (pkg != null) {
        identifyingProperties.put(ClassOrInterface.packageProp, pkg);
    }
    ClassOrInterface clsOrIface = null;
    boolean isInterface = type.asClassOrInterfaceDeclaration().isInterface();
    if (isInterface) {
        // Create Java interface
        clsOrIface = (JavaInterface) getOrCreate(JavaInterface.class, identifyingProperties, identifyingProperties);
        logger.info("Created (or found) interface " + clsOrIface.getName());
    } else {
        // Create Java class
        clsOrIface = (JavaClass) getOrCreate(JavaClass.class, identifyingProperties, identifyingProperties);
        logger.info("Created (or found) class " + clsOrIface.getName());
    }
    return clsOrIface;
}
Also used : ClassOrInterface(org.structr.javaparser.entity.ClassOrInterface) PropertyMap(org.structr.core.property.PropertyMap)

Example 19 with PropertyMap

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

the class JavaParserModule method parseJavaFilesAndSolveTypes.

private void parseJavaFilesAndSolveTypes(final Folder folder) {
    if (ignoreTests && "test".equals(folder.getName())) {
        return;
    }
    for (final File file : folder.getFiles()) {
        if (file.getContentType().equals("text/x-java")) {
            final String javaFileName = file.getName();
            if (javaFileName.equals("package-info.java") || javaFileName.equals("testPackage-info.java")) {
            } else {
                final String javaContent = file.getFavoriteContent();
                ClassOrInterface clsOrIface = null;
                CompilationUnit cu = null;
                try {
                    cu = JavaParser.parse(javaContent);
                    for (final TypeDeclaration type : cu.findAll(TypeDeclaration.class)) {
                        SymbolReference<? extends ResolvedValueDeclaration> decl = facade.solve(type.getName());
                        if (type.isClassOrInterfaceDeclaration()) {
                            org.structr.javaparser.entity.Package pkg = null;
                            if (cu.getPackageDeclaration().isPresent()) {
                                pkg = handlePackage(cu.getPackageDeclaration().get());
                            }
                            clsOrIface = handleClassOrInterface(type, pkg);
                        }
                    }
                    for (final BodyDeclaration t : cu.findAll(BodyDeclaration.class)) {
                        if (t instanceof CallableDeclaration) {
                            final CallableDeclaration callable = t.asCallableDeclaration();
                            if (t instanceof ConstructorDeclaration) {
                            // final ConstructorDeclaration cd = t.asConstructorDeclaration();
                            // logger.info("Constructor found: " + cd.getNameAsString());
                            // 
                            // final SymbolReference<ResolvedReferenceTypeDeclaration> constructorRef = typeSolver.tryToSolveType(cd.getNameAsString());
                            // if (constructorRef.isSolved()) {
                            // 
                            // logger.info("Solved constructor: " + cd.getNameAsString());
                            // //final ResolvedReferenceTypeDeclaration decl = constructorRef.getCorrespondingDeclaration();
                            // }
                            } else if (t instanceof MethodDeclaration) {
                                final MethodDeclaration md = t.asMethodDeclaration();
                                final String methodName = md.getNameAsString();
                                logger.info("Method found: " + methodName);
                                // Create methods and link to class
                                final PropertyMap identifyingMethodProperties = new PropertyMap();
                                identifyingMethodProperties.put(Method.name, methodName);
                                final PropertyMap methodProperties = new PropertyMap();
                                methodProperties.putAll(identifyingMethodProperties);
                                methodProperties.put(Method.classOrInterface, clsOrIface);
                                methodProperties.put(Method.declaration, md.getDeclarationAsString());
                                final Optional<BlockStmt> block = md.getBody();
                                if (block.isPresent()) {
                                    methodProperties.put(Method.body, block.get().toString());
                                }
                                final String symbolName = StringUtils.substringAfterLast(clsOrIface.getName(), ".") + "." + md.getNameAsString();
                                try {
                                    final SymbolReference<? extends ResolvedValueDeclaration> methodRef = facade.solve(md.getName());
                                    if (methodRef.isSolved()) {
                                        final ResolvedValueDeclaration decl = methodRef.getCorrespondingDeclaration();
                                        if (decl.isMethod()) {
                                            final String mName = decl.asMethod().getName();
                                            final String signature = decl.asMethod().getSignature();
                                            logger.info("Solved method: " + methodRef.toString() + ", signature: " + signature);
                                            methodProperties.put(Method.resolved, true);
                                        }
                                    }
                                } catch (final UnsolvedSymbolException ignore) {
                                }
                                getOrCreate(Method.class, identifyingMethodProperties, methodProperties);
                                logger.info("Created (or found) method " + symbolName);
                            }
                        // final NodeList<Parameter> parameters = callable.getParameters();
                        // 
                        // List<JsonResult> parameterList = new ArrayList<>();
                        // 
                        // parameters.forEach((p) -> {
                        // 
                        // JsonResult param = new JsonResult();
                        // 
                        // param.addName(p);
                        // param.addType(p.getType());
                        // param.addModifiers(p);
                        // 
                        // parameterList.add(param);
                        // });
                        }
                    }
                } catch (Throwable ignore) {
                }
            }
        }
    }
}
Also used : CompilationUnit(com.github.javaparser.ast.CompilationUnit) UnsolvedSymbolException(com.github.javaparser.symbolsolver.javaparsermodel.UnsolvedSymbolException) ClassOrInterface(org.structr.javaparser.entity.ClassOrInterface) Package(org.structr.javaparser.entity.Package) Optional(java.util.Optional) MethodDeclaration(com.github.javaparser.ast.body.MethodDeclaration) SymbolReference(com.github.javaparser.symbolsolver.model.resolution.SymbolReference) Method(org.structr.javaparser.entity.Method) PropertyMap(org.structr.core.property.PropertyMap) ConstructorDeclaration(com.github.javaparser.ast.body.ConstructorDeclaration) CallableDeclaration(com.github.javaparser.ast.body.CallableDeclaration) BodyDeclaration(com.github.javaparser.ast.body.BodyDeclaration) ResolvedValueDeclaration(com.github.javaparser.resolution.declarations.ResolvedValueDeclaration) File(org.structr.web.entity.File) TypeDeclaration(com.github.javaparser.ast.body.TypeDeclaration)

Example 20 with PropertyMap

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

the class JavaParserModule method handlePackage.

private org.structr.javaparser.entity.Package handlePackage(final PackageDeclaration pkg) {
    final PropertyMap packageIdentifyingProperties = new PropertyMap();
    packageIdentifyingProperties.put(org.structr.javaparser.entity.Package.name, pkg.getNameAsString());
    org.structr.javaparser.entity.Package clsPackage = (org.structr.javaparser.entity.Package) getOrCreate(Package.class, packageIdentifyingProperties, packageIdentifyingProperties);
    if (clsPackage != null) {
        try {
            // Find corresponding folder
            final Folder packageFolder = app.nodeQuery(Folder.class).and(StructrApp.key(Folder.class, "path"), StringUtils.replaceAll(clsPackage.getName(), ".", "/")).getFirst();
            if (packageFolder != null) {
                clsPackage.setProperty(Package.folder, packageFolder);
            }
        } catch (final FrameworkException ex) {
        }
        ;
    }
    return clsPackage;
}
Also used : Package(org.structr.javaparser.entity.Package) PropertyMap(org.structr.core.property.PropertyMap) FrameworkException(org.structr.common.error.FrameworkException) Package(org.structr.javaparser.entity.Package) Folder(org.structr.web.entity.Folder)

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