Skip to main content
summaryrefslogtreecommitdiffstats
blob: 18e94c47a676f9fdf319ee3ef664467b4a4a5d05 (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
/*******************************************************************************
 * 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.skynet.core.importing.resolvers;

import java.io.BufferedInputStream;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URI;
import java.util.Collection;
import java.util.Map.Entry;
import org.eclipse.osee.framework.core.enums.CoreArtifactTypes;
import org.eclipse.osee.framework.core.exception.OseeCoreException;
import org.eclipse.osee.framework.core.exception.OseeExceptions;
import org.eclipse.osee.framework.core.model.Branch;
import org.eclipse.osee.framework.core.model.type.ArtifactType;
import org.eclipse.osee.framework.skynet.core.artifact.Artifact;
import org.eclipse.osee.framework.skynet.core.artifact.ArtifactProcessor;
import org.eclipse.osee.framework.skynet.core.artifact.ArtifactTypeManager;
import org.eclipse.osee.framework.skynet.core.importing.RoughArtifact;
import org.eclipse.osee.framework.skynet.core.importing.RoughArtifactKind;
import org.eclipse.osee.framework.skynet.core.importing.RoughAttributeSet;

/**
 * @author Ryan D. Brooks
 */
public class NewArtifactImportResolver implements IArtifactImportResolver {
   private final ArtifactType primaryArtifactType;
   private final ArtifactType secondaryArtifactType;

   public NewArtifactImportResolver(ArtifactType primaryArtifactType, ArtifactType secondaryArtifactType) {
      this.primaryArtifactType = primaryArtifactType;
      this.secondaryArtifactType = secondaryArtifactType;
   }

   public Artifact resolve(final RoughArtifact roughArtifact, final Branch branch, Artifact realParent, Artifact root) throws OseeCoreException {
      ArtifactType artifactType = getArtifactType(roughArtifact.getRoughArtifactKind());

      Artifact realArtifact =
            ArtifactTypeManager.getFactory(artifactType).makeNewArtifact(branch, artifactType, roughArtifact.getGuid(),
                  roughArtifact.getHumanReadableId(), new ArtifactProcessor() {
                     @Override
                     public void run(Artifact artifact) throws OseeCoreException {
                        translateAttributes(roughArtifact, artifact);
                     }
                  });

      return realArtifact;
   }

   private ArtifactType getArtifactType(RoughArtifactKind kind) throws OseeCoreException {
      if (kind == RoughArtifactKind.PRIMARY) {
         return primaryArtifactType;
      } else if (kind == RoughArtifactKind.SECONDARY) {
         return secondaryArtifactType;
      } else if (kind == RoughArtifactKind.CONTAINER) {
         return ArtifactTypeManager.getType(CoreArtifactTypes.Folder);
      } else {
         throw new OseeCoreException("Unknown Artifact Kind " + kind);
      }
   }

   protected void translateAttributes(RoughArtifact roughArtifact, Artifact artifact) throws OseeCoreException {
      RoughAttributeSet roughAttributes = roughArtifact.getAttributes();
      for (String attrType : roughAttributes.getKeys()) {
         Collection<String> values = roughAttributes.getAttributeValueList(attrType);
         artifact.setAttributeValues(attrType, values);
      }
      transferBinaryAttributes(roughArtifact, artifact);
   }

   private void transferBinaryAttributes(RoughArtifact roughArtifact, Artifact artifact) throws OseeCoreException {
      for (Entry<String, URI> entry : roughArtifact.getURIAttributes().entrySet()) {
         try {
            artifact.setSoleAttributeFromStream(entry.getKey(), new BufferedInputStream(
                  entry.getValue().toURL().openStream()));
         } catch (MalformedURLException ex) {
            OseeExceptions.wrapAndThrow(ex);
         } catch (IOException ex) {
            OseeExceptions.wrapAndThrow(ex);
         }
      }
   }
}

Back to the top