1
1
package org .support .project .knowledge .logic .notification .webhook ;
2
2
3
+ import java .io .IOException ;
4
+ import java .io .StringReader ;
5
+ import java .io .UnsupportedEncodingException ;
3
6
import java .text .SimpleDateFormat ;
4
7
import java .util .HashMap ;
8
+ import java .util .Iterator ;
5
9
import java .util .List ;
6
10
import java .util .Map ;
7
11
8
12
import org .support .project .common .log .Log ;
9
13
import org .support .project .common .log .LogFactory ;
14
+ import org .support .project .common .util .FileUtil ;
10
15
import org .support .project .common .util .PropertyUtil ;
16
+ import org .support .project .common .util .StringUtils ;
11
17
import org .support .project .di .Container ;
12
18
import org .support .project .di .DI ;
13
19
import org .support .project .di .Instance ;
20
+ import org .support .project .knowledge .dao .CommentsDao ;
21
+ import org .support .project .knowledge .dao .KnowledgesDao ;
14
22
import org .support .project .knowledge .dao .WebhookConfigsDao ;
15
23
import org .support .project .knowledge .dao .WebhooksDao ;
16
24
import org .support .project .knowledge .entity .CommentsEntity ;
20
28
import org .support .project .knowledge .logic .MailLogic ;
21
29
import org .support .project .knowledge .logic .NotifyLogic ;
22
30
import org .support .project .knowledge .logic .WebhookLogic ;
31
+ import org .support .project .knowledge .logic .notification .QueueNotification ;
32
+ import org .support .project .knowledge .vo .notification .webhook .WebhookCommentJson ;
23
33
import org .support .project .web .dao .UsersDao ;
24
34
import org .support .project .web .entity .UsersEntity ;
25
35
36
+ import com .google .gson .JsonArray ;
37
+ import com .google .gson .JsonElement ;
38
+ import com .google .gson .JsonObject ;
39
+ import com .google .gson .JsonParser ;
40
+ import com .google .gson .JsonPrimitive ;
41
+
26
42
import net .arnx .jsonic .JSON ;
27
43
28
44
/**
@@ -38,6 +54,8 @@ public static CommentInsertWebhookNotification get() {
38
54
return Container .getComp (CommentInsertWebhookNotification .class );
39
55
}
40
56
57
+ private KnowledgeUpdateWebHookNotification knowledgeUpdateWebHookNotification = KnowledgeUpdateWebHookNotification .get ();
58
+
41
59
/**
42
60
* コメント追加のWebhookの登録を行う
43
61
* @param comment
@@ -46,34 +64,144 @@ public static CommentInsertWebhookNotification get() {
46
64
public void sendCommentWebhook (CommentsEntity comment , KnowledgesEntity knowledge ) {
47
65
WebhookConfigsDao webhookConfigsDao = WebhookConfigsDao .get ();
48
66
List <WebhookConfigsEntity > webhookConfigsEntities = webhookConfigsDao .selectOnHook (WebhookConfigsEntity .HOOK_COMMENTS );
49
-
50
67
if (0 == webhookConfigsEntities .size ()) {
51
68
return ;
52
69
}
53
-
54
- Map <String , Object > commentData = getCommentData (comment , knowledge );
55
-
70
+ String json = createWebhookJson (comment , knowledge );
56
71
WebhooksEntity webhooksEntity = new WebhooksEntity ();
57
72
String webhookId = MailLogic .get ().idGen ("Notify" );
58
73
webhooksEntity .setWebhookId (webhookId );
59
74
webhooksEntity .setStatus (WebhookLogic .WEBHOOK_STATUS_UNSENT );
60
75
webhooksEntity .setHook (WebhookConfigsEntity .HOOK_COMMENTS );
61
- webhooksEntity .setContent (JSON .encode (commentData ));
62
-
76
+ webhooksEntity .setContent (json );
63
77
if (LOG .isTraceEnabled ()) {
64
78
LOG .trace (PropertyUtil .reflectionToString (webhooksEntity ));
65
79
}
66
-
67
80
WebhooksDao .get ().insert (webhooksEntity );
68
81
}
82
+ /**
83
+ * Webhook通知情報に格納するJSONを生成する 送信先に合わせて送るJSONの型を変更するため、初めはキーの値のみをJSONとして保存している
84
+ *
85
+ * @param knowledge
86
+ * @param comment
87
+ * @return
88
+ */
89
+ private String createWebhookJson (CommentsEntity comment , KnowledgesEntity knowledge ) {
90
+ WebhookCommentJson json = new WebhookCommentJson ();
91
+ json .commentId = comment .getCommentNo ();
92
+ return JSON .encode (json );
93
+ }
69
94
70
-
95
+ /**
96
+ * Webhookで送信するJSONを生成する 送信先毎に送る型を変化させることが可能
97
+ *
98
+ * @param entity
99
+ * @param configEntity
100
+ * @throws IOException
101
+ * @throws UnsupportedEncodingException
102
+ */
103
+ public String createSendJson (WebhooksEntity entity , WebhookConfigsEntity configEntity ) throws UnsupportedEncodingException , IOException {
104
+ String template = configEntity .getTemplate ();
105
+ if (StringUtils .isEmpty (template )) {
106
+ template = FileUtil .read (getClass ().getResourceAsStream ("comment_template.json" ), "UTF-8" );
107
+ }
108
+ WebhookCommentJson json = JSON .decode (entity .getContent (), WebhookCommentJson .class );
109
+ CommentsEntity comment = CommentsDao .get ().selectOnKey (json .commentId );
110
+ UsersEntity insertUser = UsersDao .get ().selectOnKey (comment .getInsertUser ());
111
+ if (insertUser != null ) {
112
+ comment .setInsertUserName (insertUser .getUserName ());
113
+ }
114
+ UsersEntity updateUser = UsersDao .get ().selectOnKey (comment .getInsertUser ());
115
+ if (updateUser != null ) {
116
+ comment .setUpdateUserName (updateUser .getUserName ());
117
+ }
118
+ KnowledgesEntity knowledge = KnowledgesDao .get ().selectOnKeyWithUserName (comment .getKnowledgeId ());
119
+ JsonElement send = new JsonParser ().parse (new StringReader (template ));
120
+ buildJson (send .getAsJsonObject (), comment , knowledge );
121
+ return send .toString ();
122
+ }
123
+ private void buildJson (JsonObject obj , CommentsEntity comment , KnowledgesEntity knowledge ) {
124
+ Iterator <String > props = obj .keySet ().iterator ();
125
+ while (props .hasNext ()) {
126
+ String prop = (String ) props .next ();
127
+ JsonElement e = obj .get (prop );
128
+ if (e .isJsonPrimitive ()) {
129
+ JsonElement conv = convValue (e .getAsJsonPrimitive (), comment , knowledge );
130
+ if (conv != null ) {
131
+ obj .add (prop , conv );
132
+ }
133
+ } else if (e .isJsonObject ()) {
134
+ LOG .info ("property:" + prop + " is object." );
135
+ JsonObject child = e .getAsJsonObject ();
136
+ buildJson (child , comment , knowledge );
137
+ } else if (e .isJsonArray ()) {
138
+ JsonArray array = e .getAsJsonArray ();
139
+ for (int i = 0 ; i < array .size (); i ++) {
140
+ JsonElement item = array .get (i );
141
+ if (item .isJsonObject ()) {
142
+ JsonObject child = item .getAsJsonObject ();
143
+ buildJson (child , comment , knowledge );
144
+ }
145
+ }
146
+ }
147
+ }
148
+ }
149
+ private JsonElement convValue (JsonPrimitive primitive , CommentsEntity comment , KnowledgesEntity knowledge ) {
150
+ JsonElement conv = convValueOnComment (primitive , comment , knowledge );
151
+ if (conv != null ) {
152
+ return conv ;
153
+ }
154
+ return knowledgeUpdateWebHookNotification .convValue (primitive , knowledge , QueueNotification .TYPE_KNOWLEDGE_UPDATE , false );
155
+ }
156
+ private JsonElement convValueOnComment (JsonPrimitive primitive , CommentsEntity comment , KnowledgesEntity knowledge ) {
157
+ if (!primitive .isString ()) {
158
+ return null ;
159
+ }
160
+ String val = primitive .getAsString ();
161
+ if (!val .startsWith ("{" ) || !val .endsWith ("}" )) {
162
+ return null ;
163
+ }
164
+ String item = val .substring (1 , val .length () - 1 );
165
+ String option = "" ;
166
+ if (item .indexOf ("," ) != -1 ) {
167
+ option = item .substring (item .indexOf ("," ) + 1 );
168
+ item = item .substring (0 , item .indexOf ("," ));
169
+ }
170
+ LOG .debug (item + " : " + option );
171
+ String [] sp = item .split ("\\ ." );
172
+ LOG .debug (sp );
173
+ if (sp .length == 2 ) {
174
+ String name = sp [0 ];
175
+ String prop = sp [1 ];
176
+ if (name .equals ("comment" )) {
177
+ if (prop .equals ("text" )) {
178
+ /** This code make JSON to send Slack */
179
+ String linktop = "<" ;
180
+ String linkpipe = "|" ;
181
+ String linkend = ">" ;
182
+
183
+ /** This code make JSON to send Slack */
184
+ StringBuffer SendBuff = new StringBuffer ();
185
+ SendBuff .append (linktop );
186
+ SendBuff .append (NotifyLogic .get ().makeURL (knowledge .getKnowledgeId ()));
187
+ SendBuff .append (linkpipe );
188
+ SendBuff .append (knowledge .getTitle ());
189
+ SendBuff .append (linkend );
190
+ String SendString = SendBuff .toString ();
191
+ return new JsonPrimitive (SendString );
192
+ }
193
+ return knowledgeUpdateWebHookNotification .convValue (comment , prop , option );
194
+ }
195
+ }
196
+ return null ;
197
+ }
71
198
/**
72
199
* コメントのjsonデータを取得する
73
200
*
74
201
* @param comment
75
202
* @param knowledge
76
203
* @return
204
+ * @deprecated
77
205
*/
78
206
public Map <String , Object > getCommentData (CommentsEntity comment , KnowledgesEntity knowledge ) {
79
207
Map <String , Object > jsonObject = new HashMap <String , Object >();
@@ -117,6 +245,7 @@ public Map<String, Object> getCommentData(CommentsEntity comment, KnowledgesEnti
117
245
jsonObject .put ("knowledge" , KnowledgeUpdateWebHookNotification .get ().getKnowledgeData (knowledge , null ));
118
246
return jsonObject ;
119
247
}
248
+
120
249
121
250
122
251
}
0 commit comments