Skip to main content
summaryrefslogtreecommitdiffstats
blob: 1649058c1679184e3217f1ecb774e0d2cc52fd2f (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
/*******************************************************************************
 * Copyright (c) 2015 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.orcs.rest.internal.writer;

import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.eclipse.osee.framework.core.data.ArtifactToken;
import org.eclipse.osee.orcs.rest.model.writer.reader.OwApplicability;
import org.eclipse.osee.orcs.rest.model.writer.reader.OwArtifact;
import org.eclipse.osee.orcs.rest.model.writer.reader.OwArtifactToken;
import org.eclipse.osee.orcs.rest.model.writer.reader.OwArtifactType;
import org.eclipse.osee.orcs.rest.model.writer.reader.OwAttribute;
import org.eclipse.osee.orcs.rest.model.writer.reader.OwAttributeType;
import org.eclipse.osee.orcs.rest.model.writer.reader.OwBranch;
import org.eclipse.osee.orcs.rest.model.writer.reader.OwCollector;
import org.eclipse.osee.orcs.rest.model.writer.reader.OwRelation;
import org.eclipse.osee.orcs.rest.model.writer.reader.OwRelationType;

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

   private final Pattern relTypePattern = Pattern.compile("\\[(.*)\\]-\\[(.*)\\]-\\[(.*)\\]-\\[(.*)\\]");
   private final Pattern nameIdPattern = Pattern.compile("\\[(.*)\\]-\\[(.*)\\]");
   private final Map<Long, OwArtifactType> idToArtType = new HashMap<>();
   private final Map<Long, OwArtifactToken> idToArtToken = new HashMap<>();
   private final OwCollector collector;

   public OrcsWriterFactory(OwCollector collector) {
      this.collector = collector;
   }

   public OwArtifactToken getOrCreateToken(String value) {
      OwArtifactToken token = null;
      Matcher matcher = nameIdPattern.matcher(value);
      if (matcher.find()) {
         Long id = Long.valueOf(matcher.group(2));
         token = idToArtToken.get(id);
         if (token == null) {
            token = new OwArtifactToken(id, matcher.group(1));
            token.setData(value);
            collector.getArtTokens().add(token);
            idToArtToken.put(id, token);
         }
      }
      return token;
   }

   public OwArtifactType getOrCreateArtifactType(String value) {
      OwArtifactType artType = null;
      Matcher matcher = nameIdPattern.matcher(value);
      if (matcher.find()) {
         Long id = Long.valueOf(matcher.group(2));
         artType = idToArtType.get(id);
         if (artType == null) {
            artType = new OwArtifactType(id, matcher.group(1));
            artType.setData(value);
            collector.getArtTypes().add(artType);
            idToArtType.put(id, artType);
         }
      }
      return artType;
   }

   public OwAttribute getOrCreateAttribute(OwArtifact artifact, OwAttributeType attrType) {
      OwAttribute attr = null;
      for (OwAttribute fAttr : artifact.getAttributes()) {
         if (fAttr.getType().getId() > 0L && fAttr.getType().getId().equals(attrType.getId())) {
            attr = fAttr;
            break;
         } else if (fAttr.getType().getName().equals(attrType.getName())) {
            attr = fAttr;
            break;
         }
      }
      if (attr == null) {
         attr = new OwAttribute();
         attr.setType(attrType);
         artifact.getAttributes().add(attr);
      }
      return attr;
   }

   public OwApplicability getOrCreateApplicability(OwArtifact artifact) {
      OwApplicability app = artifact.getAppId();
      if (app == null) {
         app = new OwApplicability();
         artifact.setAppId(app);
      }
      return app;
   }

   public OwRelation createRelationType(OwRelationType relType, String value) {
      ArtifactToken token = getOrCreateToken(value);
      OwRelation relation = new OwRelation();
      relation.setData(value);
      relation.setArtToken(token);
      relation.setType(relType);
      return relation;
   }

   public void processRelationType(OwRelationType relType, String value) {
      relType.setData(value);
      Matcher matcher = relTypePattern.matcher(value);
      if (matcher.find()) {
         relType.setName(matcher.group(1));
         relType.setSideName(matcher.group(2));
         relType.setSideA(matcher.group(3).contains("Side A"));
         relType.setId(Long.valueOf(matcher.group(4)));
      }
   }

   public void processAttributeType(OwAttributeType attrType, String value) {
      attrType.setData(value);
      Matcher matcher = nameIdPattern.matcher(value);
      if (matcher.find()) {
         attrType.setName(matcher.group(1));
         attrType.setId(Long.valueOf(matcher.group(2)));
      }
      // otherwise, assume the value is the attribute name
      else {
         attrType.setName(value);
      }
   }

   public OwBranch getOrCreateBranchToken(String value) {
      OwBranch branch = new OwBranch(0L, "");
      Matcher matcher = nameIdPattern.matcher(value);
      if (matcher.find()) {
         branch.setName(matcher.group(1));
         branch.setId(Long.valueOf(matcher.group(2)));
      }
      return branch;
   }

}

Back to the top