Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,4 @@
hs_err_pid*

/target/
/bin/
2 changes: 0 additions & 2 deletions bin/.gitignore

This file was deleted.

7 changes: 6 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>tercen_java_client</groupId>
<groupId>com.tercen</groupId>
<artifactId>tercen_java_client</artifactId>
<version>0.0.1-SNAPSHOT</version>
<repositories>
Expand Down Expand Up @@ -31,6 +31,11 @@
<version>2.9.8</version>
</dependency>

<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.7</version>
</dependency>

<!-- https://mvnrepository.com/artifact/junit/junit -->
<dependency>
Expand Down
40 changes: 40 additions & 0 deletions src/com/tercen/examples/SelectData.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package com.tercen.examples;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import com.tercen.client.impl.TercenClient;
import com.tercen.model.impl.Schema;
import com.tercen.model.impl.Table;
import com.tercen.service.ServiceError;

public class SelectData {

private static final String LOCALHOST_URL = "http://10.0.2.2:5402/";
private static final String DOMAIN = "tercen";
private static final String USERNAME = "test";
private static final String PASSWORD = "test";
private static final String TABLE_ID = "06b38fe91c2ed72a148b3fa38702b81e";

public static void main(String[] args) {
TercenClient client = new TercenClient(SelectData.LOCALHOST_URL);

try {
client.userService.connect2(DOMAIN, USERNAME, PASSWORD);

Schema schema = client.tableSchemaService.get(TABLE_ID);
List<String> list = schema.columns.stream().map(c -> c.name).collect(Collectors.toList());
ArrayList<String> cnames = new ArrayList<String>(list);

Table result = client.tableSchemaService.select(TABLE_ID, cnames, 0, 100);
double[] colvals = (double []) result.columns.get(0).values;
for (double d : (double[]) colvals) {
System.out.println(d);
}

} catch (ServiceError e) {
e.printStackTrace();
}
}
}
52 changes: 52 additions & 0 deletions src/com/tercen/examples/UploadData.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package com.tercen.examples;

import java.io.IOException;

import com.tercen.client.impl.TercenClient;
import com.tercen.model.impl.FileDocument;
import com.tercen.model.impl.Project;
import com.tercen.service.ServiceError;
import com.tercen.util.Utils;

public class UploadData {

private static final String FILE_NAME = "src/main/resources/crabs-long.csv";
private static final String TEAM_NAME = "test-team";
private static final String PROJECT_NAME = "myproject";
private static final String LOCALHOST_URL = "http://10.0.2.2:5402/";
private static final String DOMAIN = "tercen";
private static final String USERNAME = "test";
private static final String PASSWORD = "test";

private static String type = "zip"; // either "zip", "csv" or "txt"

public static void main(String[] args) {
TercenClient client = new TercenClient(UploadData.LOCALHOST_URL);

try {
client.userService.connect2(DOMAIN, USERNAME, PASSWORD);
final Project project = Utils.getProject(client, TEAM_NAME, PROJECT_NAME);

if (type.equals("csv")) {
FileDocument result = Utils.uploadCsvFile(LOCALHOST_URL, TEAM_NAME, PROJECT_NAME, DOMAIN, USERNAME, PASSWORD, FILE_NAME);
System.out.println(result.toJson());
System.out.println("CSV file uploaded");
} else if (type.equals("zip")) {
FileDocument result = Utils.uploadZipFile(LOCALHOST_URL, TEAM_NAME, PROJECT_NAME, DOMAIN, USERNAME, PASSWORD, FILE_NAME);
System.out.println(result.toJson());
System.out.println("Zip file uploaded");
} else if (type.equals("txt")) {
FileDocument file = new FileDocument();
file.name = "hello.txt";
file.projectId = project.id;
file = client.fileService.upload(file, "hello".getBytes());
System.out.println(file.toJson());
System.out.println("Txt file uploaded");
}
} catch (ServiceError e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
129 changes: 129 additions & 0 deletions src/com/tercen/util/Utils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
package com.tercen.util;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Optional;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

import org.apache.commons.io.FileUtils;

import com.tercen.client.impl.TercenClient;
import com.tercen.model.impl.CSVFileMetadata;
import com.tercen.model.impl.FileDocument;
import com.tercen.model.impl.Project;
import com.tercen.model.impl.ProjectDocument;
import com.tercen.service.ServiceError;

public class Utils {

private static final String Separator = "/";

private static String getFilename(String fullFileName) {
String[] filenameParts = fullFileName.replaceAll(Pattern.quote(Utils.Separator), "\\\\").split("\\\\");
return filenameParts[filenameParts.length - 1];
}

public static Project getProject(TercenClient client, String teamOrUser, String projectName)
throws ServiceError {

List startKey = List.of(teamOrUser, false, "2000");
List endKey = List.of(teamOrUser, false, "2100");

List<Project> projects = client.projectService.findByTeamAndIsPublicAndLastModifiedDate(startKey, endKey, 1000,
0, false, true);

Optional<Project> result = projects.stream().filter(p -> p.name.equals(projectName)).findAny();

if (result.isPresent()) {
return result.get();
}

Project new_project = new Project();
new_project.name = projectName;
new_project.acl.owner = teamOrUser;

return client.projectService.create(new_project);
}

private static void removeProjectFileIfExists(TercenClient client, Project project, String filename) throws ServiceError {
List<ProjectDocument> projectDocs = client.projectDocumentService.findProjectObjectsByLastModifiedDate(null, null, 100, 0, true, false);
if (projectDocs.size() > 0) {
projectDocs = projectDocs
.stream()
.filter(p -> p.projectId.equals(project.id))
.filter(p -> p.name.equals(filename))
.collect(Collectors.toList());

projectDocs.forEach(p -> {
try {
client.fileService.delete(p.id, p.rev);
} catch (ServiceError e) {
e.printStackTrace();
}
});
}
}

public static FileDocument uploadZipFile(String url, String teamName, String projectName, String domain, String username, String password, String fullFileName) throws ServiceError, IOException {
// Write data to tercen
TercenClient client = new TercenClient(url);
client.userService.connect2(domain, username, password);
Project project = getProject(client, teamName, projectName);

FileDocument fileDoc = new FileDocument();
String filename = getFilename(fullFileName);
int index = filename.lastIndexOf(".");
String ext = filename.substring(index);
String outFilename = filename.replace(ext, ".zip");
fileDoc.name = outFilename;
fileDoc.projectId = project.id;
fileDoc.acl.owner = project.acl.owner;
fileDoc.metadata.contentType = "application/zip";
fileDoc.metadata.contentEncoding = "zip,iso-8859-1";
File file = new File(fullFileName);

ByteArrayOutputStream bos = new ByteArrayOutputStream();
ZipOutputStream zos = new ZipOutputStream(bos);
ZipEntry entry = new ZipEntry(file.getName());
byte[] bytes = FileUtils.readFileToByteArray(file);
zos.putNextEntry(entry);
zos.write(bytes, 0, bytes.length);
zos.closeEntry();
zos.close();

byte[] zipBytes = bos.toByteArray();
bos.close();
// remove existing file and upload new file
removeProjectFileIfExists(client, project, outFilename);
return client.fileService.upload(fileDoc, zipBytes);
}

public static FileDocument uploadCsvFile(String url, String teamName, String projectName, String domain, String username, String password, String fullFileName) throws ServiceError, IOException {
// Write data to tercen
TercenClient client = new TercenClient(url);
client.userService.connect2(domain, username, password);
Project project = getProject(client, teamName, projectName);

FileDocument fileDoc = new FileDocument();
String filename = getFilename(fullFileName);
fileDoc.name = filename;
fileDoc.projectId = project.id;
fileDoc.acl.owner = project.acl.owner;
fileDoc.metadata = new CSVFileMetadata();
fileDoc.metadata.contentType = "text/csv";
((CSVFileMetadata) fileDoc.metadata).separator = ",";
((CSVFileMetadata) fileDoc.metadata).quote = "\"";
fileDoc.metadata.contentEncoding = "iso-8859-1";
File file = new File(fullFileName);
byte[] bytes = FileUtils.readFileToByteArray(file);

// remove existing file and upload new file
removeProjectFileIfExists(client, project, filename);
return client.fileService.upload(fileDoc, bytes);
}
}
Loading