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 "";
}
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());
}
}
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;
}
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) {
}
}
}
}
}
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;
}
Aggregations