Skip to main content
summaryrefslogtreecommitdiffstats
blob: fa9409209fcfdabbac292b07f2aaf81a913a4303 (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
/*******************************************************************************
 * 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.orcs.rest.internal;

import java.net.URI;
import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Map.Entry;
import javax.ws.rs.core.UriInfo;
import org.eclipse.osee.framework.core.data.BranchId;
import org.eclipse.osee.framework.core.data.IAttributeType;
import org.eclipse.osee.framework.core.data.IOseeBranch;
import org.eclipse.osee.framework.jdk.core.type.OseeCoreException;
import org.eclipse.osee.orcs.data.ArtifactReadable;
import org.eclipse.osee.orcs.data.AttributeReadable;
import org.eclipse.osee.orcs.data.BranchReadable;
import org.eclipse.osee.orcs.data.TransactionReadable;

/**
 * @author Roberto E. Escobar
 */
public class HtmlWriter {

   private final UriInfo uriInfo;

   public HtmlWriter(UriInfo uriInfo) {
      this.uriInfo = uriInfo;
   }

   public String toHtml(Iterable<? extends Object> objects) throws OseeCoreException {
      StringBuilder builder = new StringBuilder();
      builder.append("<html><body>");
      for (Object object : objects) {
         if (object instanceof BranchReadable) {
            BranchReadable branch = (BranchReadable) object;
            addTable(builder, toData(branch));
         } else if (object instanceof ArtifactReadable) {
            ArtifactReadable artifact = (ArtifactReadable) object;
            addTable(builder, toData(artifact));
         } else if (object instanceof AttributeReadable) {
            AttributeReadable<?> attribute = (AttributeReadable<?>) object;
            addTable(builder, toData(attribute));
         } else if (object instanceof TransactionReadable) {
            TransactionReadable tx = (TransactionReadable) object;
            addTable(builder, toData(tx));
         } else {
            Map<String, Object> unhandled = new LinkedHashMap<>();
            unhandled.put("Class", object.getClass().getSimpleName());
            unhandled.put("Data", object.toString());
            addTable(builder, unhandled);
         }
         builder.append("<br/>");
      }
      builder.append("</body></html>");
      return builder.toString();
   }

   private Map<String, Object> toData(AttributeReadable<?> attribute) throws OseeCoreException {
      Map<String, Object> data = new LinkedHashMap<>();
      data.put("Name", attribute.getAttributeType().getName());

      int attrId = attribute.getLocalId();
      URI uri = uriInfo.getAbsolutePathBuilder().path("{attributeId}").build(attrId);
      data.put("AttributeId", asLink(uri.getPath(), String.valueOf(attrId)));
      return data;
   }

   private Map<String, Object> toData(ArtifactReadable artifact) throws OseeCoreException {
      Map<String, Object> data = new LinkedHashMap<>();
      data.put("Name", artifact.getName());
      data.put("Artifact Id", artifact.getUuid());
      data.put("Tx Id", artifact.getTransaction());
      Long branchId = artifact.getBranchId();

      URI uri;
      if (isAtEndOfPath(uriInfo.getPath(), "artifact")) {
         uri = uriInfo.getAbsolutePathBuilder().path("../../{uuid}").build(branchId);
      } else {
         uri = uriInfo.getAbsolutePathBuilder().path("../../../{uuid}").build(branchId);
      }
      data.put("Branch", asLink(uri.toASCIIString(), "Branch " + branchId));

      Collection<? extends IAttributeType> types = artifact.getExistingAttributeTypes();
      for (IAttributeType type : types) {
         for (AttributeReadable<?> attr : artifact.getAttributes(type)) {
            URI attrUri = uriInfo.getAbsolutePathBuilder().path("/attribute/{attributeId}").build(attr.getLocalId());
            String label = asLink(attrUri.getPath(), type.getName());
            String value = attr.getDisplayableString();
            data.put(label, value == null ? "<NULL>" : value);
         }
      }

      int count = 0;
      for (ArtifactReadable art : artifact.getChildren()) {
         URI uri1;
         if (isAtEndOfPath(uriInfo.getPath(), "artifact")) {
            uri1 = uriInfo.getAbsolutePathBuilder().path("{uuid}").build(art.getUuid());
         } else {
            uri1 = uriInfo.getAbsolutePathBuilder().path("../{uuid}").build(art.getUuid());
         }
         String value = art.getName();
         data.put("Child " + ++count, asLink(uri1.getPath(), value == null ? "<NULL>" : value));
      }
      return data;
   }

   private Map<String, Object> toData(BranchReadable branch) throws OseeCoreException {
      Map<String, Object> data = new LinkedHashMap<>();
      data.put("Name", branch.getName());
      data.put("Uuid", branch.getUuid());
      data.put("Local Id", branch.getId());
      data.put("State", branch.getBranchState());
      data.put("Type", branch.getBranchType());
      data.put("Archived", branch.getArchiveState());
      if (branch.hasParentBranch()) {
         try {
            IOseeBranch parent = getBranchFromId(branch.getParentBranch());

            URI uri;
            if (isAtEndOfPath(uriInfo.getPath(), "branch")) {
               uri = uriInfo.getAbsolutePathBuilder().path("{uuid}").build(parent.getGuid());
            } else {
               uri = uriInfo.getAbsolutePathBuilder().path("../{uuid}").build(parent.getGuid());
            }
            data.put("Parent", asLink(uri.getPath(), parent.getName()));
         } catch (OseeCoreException ex) {
            data.put("Parent", "Root");
         }

         URI uri;
         if (isAtEndOfPath(uriInfo.getPath(), "branch")) {
            uri = uriInfo.getAbsolutePathBuilder().path("{uuid}/artifact").build(branch.getUuid());
         } else {
            uri = uriInfo.getAbsolutePathBuilder().path("../{uuid}/artifact").build(branch.getUuid());
         }
         data.put("Artifacts", asLink(uri.getPath(), "Hierarchy"));
      }
      return data;
   }

   private IOseeBranch getBranchFromId(BranchId branch) {
      return OrcsApplication.getOrcsApi().getQueryFactory().branchQuery().andIds(
         branch).getResultsAsId().getExactlyOne();
   }

   public Map<String, Object> toData(TransactionReadable txRecord) throws OseeCoreException {
      Map<String, Object> data = new LinkedHashMap<>();
      data.put("TxId", txRecord);
      data.put("TxType", txRecord.getTxType());
      data.put("Date", txRecord.getDate());
      data.put("Comment", txRecord.getComment());
      data.put("Author", txRecord.getAuthor());
      IOseeBranch branch = getBranchFromId(txRecord.getBranch());

      URI uri;
      if (isAtEndOfPath(uriInfo.getPath(), "branch")) {
         uri = uriInfo.getAbsolutePathBuilder().path("{uuid}").build(branch.getUuid());
      } else {
         uri = uriInfo.getAbsolutePathBuilder().path("../{uuid}").build(branch.getUuid());
      }
      data.put("Branch", asLink(uri.getPath(), branch.getName()));
      return data;
   }

   public String asLink(String url, String text) {
      return String.format("<a href=\"%s\">%s</a>", url, text);
   }

   public void addTable(StringBuilder builder, Map<String, Object> data) {
      builder.append("<table>");
      for (Entry<String, Object> entry : data.entrySet()) {
         builder.append("<tr><td><b>");
         builder.append(entry.getKey());
         builder.append(": <b/></td><td>");
         builder.append(entry.getValue().toString());
         builder.append("</td></tr>");
      }
      builder.append("</table>");
   }

   private boolean isAtEndOfPath(String path, String value) {
      String toCheck = path;
      if (toCheck.endsWith("/")) {
         toCheck = toCheck.substring(0, toCheck.length() - 1);
      }
      return toCheck.endsWith(value);
   }

}

Back to the top