Line data Source code
1 : // Copyright (C) 2008 The Android Open Source Project
2 : //
3 : // Licensed under the Apache License, Version 2.0 (the "License");
4 : // you may not use this file except in compliance with the License.
5 : // You may obtain a copy of the License at
6 : //
7 : // http://www.apache.org/licenses/LICENSE-2.0
8 : //
9 : // Unless required by applicable law or agreed to in writing, software
10 : // distributed under the License is distributed on an "AS IS" BASIS,
11 : // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 : // See the License for the specific language governing permissions and
13 : // limitations under the License.
14 :
15 : package com.google.gerrit.entities;
16 :
17 : import com.google.auto.value.AutoValue;
18 : import com.google.gerrit.common.Nullable;
19 : import java.time.Instant;
20 : import java.util.Objects;
21 :
22 : /**
23 : * A message attached to a {@link Change}. This message is persisted in data storage, that is why it
24 : * must have template form that does not contain Gerrit user identifiable information. Hence, it
25 : * requires processing to convert it to user-facing form.
26 : *
27 : * <p>These messages are normally auto-generated by gerrit operations, but might also incorporate
28 : * user input.
29 : */
30 : public final class ChangeMessage {
31 :
32 : public static Key key(Change.Id changeId, String uuid) {
33 104 : return new AutoValue_ChangeMessage_Key(changeId, uuid);
34 : }
35 :
36 : @AutoValue
37 104 : public abstract static class Key {
38 : public abstract Change.Id changeId();
39 :
40 : public abstract String uuid();
41 : }
42 :
43 : private Key key;
44 :
45 : /** Who wrote this comment; null if it was written by the Gerrit system. */
46 : @Nullable private Account.Id author;
47 :
48 : /** When this comment was drafted. */
49 : private Instant writtenOn;
50 :
51 : /**
52 : * The text left by the user or Gerrit system in template form, that is free of Gerrit User
53 : * Identifiable Information and can be persisted in data storage.
54 : */
55 : @Nullable private String message;
56 :
57 : /** Which patchset (if any) was this message generated from? */
58 : @Nullable private PatchSet.Id patchset;
59 :
60 : /** Tag associated with change message */
61 : @Nullable private String tag;
62 :
63 : /** Real user that added this message on behalf of the user recorded in {@link #author}. */
64 : @Nullable private Account.Id realAuthor;
65 :
66 : private ChangeMessage() {}
67 :
68 : public static ChangeMessage create(
69 : final ChangeMessage.Key k, @Nullable Account.Id a, Instant wo, @Nullable PatchSet.Id psid) {
70 2 : return create(k, a, wo, psid, /*messageTemplate=*/ null, /*realAuthor=*/ null, /*tag=*/ null);
71 : }
72 :
73 : public static ChangeMessage create(
74 : final ChangeMessage.Key k,
75 : @Nullable Account.Id a,
76 : Instant wo,
77 : @Nullable PatchSet.Id psid,
78 : @Nullable String messageTemplate,
79 : @Nullable Account.Id realAuthor,
80 : @Nullable String tag) {
81 104 : ChangeMessage message = new ChangeMessage();
82 104 : message.key = k;
83 104 : message.author = a;
84 104 : message.writtenOn = wo;
85 104 : message.patchset = psid;
86 104 : message.message = messageTemplate;
87 : // Use null for same real author, as before the column was added.
88 104 : message.realAuthor = Objects.equals(a, realAuthor) ? null : realAuthor;
89 104 : message.tag = tag;
90 104 : return message;
91 : }
92 :
93 : public ChangeMessage.Key getKey() {
94 103 : return key;
95 : }
96 :
97 : /** If null, the message was written 'by the Gerrit system'. */
98 : public Account.Id getAuthor() {
99 104 : return author;
100 : }
101 :
102 : public Account.Id getRealAuthor() {
103 103 : return realAuthor != null ? realAuthor : getAuthor();
104 : }
105 :
106 : public Instant getWrittenOn() {
107 104 : return writtenOn;
108 : }
109 :
110 : /** Message template, as persisted in data storage. */
111 : public String getMessage() {
112 104 : return message;
113 : }
114 :
115 : public String getTag() {
116 103 : return tag;
117 : }
118 :
119 : public PatchSet.Id getPatchSetId() {
120 104 : return patchset;
121 : }
122 :
123 : @Override
124 : public boolean equals(Object o) {
125 2 : if (!(o instanceof ChangeMessage)) {
126 0 : return false;
127 : }
128 2 : ChangeMessage m = (ChangeMessage) o;
129 2 : return Objects.equals(key, m.key)
130 2 : && Objects.equals(author, m.author)
131 2 : && Objects.equals(writtenOn, m.writtenOn)
132 2 : && Objects.equals(message, m.message)
133 2 : && Objects.equals(patchset, m.patchset)
134 2 : && Objects.equals(tag, m.tag)
135 2 : && Objects.equals(realAuthor, m.realAuthor);
136 : }
137 :
138 : @Override
139 : public int hashCode() {
140 0 : return Objects.hash(key, author, writtenOn, message, patchset, tag, realAuthor);
141 : }
142 :
143 : @Override
144 : public String toString() {
145 0 : return "ChangeMessage{"
146 : + "key="
147 : + key
148 : + ", author="
149 : + author
150 : + ", realAuthor="
151 : + realAuthor
152 : + ", writtenOn="
153 : + writtenOn
154 : + ", patchset="
155 : + patchset
156 : + ", tag="
157 : + tag
158 : + ", message=["
159 : + message
160 : + "]}";
161 : }
162 : }
|