BrightSide Workbench Full Report + Source Code
StringList.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2019 Lluis TurrĂ³ Cutiller <http://www.turro.org/>
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU Affero General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU Affero General Public License for more details.
14  *
15  * You should have received a copy of the GNU Affero General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  */
18 
19 package org.turro.fieldit;
20 
21 import java.io.BufferedReader;
22 import java.io.File;
23 import java.io.FileReader;
24 import java.io.FileWriter;
25 import java.io.IOException;
26 import java.io.PrintWriter;
27 import java.nio.charset.Charset;
28 import java.util.HashSet;
29 import java.util.Set;
30 import java.util.logging.Level;
31 import java.util.logging.Logger;
32 import java.util.stream.Collectors;
33 import org.turro.string.Strings;
34 import org.turro.contacts.FieldIt;
35 import org.turro.elephant.context.ElephantContext;
36 
41 public class StringList {
42 
43  private static final String FIELDS_ROOT = "/WEB-INF/fields";
44 
45  private String value;
46  private FieldIt field;
47 
48  public String getValue() {
49  return value;
50  }
51 
52  public void setValue(String value) {
53  this.value = value;
54  }
55 
56  public FieldIt getField() {
57  return field;
58  }
59 
60  public void setField(FieldIt field) {
61  this.field = field;
62  }
63 
64  public Set<String> getPossibleValues() {
65  Set<String> set = new HashSet<>();
66 
67  try (BufferedReader br = new BufferedReader(new FileReader(getValueFile(), Charset.forName(ElephantContext.getEncoding())))) {
68  set = br.lines().filter(line -> !Strings.isBlank(line)).collect(Collectors.toSet());
69  } catch (IOException ex) {
70  Logger.getLogger(StringList.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(null), ex);
71  }
72 
73  return set;
74  }
75 
76  public void setPossibleValues(Set<String> values) {
77  try (PrintWriter pw = new PrintWriter(new FileWriter(getValueFile(), Charset.forName(ElephantContext.getEncoding())))) {
78  values.forEach((line) -> {
79  if(!Strings.isBlank(line)) pw.println(line);
80  });
81  } catch (IOException ex) {
82  Logger.getLogger(StringList.class.getName()).log(Level.SEVERE, null, ex);
83  }
84  }
85 
86  public File getValueFile() {
87  return getValueFile(field);
88  }
89 
90  public static File getValueFile(FieldIt field) {
91  File root = new File(ElephantContext.getRealPath(FIELDS_ROOT));
92  if(!root.exists()) root.mkdir();
93  return new File(
94  ElephantContext.getRealPath(FIELDS_ROOT + "/" + Strings.toFile(field.getName())) +
95  "_" + field.getId());
96  }
97 
98 }
void setField(FieldIt field)
Definition: StringList.java:60
static File getValueFile(FieldIt field)
Definition: StringList.java:90
Set< String > getPossibleValues()
Definition: StringList.java:64
void setPossibleValues(Set< String > values)
Definition: StringList.java:76
void setValue(String value)
Definition: StringList.java:52