001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.xtest.pes.dbfeeder;
043:
044: import org.netbeans.xtest.xmlserializer.*;
045: import java.sql.*;
046: import java.io.*;
047: import org.netbeans.xtest.util.NetUtils;
048: import java.text.SimpleDateFormat;
049:
050: import org.w3c.dom.Document;
051: import org.netbeans.xtest.util.SerializeDOM;
052:
053: public class UploadMetadata implements XMLSerializable {
054:
055: static ClassMappingRegistry classMappingRegistry = new ClassMappingRegistry(
056: UploadMetadata.class);
057: static {
058: try {
059: // load global registry
060: GlobalMappingRegistry.registerClassForElementName(
061: "PESdbUpload", UploadMetadata.class);
062: // register this class
063: //classMappingRegistry.registerSimpleField("team",ClassMappingRegistry.ATTRIBUTE,"team");
064: classMappingRegistry.registerSimpleField("mailContact",
065: ClassMappingRegistry.ATTRIBUTE, "mailContact");
066: classMappingRegistry.registerSimpleField("timestamp",
067: ClassMappingRegistry.ATTRIBUTE, "timestamp");
068: classMappingRegistry.registerContainerField("uploadedZips",
069: "uploadedZips", ClassMappingRegistry.SUBELEMENT);
070: classMappingRegistry.registerContainerSubtype(
071: "uploadedZips", String.class, "zipFile");
072: classMappingRegistry.registerContainerField("webs",
073: "PESStatus", ClassMappingRegistry.SUBELEMENT);
074: classMappingRegistry.registerContainerSubtype("webs",
075: WebStatus.class, "web");
076: } catch (MappingException me) {
077: me.printStackTrace();
078: classMappingRegistry = null;
079: }
080: }
081:
082: public ClassMappingRegistry registerXMLMapping() {
083: return classMappingRegistry;
084: }
085:
086: // empty constructor - required by XMLSerializer
087: public UploadMetadata() {
088: }
089:
090: public UploadMetadata(String mailContact) {
091: this .mailContact = mailContact;
092: this .timestamp = new Timestamp(System.currentTimeMillis());
093: }
094:
095: public static UploadMetadata loadUploadMetadata(File metadataFile)
096: throws XMLSerializeException {
097: if (!metadataFile.isFile()) {
098: throw new XMLSerializeException(
099: "Cannot load UploadMetadata from file "
100: + metadataFile.getPath()
101: + ", file does not exist");
102: }
103: try {
104: Document doc = SerializeDOM.parseFile(metadataFile);
105: XMLSerializable xmlObject = XMLSerializer
106: .getXMLSerializable(doc);
107: if (xmlObject instanceof UploadMetadata) {
108: UploadMetadata metadata = (UploadMetadata) xmlObject;
109: return metadata;
110: }
111: } catch (IOException ioe) {
112: throw new XMLSerializeException(
113: "IOException caught when loading UploadMetadata file:"
114: + metadataFile.getPath(), ioe);
115: }
116: // xmlobject is not of required type
117: throw new XMLSerializeException(
118: "Loaded xml document is not UploadMetadata");
119: }
120:
121: public void saveUploadMetadata(File metadataFile)
122: throws XMLSerializeException {
123: try {
124: Document doc = XMLSerializer.toDOMDocument(this );
125: SerializeDOM.serializeToFile(doc, metadataFile);
126: } catch (IOException ioe) {
127: throw new XMLSerializeException(
128: "IOException caught when saving UploadMetadata file:"
129: + metadataFile.getPath(), ioe);
130: } catch (javax.xml.parsers.ParserConfigurationException pce) {
131: throw new XMLSerializeException(
132: "ParserConfigurationException caught when saving UploadMetadata file:"
133: + metadataFile.getPath(), pce);
134: }
135: }
136:
137: private String mailContact;
138:
139: /*
140: private String team;
141: public String getTeam() {
142: return team;
143: }
144: */
145:
146: private Timestamp timestamp;
147:
148: private String[] uploadedZips;
149:
150: private WebStatus[] webs;
151:
152: /** Getter for property mailContact.
153: * @return Value of property mailContact.
154: *
155: */
156: public java.lang.String getMailContact() {
157: return mailContact;
158: }
159:
160: /** Getter for property timestamp.
161: * @return Value of property timestamp.
162: *
163: */
164: public java.sql.Timestamp getTimestamp() {
165: return timestamp;
166: }
167:
168: /** Setter for property timestamp.
169: * @param timestamp New value of property timestamp.
170: *
171: */
172: /*
173: public void setTimestamp(java.sql.Timestamp timestamp) {
174: this.timestamp = timestamp;
175: }
176: /*
177: /** Getter for property uploadedZips.
178: * @return Value of property uploadedZips.
179: *
180: */
181: public java.lang.String[] getUploadedZipFilenames() {
182: if (uploadedZips == null) {
183: uploadedZips = new String[0];
184: }
185: return this .uploadedZips;
186: }
187:
188: /** get uploaded zips as file objects
189: */
190: public File[] getUploadedZipFiles(File parentDir) {
191: File[] zips = new File[getUploadedZipFilenames().length];
192: for (int i = 0; i < uploadedZips.length; i++) {
193: zips[i] = new File(parentDir, uploadedZips[i].trim());
194: }
195: return zips;
196: }
197:
198: /** Setter for property uploadedZips.
199: * @param uploadedZips New value of property uploadedZips.
200: *
201: */
202: public void setUploadedZips(java.lang.String[] uploadedZips) {
203: this .uploadedZips = uploadedZips;
204: }
205:
206: public void setUploadedZips(File[] uploadedZips) {
207: String[] zipNames = new String[uploadedZips.length];
208: for (int i = 0; i < uploadedZips.length; i++) {
209: zipNames[i] = uploadedZips[i].getName();
210: }
211: this .uploadedZips = zipNames;
212: }
213:
214: /* removes uploaded zip from the array
215: *
216: */
217: public boolean removeUploadedZip(String uploadedZip) {
218: if (uploadedZips != null) {
219: for (int i = 0; i < uploadedZips.length; i++) {
220: if (uploadedZip.equals(uploadedZips[i])) {
221: uploadedZips[i] = null;
222: // the array should be shrinked as well
223: return true;
224: }
225: }
226: }
227: return false;
228: }
229:
230: public boolean removeUploadedZip(File uploadedZip) {
231: return removeUploadedZip(uploadedZip.getName());
232: }
233:
234: public String getMetadataFilename() {
235: String hostname = NetUtils.getLocalHostName();
236: SimpleDateFormat formatter = new SimpleDateFormat(
237: "yyMMdd-HHmmss");
238: String timestamp = formatter.format(this .timestamp);
239: return "pes-" + NetUtils.getLocalHostName() + "-" + timestamp
240: + ".xml";
241: }
242:
243: /** Getter for property web.
244: * @return Value of property web.
245: *
246: */
247: public WebStatus[] getWebs() {
248: return this .webs;
249: }
250:
251: /** Setter for property web.
252: * @param web New value of property web.
253: *
254: */
255: public void setWebs(WebStatus[] webs) {
256: this.webs = webs;
257: }
258: }
|