labelingsystem-server  Version 0.1.0.0
ImportcsvResource Class Reference
Collaboration diagram for ImportcsvResource:

Public Member Functions

Response csvUpload (@FormDataParam("fileName") InputStream fileInputStream, @FormDataParam("fileName") FormDataContentDisposition contentDispositionHeader, @PathParam("mode") String MODE, @PathParam("validator") String VALIDATOR, @PathParam("vocab") String VOCAB)
 
Response csvUploadDirectResponse (@FormDataParam("fileName") InputStream fileInputStream, @FormDataParam("fileName") FormDataContentDisposition contentDispositionHeader, @PathParam("vocab") String VOCAB)
 
Response csvUploadUpdate (@PathParam("mode") String MODE)
 
void start ()
 
void update ()
 
void finish ()
 
void importData ()
 

Public Attributes

String outString = ""
 

Static Public Attributes

static double status = -1.0
 
static String action = ""
 
static String creator = null
 
static String vocab = null
 
static boolean preflabel = false
 
static boolean validator = true
 
static String csvContent = ""
 
static String mode = null
 
static int maxSteps = -1
 
static int currentStep = -1
 
static String CONTEXT = null
 
static String FILENAME = null
 
static String FILELINK = null
 
static String SERVER_UPLOAD_LOCATION_FOLDER = ""
 

Private Member Functions

void saveFile (InputStream uploadedInputStream, String serverLocation)
 

Static Private Attributes

static String SHARE_WEB = ""
 

Member Function Documentation

◆ csvUpload()

Response csvUpload ( @FormDataParam("fileName") InputStream  fileInputStream,
@FormDataParam("fileName") FormDataContentDisposition  contentDispositionHeader,
@PathParam("mode") String  MODE,
@PathParam("validator") String  VALIDATOR,
@PathParam("vocab") String  VOCAB 
)
56  {
57  try {
58  if (MODE.equals("start")) {
59  mode = MODE;
60  vocab = VOCAB;
61  validator = Boolean.valueOf(VALIDATOR);
62  SHARE_WEB = ConfigProperties.getPropertyParam("share_web");
63  SERVER_UPLOAD_LOCATION_FOLDER = ConfigProperties.getPropertyParam("share_server");
64  }
65  CONTEXT = String.valueOf(System.currentTimeMillis());
66  FILENAME = CONTEXT + ".ttl";
68  String filePath = SERVER_UPLOAD_LOCATION_FOLDER + contentDispositionHeader.getFileName();
69  // save the file to the server
70  saveFile(fileInputStream, filePath);
71  outString = "File saved to server location : " + filePath;
72  String line = "";
73  File datei = new File(filePath);
74  FileInputStream fis = new FileInputStream(filePath);
75  InputStreamReader isr = new InputStreamReader(fis, "UTF8");
76  BufferedReader br = new BufferedReader(isr);
77  csvContent = "";
78  while ((line = br.readLine()) != null) {
79  csvContent += line + "\r\n";
80  }
81  br.close();
82  isr.close();
83  fis.close();
84  if (datei.exists()) {
85  datei.delete();
86  }
87  start();
88  return Response.status(200).entity(outString).header("Content-Type", "application/json;charset=UTF-8").build();
89  } catch (Exception e) {
90  return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(Logging.getMessageJSON(e, "v1.rest.ImportcsvResource"))
91  .header("Content-Type", "application/json;charset=UTF-8").build();
92  }
93  }

References Logging.getMessageJSON(), and ConfigProperties.getPropertyParam().

◆ csvUploadDirectResponse()

Response csvUploadDirectResponse ( @FormDataParam("fileName") InputStream  fileInputStream,
@FormDataParam("fileName") FormDataContentDisposition  contentDispositionHeader,
@PathParam("vocab") String  VOCAB 
)
99  {
100  try {
101  vocab = VOCAB;
102  validator = false;
103  SHARE_WEB = ConfigProperties.getPropertyParam("share_web");
104  SERVER_UPLOAD_LOCATION_FOLDER = ConfigProperties.getPropertyParam("share_server");
105  CONTEXT = String.valueOf(System.currentTimeMillis());
106  FILENAME = CONTEXT + ".ttl";
108  if (!contentDispositionHeader.getFileName().contains(".csv")) {
109  throw new DataFormatException();
110  }
111  String filePath = SERVER_UPLOAD_LOCATION_FOLDER + contentDispositionHeader.getFileName();
112  // save the file to the server
113  saveFile(fileInputStream, filePath);
114  outString = "File saved to server location : " + filePath;
115  String line = "";
116  File datei = new File(filePath);
117  FileInputStream fis = new FileInputStream(filePath);
118  InputStreamReader isr = new InputStreamReader(fis, "UTF8");
119  BufferedReader br = new BufferedReader(isr);
120  csvContent = "";
121  while ((line = br.readLine()) != null) {
122  csvContent += line + "\r\n";
123  }
124  br.close();
125  isr.close();
126  fis.close();
127  if (datei.exists()) {
128  datei.delete();
129  }
130  importData();
131  if (CSV.JSON_STRING.contains("errors")) {
132  return Response.status(Response.Status.BAD_REQUEST).entity(CSV.JSON_STRING).header("Content-Type", "application/json;charset=UTF-8").build();
133  } else {
134  // trigger for create statistics
135  Transformer.writeVocabularyStatisticsToDatabase(vocab);
136  // output
137  return Response.status(200).entity(CSV.JSON_STRING).header("Content-Type", "application/json;charset=UTF-8").build();
138  }
139  } catch (Exception e) {
140  if (e.toString().contains("DataFormatException")) {
141  JSONObject errorOutput = new JSONObject();
142  JSONArray errorArray = new JSONArray();
143  errorArray.add("error: wrong file type, must be .csv");
144  errorOutput.put("errors", 1);
145  errorOutput.put("messages", errorArray);
146  return Response.status(Response.Status.BAD_REQUEST).entity(errorOutput).header("Content-Type", "application/json;charset=UTF-8").build();
147  } else {
148  return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(Logging.getMessageJSON(e, "v1.rest.ImportcsvResource"))
149  .header("Content-Type", "application/json;charset=UTF-8").build();
150  }
151  }
152  }

References Logging.getMessageJSON(), ConfigProperties.getPropertyParam(), CSV.JSON_STRING, and Transformer.writeVocabularyStatisticsToDatabase().

◆ csvUploadUpdate()

Response csvUploadUpdate ( @PathParam("mode") String  MODE)
157  {
158  try {
159  if (MODE.equals("update")) {
160  update();
161  return Response.status(200).entity(outString).header("Content-Type", "application/json;charset=UTF-8").build();
162  } else if (MODE.equals("finish")) {
163  finish();
164  return Response.status(200).entity(outString).header("Content-Type", "application/json;charset=UTF-8").build();
165  } else {
166  throw new IllegalArgumentException();
167  }
168  } catch (Exception e) {
169  return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(Logging.getMessageJSON(e, "v1.rest.ImportcsvResource"))
170  .header("Content-Type", "application/json;charset=UTF-8").build();
171  }
172  }

References Logging.getMessageJSON().

◆ finish()

void finish ( )
195  {
196  outString = CSV.JSON_STRING;
197  }

References CSV.JSON_STRING.

◆ importData()

void importData ( )
199  {
200  status = 0.0;
201  currentStep = 0;
202  action = "start parsing...";
203  System.out.println("=================================");
204  String[] csvLines = csvContent.split("\r\n");
205  maxSteps = (csvLines.length - 1) * 2; // 100% (2 = label + label (validate))
206  System.out.println("csv-input");
207  CSV.startImport();
208  }

References CSV.startImport().

◆ saveFile()

void saveFile ( InputStream  uploadedInputStream,
String  serverLocation 
)
private
210  {
211  try {
212  OutputStream outpuStream = new FileOutputStream(new File(serverLocation));
213  int read = 0;
214  byte[] bytes = new byte[1024];
215  outpuStream = new FileOutputStream(new File(serverLocation));
216  while ((read = uploadedInputStream.read(bytes)) != -1) {
217  outpuStream.write(bytes, 0, read);
218  }
219  outpuStream.flush();
220  outpuStream.close();
221  } catch (IOException e) {
222  }
223  }

◆ start()

void start ( )
174  {
175  status = 0.0;
176  currentStep = 0;
177  action = "start parsing...";
178  System.out.println("=================================");
179  String[] csvLines = csvContent.split("\r\n");
180  if (validator) {
181  maxSteps = (csvLines.length - 1) * 1; // 100% (1 = label)
182  System.out.println("csv-test");
183  } else {
184  maxSteps = (csvLines.length - 1) * 2; // 100% (2 = label + label (validate))
185  System.out.println("csv-input");
186  }
187  (new Thread(new CSV())).start();
188  update();
189  }

◆ update()

void update ( )
191  {
192  outString = "{ \"status\": \"" + status + "\", \"action\": \"" + action + "\"}";
193  }

Member Data Documentation

◆ action

String action = ""
static

Referenced by CSV.Input().

◆ CONTEXT

String CONTEXT = null
static

◆ creator

String creator = null
static

◆ csvContent

String csvContent = ""
static

Referenced by CSV.run(), and CSV.startImport().

◆ currentStep

int currentStep = -1
static

Referenced by CSV.Input().

◆ FILELINK

String FILELINK = null
static

Referenced by CSV.Input(), CSV.run(), and CSV.startImport().

◆ FILENAME

String FILENAME = null
static

Referenced by CSV.Input(), CSV.run(), and CSV.startImport().

◆ maxSteps

int maxSteps = -1
static

Referenced by CSV.Input().

◆ mode

String mode = null
static

◆ outString

String outString = ""

◆ preflabel

boolean preflabel = false
static

◆ SERVER_UPLOAD_LOCATION_FOLDER

String SERVER_UPLOAD_LOCATION_FOLDER = ""
static

Referenced by CSV.Input().

◆ SHARE_WEB

String SHARE_WEB = ""
staticprivate

◆ status

double status = -1.0
static

Referenced by CSV.Input().

◆ validator

boolean validator = true
static

Referenced by CSV.run().

◆ vocab

String vocab = null
static
v1.rest.ImportcsvResource.FILELINK
static String FILELINK
Definition: ImportcsvResource.java:44
v1.rest.ImportcsvResource.saveFile
void saveFile(InputStream uploadedInputStream, String serverLocation)
Definition: ImportcsvResource.java:210
v1.rest.ImportcsvResource.action
static String action
Definition: ImportcsvResource.java:33
v1.rest.ImportcsvResource.update
void update()
Definition: ImportcsvResource.java:191
v1.rest.ImportcsvResource.SERVER_UPLOAD_LOCATION_FOLDER
static String SERVER_UPLOAD_LOCATION_FOLDER
Definition: ImportcsvResource.java:47
v1.rest.ImportcsvResource.maxSteps
static int maxSteps
Definition: ImportcsvResource.java:40
v1.rest.ImportcsvResource.vocab
static String vocab
Definition: ImportcsvResource.java:35
v1.rest.ImportcsvResource.mode
static String mode
Definition: ImportcsvResource.java:39
v1.rest.ImportcsvResource.currentStep
static int currentStep
Definition: ImportcsvResource.java:41
v1.rest.ImportcsvResource.status
static double status
Definition: ImportcsvResource.java:32
Exception
v1.rest.ImportcsvResource.FILENAME
static String FILENAME
Definition: ImportcsvResource.java:43
v1.rest.ImportcsvResource.SHARE_WEB
static String SHARE_WEB
Definition: ImportcsvResource.java:48
v1.rest.ImportcsvResource.validator
static boolean validator
Definition: ImportcsvResource.java:37
v1.rest.ImportcsvResource.CONTEXT
static String CONTEXT
Definition: ImportcsvResource.java:42
v1.rest.ImportcsvResource.start
void start()
Definition: ImportcsvResource.java:174
v1.rest.ImportcsvResource.outString
String outString
Definition: ImportcsvResource.java:45
v1.rest.ImportcsvResource.csvContent
static String csvContent
Definition: ImportcsvResource.java:38
v1.rest.ImportcsvResource.importData
void importData()
Definition: ImportcsvResource.java:199
v1.rest.ImportcsvResource.finish
void finish()
Definition: ImportcsvResource.java:195