Skip to main content
summaryrefslogtreecommitdiffstats
blob: 68f1a32f28a71d7e372717641fa2a40deef94cd6 (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
/*******************************************************************************
 * 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.ui.skynet.render;

import java.util.Date;
import java.util.HashSet;
import java.util.Random;
import java.util.Set;
import org.eclipse.core.resources.IFolder;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.osee.framework.core.data.IOseeBranch;
import org.eclipse.osee.framework.core.enums.CoreAttributeTypes;
import org.eclipse.osee.framework.core.exception.OseeArgumentException;
import org.eclipse.osee.framework.core.exception.OseeCoreException;
import org.eclipse.osee.framework.core.exception.OseeExceptions;
import org.eclipse.osee.framework.jdk.core.type.Pair;
import org.eclipse.osee.framework.jdk.core.util.GUID;
import org.eclipse.osee.framework.jdk.core.util.Strings;
import org.eclipse.osee.framework.plugin.core.util.OseeData;
import org.eclipse.osee.framework.skynet.core.UserManager;
import org.eclipse.osee.framework.skynet.core.artifact.Artifact;
import org.eclipse.osee.framework.skynet.core.artifact.Attribute;
import org.eclipse.osee.framework.skynet.core.artifact.BranchUtility;
import org.eclipse.osee.framework.skynet.core.change.ArtifactDelta;
import org.eclipse.osee.framework.skynet.core.word.WordUtil;
import org.eclipse.osee.framework.ui.skynet.preferences.MsWordPreferencePage;

public final class RenderingUtil {
   private static final Random generator = new Random();

   private static IFolder workingFolder;
   private static IFolder diffFolder;
   private static IFolder previewFolder;
   private static IFolder mergeEditFolder;
   private static boolean arePopupsAllowed = true;

   public static void setPopupsAllowed(boolean popupsAllowed) {
      arePopupsAllowed = popupsAllowed;
   }

   public static boolean arePopupsAllowed() {
      return arePopupsAllowed;
   }

   public static Pair<Artifact, Artifact> asRenderInput(ArtifactDelta artifactDelta) {
      Artifact artFile1;
      Artifact artFile2;

      //    if (delta.getTxDelta() == null
      //          || !delta.getTxDelta().areOnTheSameBranch()) {
      //       // Assumptions - when comparing data between transactions on
      //       // different branches, the start artifact will never be null;
      //       if (delta.getStartArtifact().getModType().isDeleted()) {
      //          artFile1 = delta.getStartArtifact();
      //          artFile2 = null;
      //       } else if (delta.getEndArtifact() == null) {
      //          artFile1 = null;
      //          artFile2 = delta.getStartArtifact();
      //       } else { // case where there are new, modified, or deleted attributes (are artifact is not new or deleted)
      //          // also could be introduced in both branches
      //          artFile1 = delta.getEndArtifact();
      //          artFile2 = delta.getStartArtifact();
      //       }
      //    } else {
      // Assumptions - when comparing data between transactions on the
      // same branch, the end artifact will never be null;
      if (artifactDelta.getEndArtifact().getModType().isDeleted()) {
         artFile1 = artifactDelta.getStartArtifact();
         artFile2 = null;
      } else if (artifactDelta.getStartArtifact() == null) {
         artFile1 = null;
         artFile2 = artifactDelta.getEndArtifact();
      } else {
         artFile1 = artifactDelta.getStartArtifact();
         artFile2 = artifactDelta.getEndArtifact();
      }
      //    }
      return new Pair<Artifact, Artifact>(artFile1, artFile2);
   }

   public static String getFilenameFromArtifact(FileSystemRenderer renderer, Artifact artifact, PresentationType presentationType) throws OseeCoreException {
      String fileName = renderer.getStringOption(IRenderer.FILE_NAME_OPTION);
      if (Strings.isValid(fileName)) {
         return fileName;
      }

      StringBuilder name = new StringBuilder(100);

      if (artifact != null) {
         name.append(artifact.getSafeName());
         name.append("(");
         name.append(artifact.getGuid());
         name.append(")");

         if (artifact.isHistorical() || presentationType == PresentationType.DIFF) {
            name.append("(");
            name.append(artifact.getTransactionNumber());
            name.append(")");
         }

         name.append(" ");
         name.append(new Date().toString().replaceAll(":", ";"));
         name.append("-");
         name.append(generator.nextInt(99) + 1);
         name.append(".");
         name.append(renderer.getAssociatedExtension(artifact));
      } else {
         name.append(GUID.create());
         name.append(".xml");
      }
      return name.toString();
   }

   public static IFolder getRenderFolder(IOseeBranch branch, PresentationType presentationType) throws OseeCoreException {
      try {
         IFolder baseFolder = ensureRenderFolderExists(presentationType);
         IFolder renderFolder = baseFolder.getFolder(BranchUtility.toFileName(branch));
         if (!renderFolder.exists()) {
            renderFolder.create(true, true, null);
         }
         return renderFolder;
      } catch (CoreException ex) {
         OseeExceptions.wrapAndThrow(ex);
         return null; // unreachable since wrapAndThrow() always throws an exception
      }
   }

   public static IFolder ensureRenderFolderExists(PresentationType presentationType) throws OseeCoreException {
      IFolder toReturn = null;
      switch (presentationType) {
         case DIFF:
            diffFolder = getOrCreateFolder(diffFolder, ".diff");
            toReturn = diffFolder;
            break;
         case SPECIALIZED_EDIT:
            workingFolder = getOrCreateFolder(workingFolder, ".working");
            toReturn = workingFolder;
            break;
         case PREVIEW:
            previewFolder = getOrCreateFolder(previewFolder, ".preview");
            toReturn = previewFolder;
            break;
         case MERGE_EDIT:
            mergeEditFolder = getOrCreateFolder(mergeEditFolder, ".mergeEdit");
            toReturn = mergeEditFolder;
            break;
         default:
            throw new OseeArgumentException("Unexpected presentation type: %s", presentationType);
      }
      return toReturn;
   }

   private static IFolder getOrCreateFolder(IFolder folder, String name) throws OseeCoreException {
      IFolder toCheck = folder;
      if (toCheck == null || !toCheck.exists()) {
         toCheck = OseeData.getFolder(name);
      }
      return toCheck;
   }

   public static Set<Artifact> checkForTrackedChangesOn(Artifact artifact) throws OseeCoreException {
      Set<Artifact> artifacts = new HashSet<Artifact>();

      if (!UserManager.getBooleanSetting(MsWordPreferencePage.REMOVE_TRACKED_CHANGES)) {
         if (artifact != null) {
            Attribute<?> attribute = artifact.getSoleAttribute(CoreAttributeTypes.WordTemplateContent);
            if (attribute != null) {
               String value = attribute.getValue().toString();
               // check for track changes
               if (WordUtil.containsWordAnnotations(value)) {
                  // capture those artifacts that have tracked changes on
                  artifacts.add(artifact);
               }
            }
         }
      }
      return artifacts;
   }
}

Back to the top