Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

♻️ Remove JsonObject from return value #505

Merged
merged 16 commits into from
Sep 27, 2021
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions classifai-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,10 @@
<groupId>org.openpnp</groupId>
<artifactId>opencv</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
</dependency>
</dependencies>


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,6 @@

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 io.vertx.core.json.JsonArray;
import io.vertx.core.json.JsonObject;
import lombok.extern.slf4j.Slf4j;

import java.io.IOException;
Expand All @@ -39,9 +34,9 @@ private DeleteProjectData() {
throw new IllegalStateException("Utility class");
}

public static JsonObject deleteProjectDataOnComplete(ProjectLoader loader, List<String> deleteUUIDList, JsonArray deletedDataPath) throws IOException {
public static List<String> deleteProjectDataOnComplete(ProjectLoader loader, List<String> deleteUUIDList,
List<String> deletedDataPathList) throws IOException {
List<String> dbUUIDList = loader.getUuidListFromDb();
List<String> deletedDataPathList = ConversionHandler.jsonArray2StringList(deletedDataPath);
if (dbUUIDList.removeAll(deleteUUIDList))
{
loader.setUuidListFromDb(dbUUIDList);
Expand All @@ -61,14 +56,9 @@ public static JsonObject deleteProjectDataOnComplete(ProjectLoader loader, List<
//update Portfolio Verticle
PortfolioVerticle.updateFileSystemUuidList(loader.getProjectId());

JsonObject response = ReplyHandler.getOkReply();
response.put(ParamConfig.getUuidListParam(), loader.getSanityUuidList());

return response;
}

return ReplyHandler.reportUserDefinedError(
"Failed to remove uuid from Portfolio Verticle. Project not expected to work fine");
return loader.getSanityUuidList();
devenyantis marked this conversation as resolved.
Show resolved Hide resolved
}

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

import ai.classifai.dto.ProjectConfigProperties;
import ai.classifai.loader.ProjectLoader;
import io.vertx.core.json.JsonObject;
import lombok.NonNull;
Expand All @@ -31,7 +32,7 @@
@Slf4j
public class FileGenerator {

public void run(@NonNull ProjectLoader loader, @NonNull JsonObject configContent, @NonNull int exportType) {
public void run(@NonNull ProjectLoader loader, @NonNull ProjectConfigProperties configContent, @NonNull int exportType) {
EventQueue.invokeLater(() -> {
String exportPath = null;
if(exportType == ActionConfig.ExportType.CONFIG_WITH_DATA.ordinal())
Expand Down
40 changes: 20 additions & 20 deletions classifai-core/src/main/java/ai/classifai/action/ProjectExport.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,13 @@

import ai.classifai.action.parser.PortfolioParser;
import ai.classifai.action.parser.ProjectParser;
import ai.classifai.dto.ImageDataProperties;
import ai.classifai.dto.ProjectConfigProperties;
import ai.classifai.loader.ProjectLoader;
import ai.classifai.util.ParamConfig;
import ai.classifai.util.data.ImageHandler;
import ai.classifai.util.datetime.DateTime;
import ai.classifai.util.project.ProjectHandler;
import io.vertx.core.json.JsonObject;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.vertx.sqlclient.Row;
import io.vertx.sqlclient.RowSet;
import lombok.*;
Expand All @@ -31,6 +32,7 @@
import java.io.*;
import java.nio.file.Paths;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
Expand All @@ -57,15 +59,7 @@ public enum ProjectExportStatus {
@Getter @Setter
private static String exportPath = "";

public static JsonObject getConfigSkeletonStructure()
{
return new JsonObject()
.put(ActionConfig.getToolParam(), ActionConfig.getToolName())
.put(ActionConfig.getToolVersionParam(), ActionConfig.getToolVersion())
.put(ActionConfig.getUpdatedDateParam(), new DateTime().toString());
}

public static String exportToFile(@NonNull String projectId, @NonNull JsonObject jsonObject)
public static String exportToFile(@NonNull String projectId, @NonNull ProjectConfigProperties configProperties)
{
ProjectLoader loader = Objects.requireNonNull(ProjectHandler.getProjectLoader(projectId));

Expand All @@ -76,7 +70,10 @@ public static String exportToFile(@NonNull String projectId, @NonNull JsonObject
{
FileWriter file = new FileWriter(configPath);

file.write(jsonObject.encodePrettily());
ObjectMapper mp = new ObjectMapper();
String jsonString = mp.writerWithDefaultPrettyPrinter().writeValueAsString(configProperties);

file.write(jsonString);

file.close();

Expand All @@ -90,7 +87,7 @@ public static String exportToFile(@NonNull String projectId, @NonNull JsonObject
return configPath;
}

public static String exportToFileWithData(ProjectLoader loader, String projectId, JsonObject configContent) throws IOException
public static String exportToFileWithData(ProjectLoader loader, String projectId, ProjectConfigProperties configContent) throws IOException
{
String configPath = exportToFile(projectId, configContent);
File zipFile = Paths.get(loader.getProjectPath().getAbsolutePath(), loader.getProjectName() + ".zip").toFile();
Expand Down Expand Up @@ -146,28 +143,31 @@ private static void addToEntry(File filePath, ZipOutputStream out, File dir) thr
}
}

public static JsonObject getConfigContent(@NonNull RowSet<Row> rowSet, @NonNull RowSet<Row> projectRowSet)
public static ProjectConfigProperties getConfigContent(@NonNull RowSet<Row> rowSet, @NonNull RowSet<Row> projectRowSet)
{
if(rowSet.size() == 0)
{
log.debug("Export project retrieve 0 rows. Project not found from portfolio database");
return null;
}

JsonObject configContent = getConfigSkeletonStructure();
PortfolioParser.parseOut(rowSet.iterator().next(), configContent);
ProjectConfigProperties projectConfig = PortfolioParser.parseOut(rowSet.iterator().next());
projectConfig.setToolName(ActionConfig.getToolName());
projectConfig.setToolVersion(ActionConfig.getToolVersion());
projectConfig.setUpdateDate(new DateTime().toString());

if(projectRowSet.size() == 0)
{
log.debug("Export project annotation retrieve 0 rows. Project not found from project database");
return null;
}

ProjectParser.parseOut(
configContent.getString(ParamConfig.getProjectPathParam()),
projectRowSet.iterator(), configContent);
Map<String, ImageDataProperties> configProperties = ProjectParser.parseOut(
projectConfig.getProjectPath(), projectRowSet.iterator());

projectConfig.setContent(configProperties);

return configContent;
return projectConfig;
}

public static ActionConfig.ExportType getExportType(String exportType)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import ai.classifai.action.ActionOps;
import ai.classifai.database.versioning.ProjectVersion;
import ai.classifai.database.versioning.Version;
import ai.classifai.dto.ProjectConfigProperties;
import ai.classifai.loader.ProjectLoader;
import ai.classifai.loader.ProjectLoaderStatus;
import ai.classifai.util.ParamConfig;
Expand Down Expand Up @@ -49,25 +50,25 @@ public class PortfolioParser
/**
* Extracting portfolio content and parse to JsonObject, preparing for saving
* @param row
* @param jsonObject
*/
public static void parseOut(@NonNull Row row, @NonNull JsonObject jsonObject)
public static ProjectConfigProperties parseOut(@NonNull Row row)
{
String annotationName = AnnotationHandler.getType(row.getInteger(2)).name();

jsonObject.put(ParamConfig.getProjectIdParam(), row.getString(0)); //project_id
jsonObject.put(ParamConfig.getProjectNameParam(), row.getString(1)); //project_name
jsonObject.put(ParamConfig.getAnnotationTypeParam(), annotationName.toLowerCase(Locale.ROOT)); //annotation_type (in string)
return ProjectConfigProperties.builder()
.projectID(row.getString(0))
.projectName(row.getString(1))
.annotationType(annotationName.toLowerCase(Locale.ROOT))
.projectPath(row.getString(3))
.isNew(row.getBoolean(4))
.isStarred(row.getBoolean(5))
.projectInfra(row.getString(6).toLowerCase())
.currentVersion(row.getString(7))
.projectVersion(row.getString(8))
.uuidVersionList(row.getString(9))
.labelVersionList(row.getString(10))
.build();

jsonObject.put(ParamConfig.getProjectPathParam(), row.getString(3)); //project_path
jsonObject.put(ParamConfig.getIsNewParam(), row.getBoolean(4)); //is_new
jsonObject.put(ParamConfig.getIsStarredParam(), row.getBoolean(5)); //is_starred

jsonObject.put(ParamConfig.getProjectInfraParam(), row.getString(6).toLowerCase()); //project_infra
jsonObject.put(ParamConfig.getCurrentVersionParam(), row.getString(7)); //current version
jsonObject.put(ParamConfig.getProjectVersionParam(), row.getString(8)); //project version
jsonObject.put(ParamConfig.getUuidVersionListParam(), row.getString(9)); //uuid_version_list
jsonObject.put(ParamConfig.getLabelVersionListParam(), row.getString(10)); //label_version_list
}

public static ProjectLoader parseIn(@NonNull JsonObject jsonObject)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,13 @@
package ai.classifai.action.parser;

import ai.classifai.database.annotation.AnnotationVerticle;
import ai.classifai.database.portfolio.PortfolioVerticle;
import ai.classifai.database.versioning.Annotation;
import ai.classifai.database.versioning.AnnotationVersion;
import ai.classifai.dto.DataInfoProperties;
import ai.classifai.dto.ImageDataProperties;
import ai.classifai.dto.AnnotationPointProperties;
import ai.classifai.dto.VersionConfigProperties;
import ai.classifai.loader.ProjectLoader;
import ai.classifai.util.Hash;
import ai.classifai.util.ParamConfig;
Expand All @@ -33,10 +38,7 @@

import java.io.File;
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.*;

/***
* Parsing Project Table in and out classifai with configuration file
Expand All @@ -47,9 +49,9 @@
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class ProjectParser
{
public static void parseOut(@NonNull String projectPath, @NonNull RowIterator<Row> rowIterator, @NonNull JsonObject jsonObject)
public static Map<String, ImageDataProperties> parseOut(@NonNull String projectPath, @NonNull RowIterator<Row> rowIterator)
{
JsonObject content = new JsonObject();
HashMap<String, ImageDataProperties> content = new HashMap<>();

while(rowIterator.hasNext())
{
Expand All @@ -61,23 +63,58 @@ public static void parseOut(@NonNull String projectPath, @NonNull RowIterator<Ro

String hash = Hash.getHash256String(new File(fullPath));

JsonObject annotationJsonObject = new JsonObject()
.put(ParamConfig.getCheckSumParam(), hash)
.put(ParamConfig.getImgPathParam(), imgPath) //img_path
.put(ParamConfig.getVersionListParam(), new JsonArray(row.getString(2))) //version_list
.put(ParamConfig.getImgDepth(), row.getInteger(3)) //img_depth
.put(ParamConfig.getImgOriWParam(), row.getInteger(4)) //img_ori_w
.put(ParamConfig.getImgOriHParam(), row.getInteger(5)) //img_ori_h
.put(ParamConfig.getFileSizeParam(), row.getInteger(6));
ImageDataProperties config = ImageDataProperties.builder()
.checksum(hash)
.imgPath(imgPath)
.versionList(getVersionList(row.getString(2)))
.imgDepth(row.getInteger(3))
.imgOriW(row.getInteger(4))
.imgOriH(row.getInteger(5))
.fileSize(row.getInteger(6))
.build();

//uuid, version, content
content.put(row.getString(0), annotationJsonObject);
content.put(row.getString(0), config);
}

jsonObject.put(ParamConfig.getProjectContentParam(), content);
return content;

}

private static List<VersionConfigProperties> getVersionList(String versionListString) {
JsonArray versionListArray = new JsonArray(versionListString);
devenyantis marked this conversation as resolved.
Show resolved Hide resolved

List<VersionConfigProperties> config = new ArrayList<>();

for(int i=0; i < versionListArray.size(); ++i) {
JsonObject jsonAnnotation = versionListArray.getJsonObject(i);
JsonObject annotationConfig = jsonAnnotation.getJsonObject(ParamConfig.getAnnotationDataParam());
JsonArray annotationConfigArray = annotationConfig.getJsonArray(ParamConfig.getAnnotationParam());
List<AnnotationPointProperties> annotationPoints = new ArrayList<>();
for(int j=0; j < annotationConfigArray.size(); ++j) {
JsonObject jsonAnnotationConfig = annotationConfigArray.getJsonObject(j);
annotationPoints.add(PortfolioVerticle.getAnnotations(jsonAnnotationConfig));
}

DataInfoProperties annotationData = DataInfoProperties.builder()
.annotation(annotationPoints)
.imgX(annotationConfig.getInteger(ParamConfig.getImgXParam()))
.imgY(annotationConfig.getInteger(ParamConfig.getImgYParam()))
.imgW(annotationConfig.getInteger(ParamConfig.getImgWParam()))
.imgH(annotationConfig.getInteger(ParamConfig.getImgHParam()))
.build();

VersionConfigProperties versionConfig = VersionConfigProperties.builder()
.versionUuid(jsonAnnotation.getString(ParamConfig.getVersionUuidParam()))
.annotationData(annotationData)
.build();

config.add(versionConfig);
}

return config;
}
devenyantis marked this conversation as resolved.
Show resolved Hide resolved

public static void parseIn(@NonNull ProjectLoader loader, @NonNull JsonObject contentJsonBody)
{
String projectId = loader.getProjectId();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,4 +79,7 @@ public static Handler<AsyncResult<RowSet<Row>>> handleResponse(@NonNull Consumer
};
}

public static Void toVoid(Object object) {
return null;
}
}
Loading