Skip to main content
summaryrefslogtreecommitdiffstats
blob: b66bb2ca9cc53f7384b75471581e1dbf9337c022 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
/*******************************************************************************
 * Copyright (c) 2004, 2007 Boeing.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     Boeing - initial API and implementation
 *******************************************************************************/
package org.eclipse.osee.framework.branch.management.exchange.handler;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.List;
import java.util.Map;
import org.eclipse.osee.framework.branch.management.exchange.ExportImportXml;
import org.eclipse.osee.framework.jdk.core.util.Strings;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;

/**
 * @author Roberto E. Escobar
 */
public class ManifestSaxHandler extends BaseExportImportSaxHandler {

   private final List<IExportItem> filesToImport;
   private String metadataFile;
   private IExportItem branchFile;
   private String sourceDatabaseId;
   private Date sourceExportDate;
   private String exportVersion;

   public ManifestSaxHandler() {
      super();
      this.filesToImport = new ArrayList<IExportItem>();
      this.metadataFile = null;
      this.branchFile = null;
      this.sourceExportDate = null;
      this.sourceDatabaseId = "UNKNOWN";
   }

   @Override
   public void startElementFound(String uri, String localName, String name, Attributes attributes) throws SAXException {
      if (localName.equalsIgnoreCase(ExportImportXml.EXPORT_ENTRY)) {
         sourceDatabaseId = attributes.getValue(ExportImportXml.DATABASE_ID);
         sourceExportDate = new Date(Long.parseLong(attributes.getValue(ExportImportXml.EXPORT_DATE)));
         exportVersion = attributes.getValue(ExportImportXml.EXPORT_VERSION);
      }
      super.startElementFound(uri, localName, name, attributes);
   }

   @Override
   protected void processData(Map<String, String> fieldMap) {
      String fileName = fieldMap.get(ExportImportXml.ID);
      Integer priority = new Integer(fieldMap.get(ExportImportXml.PRIORITY));
      String source = fieldMap.get(ExportImportXml.SOURCE);

      if (Strings.isValid(fileName) && Strings.isValid(source)) {
         if (source.equals(ExportImportXml.DB_SCHEMA)) {
            this.metadataFile = fileName;
         } else {
            ImportFile importFile = new ImportFile(fileName, source, priority);
            if (source.equals("osee_branch")) {
               branchFile = importFile;
            } else {
               filesToImport.add(importFile);
            }
         }
      }
   }

   public IExportItem getBranchFile() {
      return branchFile;
   }

   public String getMetadataFile() {
      return Strings.isValid(metadataFile) ? metadataFile : "";
   }

   public String getSourceDatabaseId() {
      return sourceDatabaseId;
   }

   public Date getSourceExportDate() {
      return sourceExportDate;
   }

   public String getSourceExportVersion() {
      return exportVersion;
   }

   public List<IExportItem> getImportFiles() {
      Collections.sort(filesToImport, new Comparator<IExportItem>() {
         @Override
         public int compare(IExportItem item1, IExportItem item2) {
            return item1.getPriority() - item2.getPriority();
         }

      });
      return filesToImport;
   }

   public class ImportFile implements IExportItem {
      private final String fileName;
      private final String source;
      private final int priority;

      public ImportFile(String fileName, String source, int priority) {
         this.fileName = fileName;
         this.source = source;
         this.priority = priority;
      }

      @Override
      public String getFileName() {
         return fileName;
      }

      @Override
      public int getPriority() {
         return priority;
      }

      @Override
      public String getSource() {
         return source;
      }

      @Override
      public boolean equals(Object obj) {
         if (obj == this) {
            return true;
         }
         if (!(obj instanceof IExportItem)) {
            return false;
         }
         IExportItem other = (IExportItem) obj;
         return getSource().equals(other.getSource());
      }

      @Override
      public int hashCode() {
         return 37 * priority;
      }

      @Override
      public String toString() {
         return String.format("ImportFile [fileName=%s, source=%s, priority=%d]", fileName, source, priority);
      }
   }
}

Back to the top