labelingsystem-server  Version 0.1.0.0
CSV Class Reference
Inheritance diagram for CSV:
Collaboration diagram for CSV:

Public Member Functions

void run ()
 

Static Public Member Functions

static void startImport ()
 
static String Input (String csvContent, String filename, String filelink, boolean validator, boolean check, String vocab) throws CsvLabelImportException, UniqueIdentifierException, IOException
 

Static Public Attributes

static String JSON_STRING = ""
 

Static Private Member Functions

static String createLabelTriples (String[] tokens, String creator) throws IOException, UniqueIdentifierException, CsvLabelImportException, exceptions.ConfigException, exceptions.SesameSparqlException
 

Static Private Attributes

static List< String > TRIPLE_LIST = new ArrayList()
 
static String vocabLanguage = ""
 
static String creator = ""
 
static boolean validation = false
 

Member Function Documentation

◆ createLabelTriples()

static String createLabelTriples ( String[]  tokens,
String  creator 
) throws IOException, UniqueIdentifierException, CsvLabelImportException, exceptions.ConfigException, exceptions.SesameSparqlException
staticprivate
301  {
302  String uuid = UniqueIdentifier.getHashID();
303  try {
304  // thumbnail[0];description[1];translations[2];END[3]
305  String thumbnail = tokens[0];
306  String description = tokens[1];
307  String[] translations = tokens[2].split(";");
308  RDF rdf = new RDF();
309  String label = "";
310  // typing
311  label = "";
312  label += "<" + rdf.getPrefixItem("ls_lab:" + uuid) + "> ";
313  label += "<" + rdf.getPrefixItem("rdf:type") + "> ";
314  label += "<" + rdf.getPrefixItem("ls:Label") + "> ";
315  label += ". ";
316  TRIPLE_LIST.add(label);
317  label = "";
318  label += "<" + rdf.getPrefixItem("ls_lab:" + uuid) + "> ";
319  label += "<" + rdf.getPrefixItem("rdf:type") + "> ";
320  label += "<" + rdf.getPrefixItem("skos:Concept") + "> ";
321  label += ". ";
322  TRIPLE_LIST.add(label);
323  // identifier
324  label = "";
325  label += "<" + rdf.getPrefixItem("ls_lab:" + uuid) + "> ";
326  label += "<" + rdf.getPrefixItem("dc:identifier") + "> ";
327  label += "\"" + uuid + "\" ";
328  label += ". ";
329  TRIPLE_LIST.add(label);
330  // creator
331  label = "";
332  label += "<" + rdf.getPrefixItem("ls_lab:" + uuid) + "> ";
333  label += "<" + rdf.getPrefixItem("dc:creator") + "> ";
334  label += "\"" + creator + "\" ";
335  label += ". ";
336  TRIPLE_LIST.add(label);
337  label = "";
338  label += "<" + rdf.getPrefixItem("ls_lab:" + uuid) + "> ";
339  label += "<" + rdf.getPrefixItem("dct:creator") + "> ";
340  label += "<" + rdf.getPrefixItem("ls_age:" + creator) + "> ";
341  label += ". ";
342  TRIPLE_LIST.add(label);
343  // inScheme
344  label = "";
345  label += "<" + rdf.getPrefixItem("ls_lab:" + uuid) + "> ";
346  label += "<" + rdf.getPrefixItem("skos:inScheme") + "> ";
347  label += "<" + rdf.getPrefixItem("ls_voc:" + ImportcsvResource.vocab) + "> ";
348  label += ". ";
349  TRIPLE_LIST.add(label);
350  // thumbnail
351  label = "";
352  label += "<" + rdf.getPrefixItem("ls_lab:" + uuid) + "> ";
353  label += "<" + rdf.getPrefixItem("ls:thumbnail") + "> ";
354  label += "\"" + thumbnail + "\"@" + vocabLanguage + " ";
355  label += ". ";
356  TRIPLE_LIST.add(label);
357  label = "";
358  label += "<" + rdf.getPrefixItem("ls_lab:" + uuid) + "> ";
359  label += "<" + rdf.getPrefixItem("skos:prefLabel") + "> ";
360  label += "\"" + thumbnail + "\"@" + vocabLanguage + " ";
361  label += ". ";
362  TRIPLE_LIST.add(label);
363  // language
364  label = "";
365  label += "<" + rdf.getPrefixItem("ls_lab:" + uuid) + "> ";
366  label += "<" + rdf.getPrefixItem("dc:language") + "> ";
367  label += "\"" + vocabLanguage + "\" ";
368  label += ". ";
369  TRIPLE_LIST.add(label);
370  // description
371  if (!description.equals("") && description != null) {
372  label = "";
373  label += "<" + rdf.getPrefixItem("ls_lab:" + uuid) + "> ";
374  label += "<" + rdf.getPrefixItem("skos:scopeNote") + "> ";
375  label += "\"" + description + "\"@" + vocabLanguage + " ";
376  label += ". ";
377  TRIPLE_LIST.add(label);
378  }
379  // translations
380  if (translations.length >= 1 && !translations[0].equals("")) {
381  for (int i = 0; i < translations.length; i = i + 2) {
382  label = "";
383  label += "<" + rdf.getPrefixItem("ls_lab:" + uuid) + "> ";
384  label += "<" + rdf.getPrefixItem("skos:prefLabel") + "> ";
385  label += "\"" + translations[i] + "\"@" + translations[i + 1] + " ";
386  label += ". ";
387  TRIPLE_LIST.add(label);
388  }
389  }
390  // date
391  Calendar cal = Calendar.getInstance();
392  Date time = cal.getTime();
393  DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
394  String d = formatter.format(time);
395  label = "";
396  label += "<" + rdf.getPrefixItem("ls_lab:" + uuid) + "> ";
397  label += "<" + rdf.getPrefixItem("dc:created") + "> ";
398  label += "\"" + d + "\" ";
399  label += ". ";
400  TRIPLE_LIST.add(label);
401  label = "";
402  label += "<" + rdf.getPrefixItem("ls_lab:" + uuid) + "> ";
403  label += "<" + rdf.getPrefixItem("dc:modified") + "> ";
404  label += "\"" + d + "\" ";
405  label += ". ";
406  TRIPLE_LIST.add(label);
407  // licence
408  label = "";
409  label += "<" + rdf.getPrefixItem("ls_lab:" + uuid) + "> ";
410  label += "<" + rdf.getPrefixItem("dct:license") + "> ";
411  label += "<" + "http://creativecommons.org/licenses/by/4.0/" + "> ";
412  label += ". ";
413  TRIPLE_LIST.add(label);
414  } catch (Exception e) {
415  throw new CsvLabelImportException(e.toString());
416  }
417  return uuid;
418  }

References CSV.creator, UniqueIdentifier.getHashID(), CSV.TRIPLE_LIST, ImportcsvResource.vocab, and CSV.vocabLanguage.

Referenced by CSV.Input().

◆ Input()

static String Input ( String  csvContent,
String  filename,
String  filelink,
boolean  validator,
boolean  check,
String  vocab 
) throws CsvLabelImportException, UniqueIdentifierException, IOException
static
66  {
67  JSONObject outputJSONobject = new JSONObject();
68  JSONArray errorArray = new JSONArray();
69  int errors = 0;
70  int importedlabels = 0;
71  boolean error = false;
72  try {
73  if (validator) {
74  ImportcsvResource.action = "check file and vocabularies (validate)...";
75  } else {
76  ImportcsvResource.action = "check file and vocabularies...";
77  }
78  TRIPLE_LIST.clear();
79  // thumbnail[0];description[1];translations[2];END[3]
80  // validator
81  String[] csvLines = csvContent.split("\r\n");
82  // check if vocabulary exists
83  RDF rdf = new RDF();
84  String query = rdf.getPREFIXSPARQL();
85  query += "SELECT * WHERE { ?v a ls:Vocabulary . ?v dc:identifier \"" + vocab + "\" . }";
86  List<BindingSet> voc_result = RDF4J_20.SPARQLquery(ConfigProperties.getPropertyParam("repository"), ConfigProperties.getPropertyParam("ts_server"), query);
87  List<String> voc_true = RDF4J_20.getValuesFromBindingSet_ORDEREDLIST(voc_result, "v");
88  if (voc_true.size() == 0) {
89  error = true;
90  errorArray.add("error: vocabulary not found");
91  errors++;
92  }
93  // get vocabulary language
94  rdf = new RDF();
95  query = rdf.getPREFIXSPARQL();
96  query += "SELECT * WHERE { ?vocab dc:identifier \"" + vocab + "\". ?vocab dc:language ?lang. }";
97  List<BindingSet> result = RDF4J_20.SPARQLquery(ConfigProperties.getPropertyParam("repository"), ConfigProperties.getPropertyParam("ts_server"), query);
98  HashSet<String> langList = RDF4J_20.getValuesFromBindingSet_UNIQUESET(result, "lang");
99  vocabLanguage = (String) langList.toArray()[0];
100  if (vocabLanguage == null) {
101  error = true;
102  errorArray.add("error: no vocabulary language");
103  errors++;
104  }
105  // get creator
106  rdf = new RDF();
107  query = rdf.getPREFIXSPARQL();
108  query += "SELECT * WHERE { ?vocab dc:identifier \"" + vocab + "\". ?vocab dc:creator ?creator. }";
109  result = RDF4J_20.SPARQLquery(ConfigProperties.getPropertyParam("repository"), ConfigProperties.getPropertyParam("ts_server"), query);
110  HashSet<String> creatorList = RDF4J_20.getValuesFromBindingSet_UNIQUESET(result, "creator");
111  creator = (String) creatorList.toArray()[0];
112  if (creator == null) {
113  error = true;
114  errorArray.add("error: no creator available");
115  errors++;
116  }
117  // check for empty lines
118  for (int i = 0; i < csvLines.length; i++) {
119  if (csvLines[i].equals("")) {
120  errorArray.add("error: empty line found in line " + (i + 1));
121  error = true;
122  errors++;
123  }
124  }
125  // check for header names
126  String[] header = csvLines[0].split("\t");
127  if (header == null) {
128  errorArray.add("error: no header existing");
129  error = true;
130  errors++;
131  // quit import
132  throw new IllegalArgumentException();
133  } else if (header.length == 4) {
134  if (header[0].contains("thumbnail") && header[1].contains("description") && header[2].contains("translations") && header[3].contains("end")) {
135  } else {
136  String headerStr = header[0] + "," + header[1] + "," + header[2] + "," + header[3];
137  errorArray.add("error: wrong header-names in first line -> " + headerStr);
138  error = true;
139  errors++;
140  }
141  } else {
142  errorArray.add("error: wrong header-names in first line");
143  error = true;
144  errors++;
145  // quit import
146  throw new IllegalArgumentException();
147  }
148  // check for line breaks
149  if (csvLines.length < 2) {
150  errorArray.add("error: linebreaks wrong");
151  error = true;
152  errors++;
153  }
154  // check for tabstop or wrong field-count
155  for (int i = 0; i < csvLines.length; i++) {
156  String[] tokens = csvLines[i].split("[\t]");
157  if (tokens.length != 4) {
158  errorArray.add("error: delimiter wrong or not enough fields (=4) in line " + (i + 1));
159  error = true;
160  errors++;
161  }
162  }
163  // thumbnail check
164  for (int i = 1; i < csvLines.length; i++) {
165  String[] tokens = csvLines[i].split("[\t]");
166  if (tokens[0].equals("")) {
167  errorArray.add("error: no thumbnail in line " + i);
168  error = true;
169  errors++;
170  }
171  }
172  // translation check
173  for (int i = 1; i < csvLines.length; i++) {
174  String[] tokens = csvLines[i].split("[\t]");
175  if (!tokens[2].equals("") && !tokens[2].contains(";")) {
176  errorArray.add("error: semocolon needed for translations in line " + (i + 1));
177  error = true;
178  errors++;
179  }
180  if (tokens[2].contains(";")) {
181  if (tokens[2].split(";").length % 2 != 0) {
182  errorArray.add("error: language or deliminiter error for translations in line " + (i + 1));
183  error = true;
184  errors++;
185  }
186  }
187  }
188  // language check for translations (same as thumbnail)
189  for (int i = 1; i < csvLines.length; i++) {
190  String[] tokens = csvLines[i].split("[\t]");
191  for (int j = 0; j < tokens.length; j = j + 2) {
192  if (tokens[j + 1].equals(vocabLanguage)) {
193  errorArray.add("error: translation language is same as thumbnail langauge in line " + (i + 1));
194  error = true;
195  errors++;
196  }
197  }
198  }
199  // language check for translations (allowed)
200  BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(LanguagesResource.class.getClassLoader().getResource("languages.json").getFile()), "UTF8"));
201  String inputLine;
202  StringBuilder response = new StringBuilder();
203  while ((inputLine = br.readLine()) != null) {
204  response.append(inputLine);
205  }
206  br.close();
207  JSONArray jsonArray = (JSONArray) new JSONParser().parse(response.toString());
208  List<String> languagesAllowed = new ArrayList();
209  for (Object obj : jsonArray) {
210  JSONObject lang = (JSONObject) obj;
211  languagesAllowed.add((String) lang.get("value"));
212  }
213  for (int i = 0; i < csvLines.length; i++) {
214  String[] tokens = csvLines[i].split("[\t]");
215  String[] translations = tokens[2].split(";");
216  List<String> languages = new ArrayList();
217  for (int j = 0; j < translations.length; j++) {
218  if (j % 2 != 0) {
219  languages.add(translations[j]);
220  }
221  }
222  for (String lang : languages) {
223  if (!languagesAllowed.contains(lang)) {
224  errorArray.add("error: a language for a translation not allowed in line " + (i + 1));
225  error = true;
226  errors++;
227  }
228  }
229  }
230  // replacements
231  for (int i = 1; i < csvLines.length; i++) {
232  String[] tokens = csvLines[i].split("[\t]");
233  tokens[0] = tokens[0].replace("\"", "'");
234  tokens[1] = tokens[1].replace("\"", "'");
235  tokens[2] = tokens[2].replace("\"", "'");
236  csvLines[i] = tokens[0] + "\t" + tokens[1] + "\t" + tokens[2] + "\t" + tokens[3] + "\r\n";
237  }
238  if (!error) {
239  // create triples
240  for (int i = 1; i < csvLines.length; i++) {
241  ImportcsvResource.currentStep = ImportcsvResource.currentStep + 1;
242  ImportcsvResource.status = ((double) ImportcsvResource.currentStep / (double) ImportcsvResource.maxSteps) * 100;
243  if (validator) {
244  ImportcsvResource.action = "check and import labels [line " + i + "] (check)...";
245  } else {
246  ImportcsvResource.action = "check and import labels [line " + i + "] ...";
247  }
248  try {
249  String[] tokens = csvLines[i].split("[\t]");
250  String labelID = CSV.createLabelTriples(tokens, creator);
251  if (!labelID.equals("")) {
252  importedlabels++;
253  }
254  } catch (Exception e) {
255  errorArray.add("error: " + e.toString());
256  error = true;
257  errors++;
258  }
259  }
260  // send triples to triplestore
261  try {
262  ImportcsvResource.action = "create RDF file...";
263  PrintWriter pw = new PrintWriter(ImportcsvResource.SERVER_UPLOAD_LOCATION_FOLDER + ImportcsvResource.FILENAME, "UTF-8");
264  for (String TRIPLE_LIST_ITEM : TRIPLE_LIST) {
265  pw.println(TRIPLE_LIST_ITEM);
266  }
267  pw.close();
268  if (!validator) {
269  RDF4J_20.SPARQLupdate(ConfigProperties.getPropertyParam("repository"), ConfigProperties.getPropertyParam("ts_server"), "LOAD <" + ImportcsvResource.FILELINK + ">");
270  }
271  } catch (Exception e) {
272  errorArray.add("error: " + e.toString());
273  error = true;
274  errors++;
275  }
276  }
277  } catch (Exception e) {
278  error = true;
279  } finally {
280  if (errors == 0) {
281  outputJSONobject.put("importedlabels", importedlabels);
282  outputJSONobject.put("triples", TRIPLE_LIST.size());
283  } else {
284  outputJSONobject.put("errors", errors);
285  outputJSONobject.put("messages", errorArray);
286  }
287  System.out.println(ImportcsvResource.status);
288  if (error) {
289  ImportcsvResource.status = 100.0;
290  }
291  ImportcsvResource.action = "done!"; // finish
292  String output = outputJSONobject.toString();
293  System.out.println("status: " + ImportcsvResource.status + " errors: " + errors + " triples: " + TRIPLE_LIST.size());
294  System.out.println(output);
295  validation = true;
296  JSON_STRING = output;
297  return output;
298  }
299  }

References ImportcsvResource.action, CSV.createLabelTriples(), CSV.creator, ImportcsvResource.currentStep, ImportcsvResource.FILELINK, ImportcsvResource.FILENAME, ConfigProperties.getPropertyParam(), CSV.JSON_STRING, ImportcsvResource.maxSteps, ImportcsvResource.SERVER_UPLOAD_LOCATION_FOLDER, ImportcsvResource.status, CSV.TRIPLE_LIST, CSV.validation, and CSV.vocabLanguage.

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

◆ run()

void run ( )
39  {
40  try {
41  if (ImportcsvResource.validator) {
42  Input(ImportcsvResource.csvContent, ImportcsvResource.FILENAME, ImportcsvResource.FILELINK, true, true, ImportcsvResource.vocab);
43  } else {
44  Input(ImportcsvResource.csvContent, ImportcsvResource.FILENAME, ImportcsvResource.FILELINK, true, false, ImportcsvResource.vocab);
45  if (validation) { // Real Input
46  Input(ImportcsvResource.csvContent, ImportcsvResource.FILENAME, ImportcsvResource.FILELINK, false, false, ImportcsvResource.vocab);
47  }
48  }
49  } catch (Exception e) {
50  Logger.getLogger(CSV.class.getName()).log(Level.SEVERE, null, e);
51  }
52  }

References ImportcsvResource.csvContent, ImportcsvResource.FILELINK, ImportcsvResource.FILENAME, CSV.Input(), CSV.validation, ImportcsvResource.validator, and ImportcsvResource.vocab.

◆ startImport()

static void startImport ( )
static
54  {
55  try {
56  Input(ImportcsvResource.csvContent, ImportcsvResource.FILENAME, ImportcsvResource.FILELINK, true, false, ImportcsvResource.vocab);
57  if (validation) {
58  Input(ImportcsvResource.csvContent, ImportcsvResource.FILENAME, ImportcsvResource.FILELINK, false, false, ImportcsvResource.vocab);
59  }
60  } catch (Exception e) {
61  Logger.getLogger(CSV.class.getName()).log(Level.SEVERE, null, e);
62  }
63  }

References ImportcsvResource.csvContent, ImportcsvResource.FILELINK, ImportcsvResource.FILENAME, CSV.Input(), CSV.validation, and ImportcsvResource.vocab.

Referenced by ImportcsvResource.importData().

Member Data Documentation

◆ creator

String creator = ""
staticprivate

◆ JSON_STRING

String JSON_STRING = ""
static

◆ TRIPLE_LIST

List<String> TRIPLE_LIST = new ArrayList()
staticprivate

◆ validation

boolean validation = false
staticprivate

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

◆ vocabLanguage

String vocabLanguage = ""
staticprivate
rdf
Definition: RDF.java:1
v1.utils.csv.CSV.validation
static boolean validation
Definition: CSV.java:36
Exception
v1.utils.csv.CSV.vocabLanguage
static String vocabLanguage
Definition: CSV.java:34
v1.utils.csv.CSV.JSON_STRING
static String JSON_STRING
Definition: CSV.java:32
v1.utils.csv.CSV.creator
static String creator
Definition: CSV.java:35
v1.utils.csv.CSV.Input
static String Input(String csvContent, String filename, String filelink, boolean validator, boolean check, String vocab)
Definition: CSV.java:65
v1.utils.csv.CSV.TRIPLE_LIST
static List< String > TRIPLE_LIST
Definition: CSV.java:33