Skip to main content
summaryrefslogtreecommitdiffstats
blob: 0dd30f37b8fb16dcff14653cd49d1b676609fb98 (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
/*******************************************************************************
 * 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.ats.artifact;

import static org.eclipse.osee.framework.jdk.core.util.Strings.intern;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import java.util.logging.Level;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.eclipse.osee.ats.artifact.ATSLog.LogType;
import org.eclipse.osee.ats.internal.AtsPlugin;
import org.eclipse.osee.framework.core.data.SystemUser;
import org.eclipse.osee.framework.core.exception.OseeCoreException;
import org.eclipse.osee.framework.core.exception.UserNotInDatabase;
import org.eclipse.osee.framework.jdk.core.util.AXml;
import org.eclipse.osee.framework.jdk.core.util.DateUtil;
import org.eclipse.osee.framework.jdk.core.util.Strings;
import org.eclipse.osee.framework.logging.OseeLog;
import org.eclipse.osee.framework.skynet.core.User;
import org.eclipse.osee.framework.skynet.core.UserManager;

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

   private Date date;
   private String msg;
   private String state;
   private User user;
   private LogType type = LogType.None;
   private final String userId;
   private final static Pattern LOG_ITEM_PATTERN =
      Pattern.compile("<Item date=\"(.*?)\" msg=\"(.*?)\" state=\"(.*?)\" type=\"(.*?)\" userId=\"(.*?)\"/>");
   private final static Pattern LOG_ITEM_TAG_PATTERN = Pattern.compile("<Item ");

   public static List<LogItem> getLogItems(String xml, String id) throws OseeCoreException {
      List<LogItem> logItems = new ArrayList<LogItem>();
      if (!xml.isEmpty()) {
         Matcher m = LOG_ITEM_PATTERN.matcher(xml);
         while (m.find()) {
            LogItem item =
               new LogItem(m.group(4), m.group(1), Strings.intern(m.group(5)), Strings.intern(m.group(3)), // NOPMD by b0727536 on 9/29/10 8:52 AM
                  AXml.xmlToText(m.group(2)), id);
            logItems.add(item);
         }

         Matcher m2 = LOG_ITEM_TAG_PATTERN.matcher(xml);
         int openTagsFound = 0;
         while (m2.find()) {
            openTagsFound++;
         }
         if (logItems.size() != openTagsFound) {
            OseeLog.log(AtsPlugin.class, Level.SEVERE, String.format(
               "ATS Log: open tags found %d doesn't match log items parsed %d for %s", openTagsFound, logItems.size(),
               id));
         }
      }
      return logItems;
   }

   public LogItem(LogType type, Date date, User user, String state, String msg, String hrid) throws OseeCoreException {
      this(type.name(), String.valueOf(date.getTime()), user.getUserId(), state, msg, hrid);
   }

   public LogItem(LogType type, String date, String userId, String state, String msg, String hrid) throws OseeCoreException {
      Long dateLong = Long.valueOf(date);
      this.date = new Date(dateLong.longValue());
      this.msg = msg;
      this.state = intern(state);
      this.userId = intern(userId);
      try {
         this.user = UserManager.getUserByUserId(userId);
      } catch (UserNotInDatabase ex) {
         this.user = UserManager.getUser(SystemUser.Guest);
         OseeLog.log(AtsPlugin.class, Level.SEVERE,
            String.format("Error parsing ATS Log for %s - %s", hrid, ex.getLocalizedMessage()), ex);
      }
      this.type = type;
   }

   public LogItem(String type, String date, String userId, String state, String msg, String hrid) throws OseeCoreException {
      this(LogType.getType(type), date, userId, state, msg, hrid);
   }

   public String toXml() throws OseeCoreException {
      return "<type>" + type.name() + "</type><date>" + date.getTime() + "</date><user>" + user.getUserId() + "</user><state>" + (state == null ? "" : state) + "</state><msg>" + (msg == null ? "" : msg) + "</msg>";
   }

   public Date getDate() {
      return date;
   }

   public String getDate(String pattern) {
      if (pattern != null) {
         return new SimpleDateFormat(pattern, Locale.US).format(date);
      }
      return date.toString();
   }

   public void setDate(Date date) {
      this.date = date;
   }

   public String getUserId() {
      return userId;
   }

   public String getMsg() {
      return msg;
   }

   public void setMsg(String msg) {
      this.msg = msg;
   }

   @Override
   public String toString() {
      return String.format("%s (%s)%s by %s on %s", getToStringMsg(), type, getToStringState(), getToStringUser(),
         DateUtil.getMMDDYYHHMM(date));
   }

   private String getToStringUser() {
      return user == null ? "unknown" : user.getName();
   }

   private String getToStringState() {
      return state.isEmpty() ? "" : "from " + state;
   }

   private String getToStringMsg() {
      return msg.isEmpty() ? "" : msg;
   }

   public User getUser() {
      return user;
   }

   public LogType getType() {
      return type;
   }

   public void setType(LogType type) {
      this.type = type;
   }

   public String toHTML(String labelFont) {
      return "NOTE (" + type + "): " + msg + " (" + user.getName() + ")";
   }

   public void setUser(User user) {
      this.user = user;
   }

   public String getState() {
      return state;
   }

   public void setState(String state) {
      this.state = state;
   }
}

Back to the top