BrightSide Workbench Full Report + Source Code
XMLHumanResourceSet.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2012 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.erp.time;
20 
21 import java.io.File;
22 import java.io.IOException;
23 import java.io.OutputStreamWriter;
24 import java.util.List;
25 import java.util.TreeSet;
26 import java.util.logging.Level;
27 import java.util.logging.Logger;
28 import org.turro.string.ObjectString;
29 import org.jdom.Document;
30 import org.jdom.Element;
31 import org.jdom.JDOMException;
32 import org.jdom.input.SAXBuilder;
33 import org.jdom.output.Format;
34 import org.jdom.output.XMLOutputter;
35 import org.turro.elephant.context.ElephantContext;
36 import org.turro.elephant.context.IConstructor;
37 import org.turro.elephant.impl.util.FileUtil;
38 
43 public class XMLHumanResourceSet extends TreeSet<XMLHumanResource> {
44 
45  public static final String XML_HUMANRESOURCE = "/WEB-INF/local/humanResource.xml";
46 
47  private IConstructor constructor;
48 
49  public XMLHumanResourceSet(IConstructor constructor) {
50  this.constructor = constructor;
51  loadCollection();
52  }
53 
54  public String getName(XMLTimeControl tc) {
55  for(XMLHumanResource hr : this) {
56  if(hr.getId() == tc.getHumanResourceId()) {
57  return hr.getName();
58  }
59  }
60  return null;
61  }
62 
63  public void saveCollection() {
64  Document doc = new Document(new Element("human-resources"));
65  writeXML(doc.getRootElement());
66  try {
67  OutputStreamWriter fw = FileUtil.getFileWriter(
69  Format fm = Format.getPrettyFormat();
70  fm.setEncoding(ElephantContext.getEncoding());
71  XMLOutputter xo = new XMLOutputter(fm);
72  xo.output(doc, fw);
73  fw.close();
74  } catch (IOException ex) {
75  Logger.getLogger(XMLHumanResourceSet.class.getName()).log(Level.SEVERE, null, ex);
76  }
77  }
78 
79  private void loadCollection() {
80  SAXBuilder builder = new SAXBuilder();
81  Document doc;
82  try {
83  File confFile = new File(ElephantContext.getRealPath(XML_HUMANRESOURCE));
84  if(confFile.exists()) {
85  doc = builder.build(confFile);
86  readXML(doc.getRootElement());
87  }
88  } catch (IOException ex) {
89  Logger.getLogger(XMLHumanResourceSet.class.getName()).log(Level.SEVERE, null, ex);
90  } catch (JDOMException ex) {
91  Logger.getLogger(XMLHumanResourceSet.class.getName()).log(Level.SEVERE, null, ex);
92  }
93  }
94 
95  private void writeXML(Element rootElement) {
96  for(XMLHumanResource hr : this) {
97  Element el = new Element("human-resource");
98  el.setAttribute("name", hr.getName());
99  el.setAttribute("id", hr.getId() + "");
100  rootElement.addContent(el);
101  }
102  }
103 
104  private void readXML(Element rootElement) {
105  clear();
106  for(Element el : (List<Element>) rootElement.getChildren("human-resource")) {
107  XMLHumanResource hr = new XMLHumanResource();
108  hr.setName(el.getAttributeValue("name"));
109  hr.setId((Long) ObjectString.parseString(el.getAttributeValue("id"), Long.class, false));
110  add(hr);
111  }
112  }
113 
114 }
static OutputStreamWriter getFileWriter(String folder, String file)
Definition: FileUtil.java:158
XMLHumanResourceSet(IConstructor constructor)