Skip to content

csv backend #1

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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 build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ allprojects {
google()
jcenter()
maven { url "https://jitpack.io" } // for MPAndroidChard, not on jcenter yet
maven { url "https://dl.bintray.com/edu-cornell-tech-foundry/SDLRSX" }
}
}

Expand Down
5 changes: 5 additions & 0 deletions rsrp/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,15 @@ android {
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])

//noinspection GradleCompatible
implementation 'com.android.support:appcompat-v7:26.1.0'

implementation 'com.google.code.gson:gson:2.8.2'
implementation 'org.researchstack:backbone:1.1.2'
compile 'edu.cornell.tech.foundry:sdl_rsx:0.0.7'
compile 'edu.cornell.tech.foundry:sdl_rsx_rsrpsupport:0.0.3'
compile 'org.apache.httpcomponents:httpclient-android:4.3.5.1'
compile 'org.apache.commons:commons-lang3:3.6'

testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.1'
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package org.researchsuite.rsrp.CSVBackend;

/**
* Created by Christina on 1/31/2018.
*/

public interface CSVConvertible {

String getTypeString();
String getHeader();


}

Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package org.researchsuite.rsrp.CSVBackend;

public interface CSVEncodable extends CSVConvertible {

String[] toRecords();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,294 @@
package org.researchsuite.rsrp.CSVBackend;

import android.content.Context;
import android.os.Environment;

import org.apache.commons.lang3.StringUtils;
import org.apache.http.client.utils.URIBuilder;
import org.researchsuite.rsrp.Core.RSRPBackEnd;
import org.researchsuite.rsrp.Core.RSRPIntermediateResult;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;


public class RSRPCSVBackend implements RSRPBackEnd {

public URI outputDirectory;

public RSRPCSVBackend(URI outputDirectory){

this.outputDirectory = outputDirectory;

Boolean isDirectory = false;

if(shouldCreateDirectory(outputDirectory)){
this.createDirectory(outputDirectory);

}

}

public Boolean shouldCreateDirectory(URI outputDirectory){

File dir = new File(Environment.getExternalStorageDirectory() + outputDirectory.getPath());
if(dir.exists()){
if(dir.isDirectory()){
return false;
}
else {
this.removeDirectory(outputDirectory);
}
}

return true;

}


private void createDirectory(URI directory){
File dir = new File(Environment.getExternalStorageDirectory() + directory.getPath());
try {
dir.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}

private void removeDirectory(URI directory){
File dir = new File(Environment.getExternalStorageDirectory() + directory.getPath());
dir.delete();
}

private void removeItem(String itemName){

URIBuilder uriBuilder = null;
try {
uriBuilder = new URIBuilder(this.outputDirectory.toString());
URI uri = uriBuilder.setPath(uriBuilder.getPath() + itemName)
.build()
.normalize();
File dir = new File(Environment.getExternalStorageDirectory() + uri.getPath());
dir.delete();

} catch (URISyntaxException e) {
e.printStackTrace();
}
}

public void removeFileForType(String typeIdentifier){

String stringToAppend = typeIdentifier + ".csv";

URIBuilder uriBuilder = null;
try {
uriBuilder = new URIBuilder(this.outputDirectory.toString());
URI uri = uriBuilder.setPath(uriBuilder.getPath() + stringToAppend)
.build()
.normalize();
File dir = new File(Environment.getExternalStorageDirectory() + uri.getPath());
dir.delete();

} catch (URISyntaxException e) {
e.printStackTrace();
}

}

public void removeAll() {

this.removeDirectory(this.outputDirectory);
this.createDirectory(this.outputDirectory);

}

public void destroy() {
this.removeDirectory(this.outputDirectory);
}

private void addFile(String itemName, String text) {

URIBuilder uriBuilder = null;
String stringToAppend = itemName + ".csv";
try {
uriBuilder = new URIBuilder(this.outputDirectory.toString());
URI fileURL = uriBuilder.setPath(uriBuilder.getPath() + stringToAppend)
.build()
.normalize();

File dataFile = new File (fileURL);
FileOutputStream fOut = null;
try {
fOut = new FileOutputStream(dataFile);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
OutputStreamWriter myOutWriter = new OutputStreamWriter(fOut);
try {
myOutWriter.append(text);
myOutWriter.close();
fOut.close();
} catch (IOException e) {
e.printStackTrace();
}
} catch (URISyntaxException e){
e.printStackTrace();
}

//TODO: remember to set android permission for storage: android.permission.WRITE_EXTERNAL_STORAGE

}

public URL [] getFileURLs() {

return null;
}

public URL getFileURLForType(String typeIdentifier){

URIBuilder uriBuilder = null;
try {
uriBuilder = new URIBuilder(this.outputDirectory.toString());
URI uri = uriBuilder.setPath(uriBuilder.getPath() + typeIdentifier + ".csv")
.build()
.normalize();
File fileUrl = new File(Environment.getExternalStorageDirectory() + uri.getPath());
if (fileUrl.exists()){
try {
return fileUrl.toURI().toURL();
} catch (MalformedURLException e) {
e.printStackTrace();
}
}
else {
return null;
}
} catch (URISyntaxException e) {
e.printStackTrace();
}

return null;


}

private File getOrCreateFileForType(String typeIdentifier, String header){

URIBuilder uriBuilder = null;
String stringToAppend = typeIdentifier + ".csv";
try {
uriBuilder = new URIBuilder(this.outputDirectory.toString());
URI uri = uriBuilder.setPath(uriBuilder.getPath() + stringToAppend)
.build()
.normalize();
File fileUrl = new File(Environment.getExternalStorageDirectory() + uri.getPath());

if(fileUrl.exists()){
return fileUrl;
}
else {
try {
fileUrl.createNewFile();
FileOutputStream fOut = null;
try {
fOut = new FileOutputStream(fileUrl);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
OutputStreamWriter myOutWriter = new OutputStreamWriter(fOut);
try {
myOutWriter.append(header);
myOutWriter.append("\n");
myOutWriter.close();
fOut.close();
} catch (IOException e) {
e.printStackTrace();
}
} catch (IOException e) {
e.printStackTrace();
}
return fileUrl;
}

} catch (URISyntaxException e){

}

return null;
}

public void add(CSVEncodable[] csvRecords){

CSVEncodable first = csvRecords[0];


//TODO: complete this

}

public void add(CSVEncodable encodable){

String [] records = encodable.toRecords();
if (records.length == 0){
return;
}

this.add(encodable.getTypeString(),encodable.getHeader(),records,true);

}

public Boolean add(String typeString, String header, String[] records, Boolean shouldAddToQueue){

File fileHandle = this.getOrCreateFileForType(typeString,header);
String joinedRecords = StringUtils.join(records);

if(fileHandle != null) {

FileOutputStream fOut = null;
try {
fOut = new FileOutputStream(fileHandle,true);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
OutputStreamWriter myOutWriter = new OutputStreamWriter(fOut);
try {
myOutWriter.append(joinedRecords);
myOutWriter.append("\n");
myOutWriter.close();
fOut.close();
} catch (IOException e) {
e.printStackTrace();
}

return true;
}
else if (shouldAddToQueue){
// TODO: ADD TO TEMPQUEUE
return true;
}
else {
return false;
}
}

private void syncElements() {

}

@Override
public void add(Context context, RSRPIntermediateResult intermediateResult) {

CSVEncodable datapoint = (CSVEncodable) intermediateResult;
if (datapoint != null){
this.add(datapoint);
}

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package org.researchsuite.rsrp.CSVBackend;

/**
* Created by Christina on 1/31/2018.
*/

public class TempQueueElement {

public String typeString;
public String header;
public String[] records;

public TempQueueElement(String typeString, String header, String[] records){
this.typeString = typeString;
this.header = header;
this.records = records;
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package org.researchsuite.rsrp.CSVBackend_RSRPSupport;

import org.researchsuite.rsrp.Core.RSRPIntermediateResult;

import java.util.Map;
import java.util.UUID;

/**
* Created by jameskizer on 4/19/17.
*/

public class YADLFullRaw extends RSRPIntermediateResult {
public static String TYPE = "YADLFullRaw";

private Map<String, String> resultMap;
private Map<String, Object> schemaID;

public YADLFullRaw(
UUID uuid,
String taskIdentifier,
UUID taskRunUUID,
Map<String, Object> schemaID,
Map<String, String> resultMap) {

super(TYPE, uuid, taskIdentifier, taskRunUUID);
this.schemaID = schemaID;
this.resultMap = resultMap;
}

public Map<String, String> getResultMap() {
return resultMap;
}

public Map<String, Object> getSchemaID() {
return schemaID;
}
}
Loading