Skip to content

Commit

Permalink
made execute on usecase implamentation private
Browse files Browse the repository at this point in the history
  • Loading branch information
rportela committed Feb 28, 2018
1 parent 98e7adb commit 65a92b3
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 57 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,12 @@
* @param <TParams>
* @param <TResult>
*/
public class UsecaseExecution<TParams, TResult> implements Runnable {
public class UsecaseExecution<TParams, TResult> {

/**
* Holds the name of the usecase;
*/
public String name;

/**
* Marks the date that the execution started;
Expand Down Expand Up @@ -60,35 +65,4 @@ public class UsecaseExecution<TParams, TResult> implements Runnable {
*/
public transient UsecaseImplementation<TParams, TResult> usecase;

/**
* Runs this execution with the current fields.
*/
@Override
public void run() {
this.usecase.execute(this);
}

/**
* Helper method to avoid typing all the reflection types on instantiation;
*
* @param claz
* @return
* @throws InstantiationException
* @throws IllegalAccessException
*/
public static final <TParams, TResult> UsecaseExecution<TParams, TResult> create(
final Class<? extends UsecaseImplementation<TParams, TResult>> claz) {
try {
final UsecaseImplementation<TParams, TResult> uc = (UsecaseImplementation<TParams, TResult>) claz
.getConstructor()
.newInstance();
final UsecaseExecution<TParams, TResult> execution = new UsecaseExecution<>();
execution.usecase = uc;
return execution;
} catch (Exception e) {
throw new RuntimeException(e);
}

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
* @author Rodrigo Portela
*
*/
public class UsecaseExecutionServlet<TParams, TResult> extends UsecaseExecution<TParams, TResult> {
public class UsecaseExecutionServlet<TParams, TResult> extends UsecaseExecution<TParams, TResult> implements Runnable {

/**
* A static shared object mapper instance;
Expand Down Expand Up @@ -69,28 +69,27 @@ public UsecaseExecutionServlet(HttpServletRequest request, HttpServletResponse r
}

/**
* Runs the usecase from the path info of the request, parsing the parameters or
* json body and writes the execution result to the response;
* Locates a usecase by name from the path info;
*
* @return
*/
@SuppressWarnings("unchecked")
@Override
public void run() {

private boolean parseUsecase() {
try {
// Locates a usecase by name from the path info;
String usecaseName = this.package_name + request.getPathInfo().replace('/', '.');
Class<?> usecaseClass = Class.forName(usecaseName);
Object usecase = usecaseClass.getConstructor().newInstance();
this.usecase = (UsecaseImplementation<TParams, TResult>) usecase;

this.name = this.package_name + request.getPathInfo().replace('/', '.');
Class<?> usecaseClass = Class.forName(this.name);
Object usecaseInstance = usecaseClass.getConstructor().newInstance();
this.usecase = (UsecaseImplementation<TParams, TResult>) usecaseInstance;
return true;
} catch (ClassNotFoundException e) {
// Sends a 404 - not found response if the usecase is not located
try {
response.sendError(404, "Usecase not found: " + request.getPathInfo());
} catch (IOException e1) {
e1.printStackTrace();
}
return;
return false;
} catch (InstantiationException e) {
// Sends a 404 - not found if the usecase can't be instantiated;
try {
Expand All @@ -99,7 +98,7 @@ public void run() {
} catch (IOException e1) {
e1.printStackTrace();
}
return;
return false;
} catch (IllegalAccessException e) {
// Sends a 404 - not found if the usecase can't be instantiated;
try {
Expand All @@ -108,7 +107,7 @@ public void run() {
} catch (IOException e1) {
e1.printStackTrace();
}
return;
return false;
} catch (Exception e) {
// Sends a 404 - not found if the usecase can't be cast to an
// usecase implementation or in any other exception;
Expand All @@ -119,10 +118,16 @@ public void run() {
} catch (IOException e1) {
e1.printStackTrace();
}
return;
return false;
}
}

// Parses the parameters of the usecase execution;
/**
* Parses the parameters of the usecase execution;
*
* @return
*/
private boolean parseParams() {
try {
Class<TParams> paramsClass = usecase.getParamsClass();
if (!Void.class.equals(paramsClass)) {
Expand All @@ -147,13 +152,31 @@ else if (contentType.startsWith("multipart/form-data"))
this.params = parseParameters(restrictionAspect);
}

return true;

} catch (Exception e) {

// If something fails, fallback to writing to output
// VALIDATION_FAILED and returns;
this.validation = new RestrictionValidation("params", false, e.getMessage());
this.result_type = UsecaseResultType.VALIDATION_FAILED;
writeOutputAsJson();
writeOutputAsJson(this);
return false;
}
}

/**
* Runs the usecase from the path info of the request, parsing the parameters or
* json body and writes the execution result to the response;
*/
@Override
public void run() {

if (!parseUsecase()) {
return;
}

if (!parseParams()) {
return;
}

Expand All @@ -165,15 +188,14 @@ else if (contentType.startsWith("multipart/form-data"))
this.headers.put(name, value);
}

// Actually runs the usecase;
this.usecase.execute(this);
UsecaseExecution<TParams, TResult> actual = this.usecase.execute(this.params, this.headers);

// Special case: SUCCESS
if (this.result_type == UsecaseResultType.SUCCESS) {
if (actual.result_type == UsecaseResultType.SUCCESS) {

// Does the implementation has a custom result writer to write to
// the output?
UsecaseResultWriter resultWriter = usecase.getResultWriter(this.result);
UsecaseResultWriter resultWriter = usecase.getResultWriter(actual.result);
if (resultWriter != null) {
try {
String contentDisposition = resultWriter.getContentDisposition();
Expand All @@ -190,16 +212,17 @@ else if (contentType.startsWith("multipart/form-data"))
}

// Falls back to writing itself as json to the output;
writeOutputAsJson();
writeOutputAsJson(actual);

}

/**
* Writes itself as json object to the output stream of the response;
*/
private void writeOutputAsJson() {
private void writeOutputAsJson(Object source) {
try {
response.setContentType("application/json");
MAPPER.writeValue(response.getOutputStream(), this);
MAPPER.writeValue(response.getOutputStream(), source);
} catch (Exception e) {
e.printStackTrace();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ protected void postFlow(UsecaseExecution<TParams, TResult> execution) throws Exc
*
* @param execution
*/
public final void execute(UsecaseExecution<TParams, TResult> execution) {
private final void execute(UsecaseExecution<TParams, TResult> execution) {

execution.execution_start = new Date();
execution.result_type = UsecaseResultType.NOT_EXECUTED;
Expand Down

0 comments on commit 65a92b3

Please sign in to comment.