Skip to main content
summaryrefslogtreecommitdiffstats
blob: 21c3cd1c94a56f2ec194b2ffebf37d7796c8fc9e (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
/*******************************************************************************
 * 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.define.blam.operation;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;
import java.util.logging.Level;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.osee.define.DefinePlugin;
import org.eclipse.osee.framework.db.connection.exception.OseeCoreException;
import org.eclipse.osee.framework.db.connection.exception.OseeStateException;
import org.eclipse.osee.framework.jdk.core.util.time.GlobalTime;
import org.eclipse.osee.framework.logging.OseeLog;
import org.eclipse.osee.framework.skynet.core.artifact.Artifact;
import org.eclipse.osee.framework.skynet.core.artifact.ArtifactCache;
import org.eclipse.osee.framework.skynet.core.artifact.ArtifactLoad;
import org.eclipse.osee.framework.skynet.core.artifact.ArtifactLoader;
import org.eclipse.osee.framework.skynet.core.artifact.Branch;
import org.eclipse.osee.framework.skynet.core.artifact.BranchManager;
import org.eclipse.osee.framework.skynet.core.artifact.WordArtifact;
import org.eclipse.osee.framework.skynet.core.attribute.Attribute;
import org.eclipse.osee.framework.skynet.core.attribute.WordAttribute;
import org.eclipse.osee.framework.skynet.core.relation.CoreRelationEnumeration;
import org.eclipse.osee.framework.skynet.core.relation.RelationManager;
import org.eclipse.osee.framework.skynet.core.transaction.SkynetTransaction;
import org.eclipse.osee.framework.ui.skynet.blam.VariableMap;
import org.eclipse.osee.framework.ui.skynet.blam.operation.AbstractBlam;
import org.eclipse.osee.framework.ui.skynet.render.ITemplateRenderer;
import org.eclipse.osee.framework.ui.skynet.render.RendererManager;
import org.eclipse.osee.framework.ui.skynet.render.WordTemplateRenderer;
import org.eclipse.osee.framework.ui.skynet.util.OSEELog;

/**
 * @author Jeff C. Phillips
 * @author Theron Virgin
 */
public class PublishRequirements extends AbstractBlam {
   private boolean includeAttributes;
   private boolean publishAsDiff;
   private boolean removeTrackedChanges;
   private Date date;
   private Branch branch;

   /* (non-Javadoc)
    * @see org.eclipse.osee.framework.ui.skynet.blam.operation.BlamOperation#runOperation(org.eclipse.osee.framework.ui.skynet.blam.VariableMap, org.eclipse.osee.framework.skynet.core.artifact.Branch, org.eclipse.core.runtime.IProgressMonitor)
    */
   public void runOperation(VariableMap variableMap, IProgressMonitor monitor) throws Exception {
      Boolean updateParagraphNumber = variableMap.getBoolean("Update Paragraph Numbers");
      List<Artifact> artifacts = variableMap.getArtifacts("artifacts");
      includeAttributes = variableMap.getBoolean("Publish With Attributes");
      publishAsDiff = variableMap.getBoolean("Publish As Diff");
      removeTrackedChanges = variableMap.getBoolean("Skip Artifacts with Tracked Changes");
      if (variableMap.getValue("Diff Starting Point") instanceof Date) {
         date = (Date) variableMap.getValue("Diff Starting Point");
      }
      branch = variableMap.getBranch("Diff Branch");

      RelationManager.getRelatedArtifacts(artifacts, 999, true, CoreRelationEnumeration.DEFAULT_HIERARCHICAL__CHILD);

      SkynetTransaction transaction = new SkynetTransaction(artifacts.get(0).getBranch());
      String templateOption =
            publishAsDiff ? (includeAttributes ? ITemplateRenderer.DIFF_VALUE : ITemplateRenderer.DIFF_NO_ATTRIBUTES_VALUE) : (includeAttributes ? ITemplateRenderer.PREVIEW_WITH_RECURSE_VALUE : ITemplateRenderer.PREVIEW_WITH_RECURSE_NO_ATTRIBUTES_VALUE);
      VariableMap options =
            new VariableMap(WordTemplateRenderer.UPDATE_PARAGRAPH_NUMBER_OPTION, updateParagraphNumber,
                  ITemplateRenderer.TEMPLATE_OPTION, templateOption, ITemplateRenderer.TRANSACTION_OPTION, transaction);
      for (Artifact artifact : artifacts) {
         try {
            publish(monitor, artifact, options);
         } catch (OseeStateException ex) {
            OseeLog.log(DefinePlugin.class, Level.SEVERE, ex);
         }
      }
      transaction.execute();
   }

   private void publish(IProgressMonitor monitor, Artifact artifact, VariableMap options) throws OseeCoreException {
      if (monitor.isCanceled()) {
         return;
      }

      ArrayList<Artifact> nonFolderChildren = new ArrayList<Artifact>();
      if (artifact.isOfType("Folder")) {
         for (Artifact child : artifact.getChildren(true)) {
            if (child.isOfType("Folder")) {
               publish(monitor, child, options);
            } else {
               nonFolderChildren.add(child);
            }
         }
      } else {
         nonFolderChildren.add(artifact);
      }

      if (publishAsDiff) {
         if (branch == null || date == null) {
            throw new OseeCoreException(
                  "Must Select a " + branch == null ? "Branch" : "Date" + " to diff against when publishing as Diff");
         }
         nonFolderChildren = buildRecursiveList(nonFolderChildren);
         int transactionId = BranchManager.getBranchTransaction(date, branch.getBranchId());
         ArrayList<Artifact> olderArtifacts = getOlderArtifacts(nonFolderChildren, transactionId, branch.getBranchId());
         int index = 0;
         for (Artifact art : olderArtifacts) {
            if (art != null && art.isDeleted()) {
               olderArtifacts.set(index, null);
            }
            if (art != null && removeTrackedChanges && containsTrackedChanges(art)) {
               appendResultLine(String.format("Skiped %s because it Contains Tracked Changes\n",
                     art.getDescriptiveName()));
               olderArtifacts.set(index, null);
            }
            index++;
         }
         index = 0;
         for (Artifact art : nonFolderChildren) {
            if (art != null && art.isDeleted()) {
               nonFolderChildren.set(index, null);
            }
            if (art != null && removeTrackedChanges && containsTrackedChanges(art)) {
               appendResultLine(String.format("Skiped %s because it contains Tracked Changes\n",
                     art.getDescriptiveName()));
               nonFolderChildren.set(index, null);
            }
            index++;
         }
         RendererManager.diffInJob(olderArtifacts, nonFolderChildren, options);
      } else {
         RendererManager.preview(nonFolderChildren, monitor, options);
      }
   }

   /* (non-Javadoc)
    * @see org.eclipse.osee.framework.ui.skynet.blam.operation.BlamOperation#getDescriptionUsage()
    */
   public String getDescriptionUsage() {
      return "Drag in parent artifacts below and click the play button at the top right.";
   }

   /* (non-Javadoc)
    * @see org.eclipse.osee.framework.ui.skynet.blam.operation.BlamOperation#getXWidgetXml()
    */
   public String getXWidgetsXml() {
      return "<xWidgets><XWidget xwidgetType=\"XCheckBox\" horizontalLabel=\"true\" labelAfter=\"true\" displayName=\"Update Paragraph Numbers\" /><XWidget xwidgetType=\"XCheckBox\" horizontalLabel=\"true\" labelAfter=\"true\" displayName=\"Publish With Attributes\" /><XWidget xwidgetType=\"XCheckBox\" horizontalLabel=\"true\" labelAfter=\"true\" displayName=\"Publish As Diff\" /><XWidget xwidgetType=\"XLabel\" displayName=\" \" /><XWidget xwidgetType=\"XLabel\" displayName=\"Diff Options:\" /><XWidget xwidgetType=\"XCheckBox\" horizontalLabel=\"true\" labelAfter=\"true\" displayName=\"Skip Artifacts with Tracked Changes\" /><XWidget xwidgetType=\"XDate\" displayName=\"Diff Starting Point\" /><XWidget xwidgetType=\"XBranchSelectWidget\" displayName=\"Diff Branch\" defaultValue=\"" + BranchManager.getDefaultBranch().getBranchName() + "\" /><XWidget xwidgetType=\"XListDropViewer\" displayName=\"artifacts\" /></xWidgets>";
   }

   private ArrayList<Artifact> getOlderArtifacts(ArrayList<Artifact> artifacts, int transactionId, int branchId) throws OseeCoreException {
      ArrayList<Artifact> historicArtifacts = new ArrayList<Artifact>(artifacts.size());
      int queryId = ArtifactLoader.getNewQueryId();
      Timestamp insertTime = GlobalTime.GreenwichMeanTimestamp();

      Set<Artifact> artifactSet = new HashSet<Artifact>(artifacts);
      List<Object[]> insertParameters = new LinkedList<Object[]>();
      for (Artifact artifact : artifactSet) {
         insertParameters.add(new Object[] {queryId, insertTime, artifact.getArtId(), branchId, transactionId});
      }
      ArtifactLoader.loadArtifacts(queryId, ArtifactLoad.FULL, null, insertParameters, false, true, true);
      for (Artifact artifact : artifacts) {
         historicArtifacts.add(ArtifactCache.getHistorical(artifact.getArtId(), transactionId));
      }
      return historicArtifacts;
   }

   private ArrayList<Artifact> buildRecursiveList(ArrayList<Artifact> artifacts) throws OseeCoreException {
      ArrayList<Artifact> artifactWithChildren = new ArrayList<Artifact>(artifacts.size());
      for (Artifact artifact : artifacts) {
         artifactWithChildren.add(artifact);
         addChildren(artifactWithChildren, artifact);
      }
      return artifactWithChildren;
   }

   private void addChildren(ArrayList<Artifact> artifacts, Artifact artifact) throws OseeCoreException {
      for (Artifact loopArtifact : artifact.getChildren(true)) {
         artifacts.add(loopArtifact);
         addChildren(artifacts, loopArtifact);
      }
   }

   private boolean containsTrackedChanges(Artifact art) {
      if (art.isOfType(WordArtifact.ARTIFACT_NAME)) {
         boolean isWholeWord = art.isOfType(WordArtifact.WHOLE_WORD);
         try {
            Attribute<?> attribute =
                  art.getSoleAttribute(isWholeWord ? WordAttribute.WHOLE_WORD_CONTENT : WordAttribute.WORD_TEMPLATE_CONTENT);
            if (attribute != null) {
               return ((WordAttribute) attribute).mergeMarkupPresent();
            }
         } catch (OseeCoreException ex) {
            OSEELog.logException(PublishRequirements.class, ex, false);
         }
      }
      return false;
   }
}

Back to the top