Skip to content
Permalink
Browse files

Added ModelManager

  • Loading branch information...
Ghost-Programmer committed Apr 6, 2019
1 parent 98b3a35 commit eac8f9a25a881d3cf27a1cf6624d6e6acb8ca20e
Showing with 126 additions and 0 deletions.
  1. +13 −0 pom.xml
  2. +2 −0 src/module-info.java
  3. +111 −0 src/name/mymiller/extensions/models/ModelManager.java
13 pom.xml
@@ -5,6 +5,19 @@
<version>0.0.1-SNAPSHOT</version>
<name>MyMiller Java Extensions</name>
<description>Extensions to the Java library objects. Useable for most Java packages.</description>

<dependencies>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.9.7</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.7</version>
</dependency>
</dependencies>
<build>
<sourceDirectory>src</sourceDirectory>
<plugins>
@@ -15,4 +15,6 @@

requires java.desktop;
requires transitive java.logging;
requires com.fasterxml.jackson.core;
requires com.fasterxml.jackson.databind;
}
@@ -0,0 +1,111 @@
package name.mymiller.extensions.models;

import java.beans.IntrospectionException;
import java.beans.PropertyDescriptor;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.function.Function;

import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;

public class ModelManager {

private final Function<String, String> classResolver;

private final Function<String, String> daoResolver;

private final Function<String, String> daoFindResolver;

private final Function<String, String> daoSaveResolver;

public ModelManager(Function<String, String> classResolver, Function<String, String> daoResolver,
Function<String, String> daoFindResolver, Function<String, String> daoSaveResolver) {
super();
this.classResolver = classResolver;
this.daoResolver = daoResolver;
this.daoFindResolver = daoFindResolver;
this.daoSaveResolver = daoSaveResolver;
}

private String getDaoName(String className) {
return this.daoResolver.apply(className);
}

public String getField(String className, Long id, String field)
throws InstantiationException, IllegalAccessException, ClassNotFoundException, NoSuchMethodException,
InvocationTargetException, IntrospectionException, JsonProcessingException {
final Object data = this.getRow(className, id);

if (data != null) {
final Object value = new PropertyDescriptor(field, data.getClass()).getReadMethod().invoke(data);

final ObjectMapper mapper = new ObjectMapper();

return mapper.writeValueAsString(value);
}
return null;
}

private String getFindMethod(String className) {
return this.daoFindResolver.apply(className);
}

private String getFullClassName(String className) {
return this.classResolver.apply(className);
}

private Optional<Method> getMethodByName(Class<?> cls, String methodName) {
final List<Method> methods = Arrays.asList(cls.getMethods());

final Optional<Method> findFirst = methods.stream().filter(method -> method.getName().equals(methodName))
.findFirst();

return findFirst;
}

private Object getRow(String className, Long id) throws InstantiationException, IllegalAccessException,
ClassNotFoundException, NoSuchMethodException, InvocationTargetException {
final String daoClassName = this.getDaoName(className);
final Object dao = Class.forName(daoClassName).getDeclaredConstructor().newInstance();

final Optional<Method> findMethod = this.getMethodByName(dao.getClass(), this.getFindMethod(className));

if (findMethod.isPresent()) {
return findMethod.get().invoke(dao, id);
}

return null;
}

private String getSaveMethod(String className) {
return this.daoSaveResolver.apply(className);
}

public String setField(String className, Long id, String field, String jsonValue) throws InstantiationException,
IllegalAccessException, ClassNotFoundException, NoSuchMethodException, InvocationTargetException,
IllegalArgumentException, IntrospectionException, JsonParseException, JsonMappingException, IOException {

final Object data = this.getRow(className, id);
if (data != null) {
final PropertyDescriptor propertyDescriptor = new PropertyDescriptor(field, data.getClass());

final Class<?> parameterType = propertyDescriptor.getWriteMethod().getParameterTypes()[0];

final ObjectMapper mapper = new ObjectMapper();

final Object obj = mapper.readValue(jsonValue, parameterType);

propertyDescriptor.getWriteMethod().invoke(data, obj);

return jsonValue;
}
return null;
}
}

0 comments on commit eac8f9a

Please sign in to comment.
You can’t perform that action at this time.