Skip to content

Commit

Permalink
Merge pull request #504 from CertifaiAI/remove_eventBus
Browse files Browse the repository at this point in the history
♻️ Remove event bus
  • Loading branch information
devenyantis authored Sep 14, 2021
2 parents 481d3ea + d3d0dc7 commit e94d907
Show file tree
Hide file tree
Showing 12 changed files with 517 additions and 358 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ public void start(Promise<Void> promise)
promise.fail(ar.cause());
}
});

}

public static void closeVerticles()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,17 @@
*/
package ai.classifai.action;

import ai.classifai.database.annotation.AnnotationQuery;
import ai.classifai.database.portfolio.PortfolioVerticle;
import ai.classifai.loader.ProjectLoader;
import ai.classifai.util.ParamConfig;
import ai.classifai.util.collection.ConversionHandler;
import ai.classifai.util.message.ReplyHandler;
import ai.classifai.util.project.ProjectHandler;
import io.vertx.core.eventbus.Message;
import io.vertx.core.json.JsonArray;
import io.vertx.core.json.JsonObject;
import io.vertx.jdbcclient.JDBCPool;
import io.vertx.sqlclient.Tuple;
import lombok.extern.slf4j.Slf4j;

import java.io.IOException;
import java.util.List;
import java.util.Objects;

/**
* Utility class for delete data
Expand All @@ -41,46 +35,12 @@
@Slf4j
public class DeleteProjectData {

private static ProjectLoader loader;
private static String projectId;

private DeleteProjectData() {
throw new IllegalStateException("Utility class");
}

public static void deleteProjectData(JDBCPool jdbcPool, Message<JsonObject> message)
{
projectId = message.body().getString(ParamConfig.getProjectIdParam());
loader = Objects.requireNonNull(ProjectHandler.getProjectLoader(projectId));

JsonArray UUIDListJsonArray = message.body().getJsonArray(ParamConfig.getUuidListParam());

List<String> deleteUUIDList = ConversionHandler.jsonArray2StringList(UUIDListJsonArray);
String uuidQueryParam = String.join(",", deleteUUIDList);

Tuple params = Tuple.of(projectId, uuidQueryParam);

jdbcPool.preparedQuery(AnnotationQuery.getDeleteProjectData())
.execute(params)
.onComplete(fetch -> {
if (fetch.succeeded())
{
try {
deleteProjectDataOnComplete(message, deleteUUIDList);
} catch (IOException e) {
log.info("Fail to delete. IO exception occurs.");
}
}
else
{
message.replyAndRequest(ReplyHandler.reportDatabaseQueryError(fetch.cause()));
}
});
}

private static void deleteProjectDataOnComplete(Message<JsonObject> message, List<String> deleteUUIDList) throws IOException {
public static JsonObject deleteProjectDataOnComplete(ProjectLoader loader, List<String> deleteUUIDList, JsonArray deletedDataPath) throws IOException {
List<String> dbUUIDList = loader.getUuidListFromDb();
JsonArray deletedDataPath = message.body().getJsonArray(ParamConfig.getImgPathListParam());
List<String> deletedDataPathList = ConversionHandler.jsonArray2StringList(deletedDataPath);
if (dbUUIDList.removeAll(deleteUUIDList))
{
Expand All @@ -99,17 +59,16 @@ private static void deleteProjectDataOnComplete(Message<JsonObject> message, Lis
}

//update Portfolio Verticle
PortfolioVerticle.updateFileSystemUuidList(projectId);
PortfolioVerticle.updateFileSystemUuidList(loader.getProjectId());

JsonObject response = ReplyHandler.getOkReply();
response.put(ParamConfig.getUuidListParam(), loader.getSanityUuidList());
message.replyAndRequest(response);
}
else
{
message.reply(ReplyHandler.reportUserDefinedError(
"Failed to remove uuid from Portfolio Verticle. Project not expected to work fine"));

return response;
}

return ReplyHandler.reportUserDefinedError(
"Failed to remove uuid from Portfolio Verticle. Project not expected to work fine");
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,14 @@
*/
package ai.classifai.action;

import ai.classifai.database.annotation.AnnotationQuery;
import ai.classifai.database.versioning.Annotation;
import ai.classifai.loader.ProjectLoader;
import ai.classifai.util.ParamConfig;
import ai.classifai.util.message.ReplyHandler;
import ai.classifai.util.project.ProjectHandler;
import io.vertx.core.eventbus.Message;
import io.vertx.core.json.JsonObject;
import io.vertx.jdbcclient.JDBCPool;
import io.vertx.sqlclient.Tuple;
import lombok.extern.slf4j.Slf4j;

import java.io.File;
import java.nio.file.Paths;
import java.util.Map;
import java.util.Objects;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

Expand All @@ -42,90 +34,28 @@
@Slf4j
public final class RenameProjectData {

private enum RenameDataErrorCode {
public enum RenameDataErrorCode {
RENAME_FAIL,
FILENAME_EXIST,
FILENAME_CONTAIN_ILLEGAL_CHAR,
RENAME_SUCCESS
}

private static ProjectLoader loader;
private static String dataUUID;
private static Annotation annotation;
private final ProjectLoader loader;
private Annotation annotation;

private RenameProjectData() {
throw new IllegalStateException("Utility class");
public RenameProjectData(ProjectLoader loader) {
this.loader = loader;
}

public static void renameProjectData(JDBCPool jdbcPool, Message<JsonObject> message)
{
String projectId = message.body().getString(ParamConfig.getProjectIdParam());
loader = Objects.requireNonNull(ProjectHandler.getProjectLoader(projectId));
dataUUID = message.body().getString(ParamConfig.getUuidParam());
getAnnotationVersion();

String newDataFileName = message.body().getString(ParamConfig.getNewFileNameParam());
if(containIllegalChars(newDataFileName)) {
// Abort if filename contain illegal chars
String illegalCharMes = "Contain illegal character";
message.reply(reportRenameError(RenameDataErrorCode.FILENAME_CONTAIN_ILLEGAL_CHAR.ordinal(), illegalCharMes));
return;
}

String oldDataFileName = getOldDataFileName();

String updatedFileName = modifyFileNameFromCache(newDataFileName);
File newDataPath = createNewDataPath(updatedFileName);

if(newDataPath.exists()) {
// Abort if name exists
String nameExistMes = "Name exists";
message.reply(reportRenameError(RenameDataErrorCode.FILENAME_EXIST.ordinal(), nameExistMes));
return;
}

Tuple params = Tuple.of(updatedFileName, dataUUID, projectId);

if(renameDataPath(newDataPath, oldDataFileName))
{
invokeJDBCPool(jdbcPool, message, params, newDataPath.toString());
updateAnnotationCache(updatedFileName);
}
else
{
String failRenameMes = "Fail to rename file";
message.reply(reportRenameError(RenameDataErrorCode.RENAME_FAIL.ordinal(), failRenameMes));
}

}

private static boolean containIllegalChars(String filename) {
public boolean containIllegalChars(String filename) {
Pattern pattern = Pattern.compile("[ `&:;'?$~#@=*+%{}<>/\\[\\]|\"^]");
Matcher matcher = pattern.matcher(filename);

return matcher.find();
}

private static void invokeJDBCPool(JDBCPool jdbcPool, Message<JsonObject> message, Tuple params, String newDataPath)
{
jdbcPool.preparedQuery(AnnotationQuery.getRenameProjectData())
.execute(params)
.onComplete(fetch -> {
if(fetch.succeeded())
{
JsonObject response = ReplyHandler.getOkReply();
response.put(ParamConfig.getImgPathParam(), newDataPath);
message.replyAndRequest(response);
}
else
{
String queryErrorMes = "Fail to update filename in database";
message.reply(reportRenameError(RenameDataErrorCode.RENAME_FAIL.ordinal(), queryErrorMes));
}
});
}

private static boolean renameDataPath(File newDataPath, String oldDataFileName)
public boolean renameDataPath(File newDataPath, String oldDataFileName)
{
File oldDataPath = new File(oldDataFileName);

Expand All @@ -134,12 +64,12 @@ private static boolean renameDataPath(File newDataPath, String oldDataFileName)
return oldDataPath.renameTo(newDataPath);
}

private static String getOldDataFileName()
public String getOldDataFileName()
{
return Paths.get(loader.getProjectPath().toString(), annotation.getImgPath()).toString();
}

private static String modifyFileNameFromCache(String newFileName)
public String modifyFileNameFromCache(String newFileName)
{
String oldDataPath = annotation.getImgPath();
// get only the filename after last slash before file extension
Expand All @@ -154,7 +84,7 @@ private static String modifyFileNameFromCache(String newFileName)
return newDataPathModified;
}

private static File createNewDataPath(String newDataFileName)
public File createNewDataPath(String newDataFileName)
{
File newDataPath = Paths.get(
loader.getProjectPath().toString(), newDataFileName).toFile();
Expand All @@ -163,13 +93,13 @@ private static File createNewDataPath(String newDataFileName)
return newDataPath;
}

private static void getAnnotationVersion()
public void getAnnotationVersion(String dataUUID)
{
Map<String, Annotation> uuidAnnotationDict = loader.getUuidAnnotationDict();
annotation = uuidAnnotationDict.get(dataUUID);
}

private static void updateAnnotationCache(String newImagePath)
public void updateAnnotationCache(String newImagePath, String dataUUID)
{
Map<String, Annotation> uuidAnnotationDict = loader.getUuidAnnotationDict();

Expand All @@ -180,7 +110,7 @@ private static void updateAnnotationCache(String newImagePath)

}

private static JsonObject reportRenameError(int errorKey, String errorMessage)
public JsonObject reportRenameError(int errorKey, String errorMessage)
{
log.info(errorMessage);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,4 @@ public interface AnnotationServiceable

void deleteProject(Message<JsonObject> message);

void deleteProjectData(Message<JsonObject> message);
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@
package ai.classifai.database.annotation;


import ai.classifai.action.DeleteProjectData;
import ai.classifai.action.RenameProjectData;
import ai.classifai.action.parser.ProjectParser;
import ai.classifai.database.DBUtils;
import ai.classifai.database.VerticleServiceable;
Expand Down Expand Up @@ -112,7 +110,7 @@ public void retrieveDataPath(Message<JsonObject> message)
));
}

private static File getDataFullPath(@NonNull String projectId, @NonNull String dataSubPath)
public static File getDataFullPath(@NonNull String projectId, @NonNull String dataSubPath)
{
ProjectLoader loader = Objects.requireNonNull(ProjectHandler.getProjectLoader(projectId));

Expand Down Expand Up @@ -208,7 +206,7 @@ public static void configProjectLoaderFromDb(@NonNull ProjectLoader loader)
result -> {
if(result.size() == 0)
{
log.debug("Extract project annotation retrieve 0 rows. Project not found from project database");
log.info("Extract project annotation retrieve 0 rows. Project not found from project database");
}
else
{
Expand Down Expand Up @@ -352,11 +350,6 @@ public void deleteProject(Message<JsonObject> message)
.onComplete(DBUtils.handleEmptyResponse(message));
}

public void deleteProjectData(Message<JsonObject> message)
{
DeleteProjectData.deleteProjectData(jdbcPool, message);
}

public void updateData(Message<JsonObject> message, @NonNull String annotationKey)
{
JsonObject requestBody = message.body();
Expand Down Expand Up @@ -476,9 +469,4 @@ public void queryData(Message<JsonObject> message, @NonNull String annotationKey

message.replyAndRequest(response);
}

public void renameProjectData(Message<JsonObject> message)
{
RenameProjectData.renameProjectData(jdbcPool, message);
}
}
Loading

0 comments on commit e94d907

Please sign in to comment.