Skip to main content
summaryrefslogtreecommitdiffstats
blob: d3cde70c5752543cdd65d8cca52032617d7551a5 (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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
/*******************************************************************************
 * 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.ats.util.Import;

import java.io.FileFilter;
import java.io.InputStreamReader;
import java.net.URI;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;
import java.util.logging.Level;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.osee.ats.api.data.AtsAttributeTypes;
import org.eclipse.osee.ats.api.notify.AtsNotifyType;
import org.eclipse.osee.ats.api.user.IAtsUser;
import org.eclipse.osee.ats.api.util.IAtsChangeSet;
import org.eclipse.osee.ats.core.client.notify.AtsNotificationManager;
import org.eclipse.osee.ats.core.client.task.AbstractTaskableArtifact;
import org.eclipse.osee.ats.core.client.task.TaskArtifact;
import org.eclipse.osee.ats.core.client.task.TaskManager;
import org.eclipse.osee.ats.core.client.team.TeamWorkFlowArtifact;
import org.eclipse.osee.ats.core.client.util.AtsUtilCore;
import org.eclipse.osee.ats.core.client.workflow.AbstractWorkflowArtifact;
import org.eclipse.osee.ats.internal.Activator;
import org.eclipse.osee.ats.internal.AtsClientService;
import org.eclipse.osee.framework.core.exception.OseeExceptions;
import org.eclipse.osee.framework.core.util.Result;
import org.eclipse.osee.framework.jdk.core.type.OseeArgumentException;
import org.eclipse.osee.framework.jdk.core.type.OseeCoreException;
import org.eclipse.osee.framework.jdk.core.util.Strings;
import org.eclipse.osee.framework.jdk.core.util.io.xml.ExcelSaxHandler;
import org.eclipse.osee.framework.jdk.core.util.io.xml.RowProcessor;
import org.eclipse.osee.framework.logging.OseeLog;
import org.eclipse.osee.framework.ui.plugin.util.AWorkbench;
import org.xml.sax.InputSource;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.XMLReaderFactory;

/**
 * @author Donald G. Dunne
 */
public class ExcelAtsTaskArtifactExtractor {

   private final AbstractWorkflowArtifact sma;
   private final boolean emailPOCs;
   private final IAtsChangeSet changes;

   private IProgressMonitor monitor;

   public ExcelAtsTaskArtifactExtractor(TeamWorkFlowArtifact artifact, boolean emailPOCs, IAtsChangeSet changes) {
      this.emailPOCs = emailPOCs;
      this.changes = changes;
      this.sma = artifact;
   }

   public void process(URI source) throws OseeCoreException {
      try {
         XMLReader xmlReader = XMLReaderFactory.createXMLReader();
         IProgressMonitor monitor = getMonitor();
         if (monitor == null) {
            monitor = new NullProgressMonitor();
         }
         xmlReader.setContentHandler(new ExcelSaxHandler(new InternalRowProcessor(monitor, changes, sma, emailPOCs),
            true));
         xmlReader.parse(new InputSource(new InputStreamReader(source.toURL().openStream(), "UTF-8")));
      } catch (Exception ex) {
         OseeExceptions.wrapAndThrow(ex);
      }
   }

   public String getDescription() {
      return "Extract each row as a task";
   }

   public IProgressMonitor getMonitor() {
      return monitor;
   }

   public void setMonitor(IProgressMonitor monitor) {
      this.monitor = monitor;
   }

   public FileFilter getFileFilter() {
      return null;
   }

   public String getName() {
      return "Excel Ats Tasks";
   }

   private final static class InternalRowProcessor implements RowProcessor {
      private String[] headerRow;
      private int rowNum;
      private final IProgressMonitor monitor;
      private final AbstractWorkflowArtifact sma;
      private final IAtsChangeSet changes;
      private final boolean emailPOCs;
      private final Date createdDate;
      private final IAtsUser createdBy;

      protected InternalRowProcessor(IProgressMonitor monitor, IAtsChangeSet changes, AbstractWorkflowArtifact sma, boolean emailPOCs) throws OseeCoreException {
         this.monitor = monitor;
         this.changes = changes;
         this.emailPOCs = emailPOCs;
         this.sma = sma;
         createdDate = new Date();
         createdBy = AtsClientService.get().getUserAdmin().getCurrentUser();
      }

      @Override
      public void processEmptyRow() {
         // do nothing
      }

      @Override
      public void processCommentRow(String[] row) {
         // do nothing
      }

      @Override
      public void reachedEndOfWorksheet() {
         // do nothing
      }

      @Override
      public void detectedRowAndColumnCounts(int rowCount, int columnCount) {
         // do nothing
      }

      @Override
      public void foundStartOfWorksheet(String sheetName) {
         // do nothing
      }

      @Override
      public void processHeaderRow(String[] headerRow) {
         this.headerRow = headerRow.clone();
      }

      @Override
      public void processRow(String[] row) throws OseeCoreException {
         rowNum++;
         monitor.setTaskName("Processing Row " + rowNum);
         TaskArtifact taskArt =
            ((AbstractTaskableArtifact) sma).createNewTask("", createdDate, createdBy, null, changes);

         monitor.subTask("Validating...");
         boolean valid = validateRow(row);
         if (!valid) {
            return;
         }
         AtsUtilCore.setEmailEnabled(false);
         for (int i = 0; i < row.length; i++) {
            if (headerRow[i] == null) {
               OseeLog.log(Activator.class, Level.SEVERE, "Null header column => " + i);
            } else if (headerRow[i].equalsIgnoreCase("Originator")) {
               processOriginator(row, taskArt, i);
            } else if (headerRow[i].equalsIgnoreCase("Assignees")) {
               processAssignees(row, taskArt, i);
            } else if (headerRow[i].equalsIgnoreCase("Resolution")) {
               processResolution(row, taskArt, i);
            } else if (headerRow[i].equalsIgnoreCase("Description")) {
               processDescription(row, taskArt, i);
            } else if (headerRow[i].equalsIgnoreCase("Related to State")) {
               processRelatedToState(row, taskArt, i);
            } else if (headerRow[i].equalsIgnoreCase("Notes")) {
               processNotes(row, taskArt, i);
            } else if (headerRow[i].equalsIgnoreCase("Title")) {
               processTitle(row, taskArt, i);
            } else if (headerRow[i].equalsIgnoreCase("Percent Complete")) {
               processPercentComplete(row, i);
            } else if (headerRow[i].equalsIgnoreCase("Hours Spent")) {
               processHoursSpent(row, i);
            } else if (headerRow[i].equalsIgnoreCase("Estimated Hours")) {
               processEstimatedHours(row, taskArt, i);
            } else {
               OseeLog.log(Activator.class, Level.SEVERE, "Unhandled column => " + headerRow[i]);
            }
         }
         AtsUtilCore.setEmailEnabled(true);
         if (taskArt.isCompleted()) {
            Result result = TaskManager.transitionToCompleted(taskArt, 0.0, 0, changes);
            if (result.isFalse()) {
               AWorkbench.popup(result);
            }
         }
         // always persist
         changes.add(taskArt);
         if (emailPOCs && !taskArt.isCompleted() && !taskArt.isCancelled()) {
            AtsNotificationManager.notify(sma, AtsNotifyType.Assigned);
         }
      }

      private boolean validateRow(String[] row) {
         boolean fullRow = false;
         for (int i = 0; i < row.length; i++) {
            if (Strings.isValid(row[i])) {
               fullRow = true;
               break;
            }
         }
         if (!fullRow) {
            OseeLog.log(Activator.class, Level.SEVERE, "Empty Row Found => " + rowNum + " skipping...");
         }
         return fullRow;
      }

      private void processTitle(String[] row, TaskArtifact taskArt, int i) throws OseeCoreException {
         String str = row[i];
         if (Strings.isValid(str)) {
            monitor.subTask(String.format("Title \"%s\"", str));
            taskArt.setName(str);
         }
      }

      private void processNotes(String[] row, TaskArtifact taskArt, int i) throws OseeCoreException {
         String str = row[i];
         if (Strings.isValid(str)) {
            taskArt.setSoleAttributeValue(AtsAttributeTypes.SmaNote, str);
         }
      }

      private void processRelatedToState(String[] row, TaskArtifact taskArt, int i) throws OseeCoreException {
         String str = row[i];
         if (Strings.isValid(str)) {
            taskArt.setSoleAttributeValue(AtsAttributeTypes.RelatedToState, str);
         }
      }

      private void processDescription(String[] row, TaskArtifact taskArt, int i) throws OseeCoreException {
         String str = row[i];
         if (Strings.isValid(str)) {
            taskArt.setSoleAttributeValue(AtsAttributeTypes.Description, str);
         }
      }

      private void processResolution(String[] row, TaskArtifact taskArt, int i) throws OseeCoreException {
         String str = row[i];
         if (Strings.isValid(str)) {
            taskArt.setSoleAttributeValue(AtsAttributeTypes.Resolution, str);
         }
      }

      private void processEstimatedHours(String[] row, TaskArtifact taskArt, int i) throws OseeArgumentException, OseeCoreException {
         String str = row[i];
         double hours = 0;
         if (Strings.isValid(str)) {
            try {
               hours = new Double(str);
            } catch (Exception ex) {
               throw new OseeArgumentException("Invalid Estimated Hours \"%s\" for row %d", str, rowNum);
            }
            taskArt.setSoleAttributeValue(AtsAttributeTypes.EstimatedHours, hours);
         }
      }

      private void processHoursSpent(String[] row, int i) throws OseeArgumentException, OseeCoreException {
         String str = row[i];
         double hours = 0;
         if (Strings.isValid(str)) {
            try {
               hours = new Double(str);
            } catch (Exception ex) {
               throw new OseeArgumentException("Invalid Hours Spent \"%s\" for row %d", str, rowNum);
            }
            sma.getStateMgr().updateMetrics(sma.getStateDefinition(), hours,
               sma.getStateMgr().getPercentComplete(sma.getCurrentStateName()), true);
         }
      }

      private void processPercentComplete(String[] row, int i) throws OseeArgumentException, OseeCoreException {
         String str = row[i];
         Double percent;
         if (Strings.isValid(str)) {
            try {
               percent = new Double(str);
               if (percent < 1) {
                  percent = percent * 100;
               }
            } catch (Exception ex) {
               throw new OseeArgumentException("Invalid Percent Complete \"%s\" for row %d", str, rowNum);
            }
            int percentInt = percent.intValue();
            sma.getStateMgr().updateMetrics(sma.getStateDefinition(), 0, percentInt, true);
         }
      }

      private void processAssignees(String[] row, TaskArtifact taskArt, int i) throws OseeCoreException {
         Set<IAtsUser> assignees = new HashSet<IAtsUser>();
         for (String userName : row[i].split(";")) {
            userName = userName.replaceAll("^\\s+", "");
            userName = userName.replaceAll("\\+$", "");
            IAtsUser user = null;
            if (!Strings.isValid(userName)) {
               user = AtsClientService.get().getUserAdmin().getCurrentUser();
            } else {
               try {
                  user = AtsClientService.get().getUserAdmin().getUserByName(userName);
               } catch (OseeCoreException ex) {
                  OseeLog.log(Activator.class, Level.SEVERE, ex);
               }
            }
            if (user == null) {
               OseeLog.logf(Activator.class, Level.SEVERE, "Invalid Assignee \"%s\" for row %d.  Using current user.",
                  userName, rowNum);
               user = AtsClientService.get().getUserAdmin().getCurrentUser();
            }
            assignees.add(user);
         }
         taskArt.getStateMgr().setAssignees(assignees);
      }

      private void processOriginator(String[] row, TaskArtifact taskArt, int i) throws OseeCoreException {
         String userName = row[i];
         IAtsUser user = null;
         if (!Strings.isValid(userName)) {
            user = AtsClientService.get().getUserAdmin().getCurrentUser();
         } else {
            user = AtsClientService.get().getUserAdmin().getUserByName(userName);
         }
         if (user == null) {
            OseeLog.logf(Activator.class, Level.SEVERE,
               "Invalid Originator \"%s\" for row %d\nSetting to current user.", userName, rowNum);
         }
         taskArt.internalSetCreatedBy(user, changes);
      }
   }
}

Back to the top