BrightSide Workbench Full Report + Source Code
MyDataList.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2014 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.contacts.mydata;
20 
21 import java.io.File;
22 import java.io.IOException;
23 import java.util.ArrayList;
24 import java.util.List;
25 import java.util.logging.Level;
26 import java.util.logging.Logger;
27 import org.turro.string.ObjectString;
28 import org.turro.string.Strings;
29 import org.jdom.Document;
30 import org.jdom.Element;
31 import org.jdom.JDOMException;
32 import org.jdom.input.SAXBuilder;
33 import org.turro.elephant.context.ElephantContext;
34 import org.turro.plugin.contacts.IContact;
35 import org.zkoss.zul.impl.InputElement;
36 
41 public class MyDataList extends ArrayList<MyDataItem> {
42 
43  private final IContact contact;
44 
45  public MyDataList(IContact contact) {
46  this.contact = contact;
47  readItems();
48  }
49 
50  public boolean isValid() {
51  for(MyDataItem item : this) {
52  if(item.isRequired()) {
53  if(item.getType().equals(MyDataItemType.MYDATA_NAME)) {
54  if(Strings.isBlank(contact.getName())) return false;
55  } else if(item.getType().equals(MyDataItemType.MYDATA_GIF)) {
56  if(Strings.isBlank(contact.getGlobalId())) return false;
57  } else if(item.getType().equals(MyDataItemType.MYDATA_CONNECTOR)) {
58  if(Strings.isBlank(contact.getConnector(item.getName()))) return false;
59  }
60  }
61  }
62  return true;
63  }
64 
65  public void saveItems() {
66  formToFields();
67  for(MyDataItem item : this) {
68  if(!Strings.isBlank(item.getValue())) {
69  if(item.getType().equals(MyDataItemType.MYDATA_NAME)) {
70  contact.setName(item.getValue());
71  } else if(item.getType().equals(MyDataItemType.MYDATA_GIF)) {
72  //contact.setGlobalId(item.getValue());
73  } else if(item.getType().equals(MyDataItemType.MYDATA_CONNECTOR)) {
74  contact.setConnector(item.getName(), item.getValue());
75  } else if(item.getType().equals(MyDataItemType.MYDATA_PASSWORD)) {
76  String password = item.getValue();
77  if(!Strings.isBlank(password)) {
78  contact.setPassword(password, password);
79  }
80  }
81  }
82  }
83  contact.applyChanges();
84  }
85 
86  private void readItems() {
87  File confFile = new File(ElephantContext.getRealPath("/WEB-INF/elephant/conf/my-data.xml"));
88  if(confFile.exists()) {
89  try {
90  Element conf = null;
91  SAXBuilder builder = new SAXBuilder();
92  Document doc;
93  doc = builder.build(confFile);
94  conf = doc.getRootElement();
95  if(conf != null) {
96  addItems(conf.getChild("my-data"));
97  }
98  } catch (JDOMException | IOException ex) {
99  Logger.getLogger(MyDataList.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(null), ex);
100  }
101  }
102  }
103 
104  private void addItems(Element myData) {
105  if(myData != null) {
106  for(Element item : (List<Element>) myData.getChildren()) {
107  MyDataItemType mdit = MyDataItemType.getMyDataType(item.getName());
108  if(mdit != null) {
109  MyDataItem mdi = new MyDataItem(mdit);
110  mdi.setRequired(item.getAttributeValue("required", "false").equals("true"));
111  mdi.setReadonly(item.getAttributeValue("readonly", "false").equals("true"));
112  mdi.setName(item.getAttributeValue("name"));
113  if(mdit.equals(MyDataItemType.MYDATA_NAME)) {
114  mdi.setLabel(item.getAttributeValue("label", "Full name"));
115  } else if(mdit.equals(MyDataItemType.MYDATA_GIF)) {
116  mdi.setLabel(item.getAttributeValue("label", "Global identifier"));
117  } else if(mdit.equals(MyDataItemType.MYDATA_PASSWORD)) {
118  mdi.setLabel(item.getAttributeValue("label", "New password"));
119  mdi.setRequired(false);
120  } else if(mdit.equals(MyDataItemType.MYDATA_CONNECTOR)) {
121  mdi.setLabel(item.getAttributeValue("label"));
122  } else if(mdit.equals(MyDataItemType.MYDATA_IMAGE)) {
123  mdi.setLabel(item.getAttributeValue("label"));
124  mdi.setValue(item.getAttributeValue("value"));
125  }
126  mdi.setCols((int) ObjectString.parseNativeString(item.getAttributeValue("cols"), Integer.class, true));
127  mdi.fillValue(contact);
128  add(mdi);
129  }
130  }
131  }
132  }
133 
134  private void formToFields() {
135  for(MyDataItem item : this) {
136  if(item.getInput() instanceof InputElement) {
137  item.setValue(item.getInput().getText());
138  }
139  }
140  }
141 
142 }
void setPassword(String newValue, String repeatValue)
void setConnector(String id, String value)