BrightSide Workbench Full Report + Source Code
XMLTimeControlSet.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.Date;
25 import java.util.List;
26 import java.util.TreeSet;
27 import java.util.logging.Level;
28 import java.util.logging.Logger;
29 import org.turro.string.ObjectString;
30 import org.turro.string.Strings;
31 import org.jdom.Document;
32 import org.jdom.Element;
33 import org.jdom.JDOMException;
34 import org.jdom.input.SAXBuilder;
35 import org.jdom.output.Format;
36 import org.jdom.output.XMLOutputter;
37 import org.turro.elephant.context.ElephantContext;
38 import org.turro.elephant.context.IConstructor;
39 import org.turro.elephant.impl.util.FileUtil;
40 
45 public class XMLTimeControlSet extends TreeSet<XMLTimeControl> {
46 
47  public static final String XML_TIMECONTROL = "/WEB-INF/local/timeControl.xml";
48 
49  private IConstructor constructor;
50 
51  public XMLTimeControlSet(IConstructor constructor) {
52  this.constructor = constructor;
53  loadCollection();
54  }
55 
56  public void saveCollection() {
57  Document doc = new Document(new Element("time-controls"));
58  writeXML(doc.getRootElement());
59  try {
60  OutputStreamWriter fw = FileUtil.getFileWriter(
62  Format fm = Format.getPrettyFormat();
63  fm.setEncoding(ElephantContext.getEncoding());
64  XMLOutputter xo = new XMLOutputter(fm);
65  xo.output(doc, fw);
66  fw.close();
67  } catch (IOException ex) {
68  Logger.getLogger(XMLHumanResourceSet.class.getName()).log(Level.SEVERE, null, ex);
69  }
70  }
71 
72  public void createCopy() {
73  File confFile = new File(ElephantContext.getRealPath(XML_TIMECONTROL));
74  File copseg = new File(FileUtil.getFolderFile(confFile).getAbsolutePath() + "/copseg");
75  copseg.mkdirs();
76  try {
77  org.amic.util.file.FileUtil.copyFile(confFile, copseg.getAbsolutePath());
78  } catch (IOException ex) {
79  Logger.getLogger(XMLTimeControlSet.class.getName()).log(Level.SEVERE, null, ex);
80  }
81  }
82 
83  private void loadCollection() {
84  SAXBuilder builder = new SAXBuilder();
85  Document doc;
86  try {
87  File confFile = new File(ElephantContext.getRealPath(XML_TIMECONTROL));
88  if(confFile.exists()) {
89  doc = builder.build(confFile);
90  readXML(doc.getRootElement());
91  }
92  } catch (IOException ex) {
93  Logger.getLogger(XMLHumanResourceSet.class.getName()).log(Level.SEVERE, null, ex);
94  } catch (JDOMException ex) {
95  Logger.getLogger(XMLHumanResourceSet.class.getName()).log(Level.SEVERE, null, ex);
96  }
97  }
98 
99  private void writeXML(Element rootElement) {
100  for(XMLTimeControl tc : this) {
101  Element el = new Element("time-control");
102  el.setAttribute("human-resource-id", tc.getHumanResourceId() + "");
103  if(tc.getStartTime() != null) {
104  el.setAttribute("start-time",
105  ObjectString.formatObject(tc.getStartTime(), ObjectString.COMPRESSED_DATE_PATTERN, false));
106  if(tc.getEndTime() != null) {
107  el.setAttribute("end-time",
108  ObjectString.formatObject(tc.getEndTime(), ObjectString.COMPRESSED_DATE_PATTERN, false));
109  }
110  rootElement.addContent(el);
111  }
112  }
113  }
114 
115  private void readXML(Element rootElement) {
116  clear();
117  for(Element el : (List<Element>) rootElement.getChildren("time-control")) {
118  XMLTimeControl tc = new XMLTimeControl();
119  tc.setHumanResourceId((Long) ObjectString.parseString(el.getAttributeValue("human-resource-id"), Long.class, false));
120  if(!Strings.isBlank(el.getAttributeValue("start-time"))) {
121  tc.setStartTime((Date) ObjectString.parseString(el.getAttributeValue("start-time"),
122  ObjectString.COMPRESSED_DATE_PATTERN, Date.class, false));
123  }
124  if(!Strings.isBlank(el.getAttributeValue("end-time"))) {
125  tc.setEndTime((Date) ObjectString.parseString(el.getAttributeValue("end-time"),
126  ObjectString.COMPRESSED_DATE_PATTERN, Date.class, false));
127  }
128  if(tc.getStartTime() != null) {
129  add(tc);
130  }
131  }
132  }
133 
134 }
135 
static OutputStreamWriter getFileWriter(String folder, String file)
Definition: FileUtil.java:158
static File getFolderFile(File file)
Definition: FileUtil.java:290
XMLTimeControlSet(IConstructor constructor)