summaryrefslogtreecommitdiffstats
path: root/auth/auth-batch/src/main/java/org/onap/aaf/auth/batch/reports/bodies/NotifyBody.java
blob: 453c2f2f6291312139d82ed4fca22a2d54786a50 (plain)
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
199
200
201
202
203
204
205
206
/**
 * ============LICENSE_START====================================================
 * org.onap.aaf
 * ===========================================================================
 * Copyright (c) 2018 AT&T Intellectual Property. All rights reserved.
 * ===========================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END====================================================
 *
 */
package org.onap.aaf.auth.batch.reports.bodies;

import java.io.File;
import java.io.IOException;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Modifier;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;

import org.onap.aaf.auth.batch.reports.Notify;
import org.onap.aaf.auth.env.AuthzTrans;
import org.onap.aaf.cadi.Access;
import org.onap.aaf.misc.env.APIException;

public abstract class NotifyBody {
	private static final String DUPL = "<td style=\"text-indent: 4em;\">''</td>";
	private static final Map<String,NotifyBody> bodyMap = new HashMap<>();

	protected Map<String,List<List<String>>> rows;
	private final String name;
	private final String type;
	private String date;
	private int escalation;
	
	public NotifyBody(final String type, final String name) {
		rows = new TreeMap<>();
		this.name = name;
		this.type = type;
		date="";
		escalation = 1;
	}
	
	public void store(List<String> row) {
		if(!row.isEmpty()) {
			if("info".equals(row.get(0))) {
				if(row.size()>2) {
					date = row.get(2);
				}
				if(row.size()>3) {
					escalation = Integer.parseInt(row.get(3));
				}
				return;
			} else if(type.equals(row.get(0))) {
				String user = user(row);
				if(user!=null) {
					List<List<String>> lss = rows.get(user); 
					if(lss == null) {
						lss = new ArrayList<>();
						rows.put(user,lss);
					}
					lss.add(row);
				}
			}
		}
	}

	public String name() {
		return name;
	}
	
	public String date() {
		return date;
	}
	public int escalation() {
		return escalation;
	}
	
	public Set<String> users() {
		return rows.keySet();
	}
	
	/**
	 * ID must be set from Row for Email lookup
	 * 
	 * @param trans
	 * @param n
	 * @param id
	 * @param row
	 * @return
	 */
	public abstract boolean body(AuthzTrans trans, StringBuilder sb, int indent, Notify n, String id);
	
	/**
	 * Return "null" if user not found in row... Code will handle.
	 * @param row
	 * @return
	 */
	protected abstract String user(List<String> row);
	
	/**
	 * Get Notify Body based on key of
	 * type|name
	 */
	public static NotifyBody get(String key) {
		return bodyMap.get(key);
	}
	
	/**
	 * Return set of loaded NotifyBodies
	 * 
	 */
	public static Collection<NotifyBody> getAll() {
		// Note: The same Notify Body is entered several times with different keys.
		// Therefore, need a Set of Values, not all the Values.
		Set<NotifyBody> set = new HashSet<>();
		set.addAll(bodyMap.values());
		return set;
	}
	
	/**
	 * @param propAccess 
	 * @throws URISyntaxException 
	 * 
	 */
	public static void load(Access access) throws APIException, IOException {
		// class load available NotifyBodies
		ClassLoader cl = Thread.currentThread().getContextClassLoader();
		Package pkg = NotifyBody.class.getPackage();
		String path = pkg.getName().replace('.', '/');
		URL url = cl.getResource(path);
		if(url == null) {
			throw new APIException("Cannot load resources from " + path);
		}
		File dir;
		try {
			dir = new File(url.toURI());
		} catch (URISyntaxException e) {
			throw new APIException(e);
		}
		if(dir.exists()) {
			String[] files = dir.list();
			if(files!=null) {
				for(String sf : files) {
					int dot = sf.indexOf('.');
					if(dot>=0) {
						String cls = pkg.getName()+'.'+sf.substring(0,dot);
						try {
							Class<?> c = cl.loadClass(cls);
							if(c!=null) {
								if(!Modifier.isAbstract(c.getModifiers())) {
									Constructor<?> cst = c.getConstructor(Access.class);
									NotifyBody nb = (NotifyBody)cst.newInstance(access);
									if(nb!=null) {
										bodyMap.put("info|"+nb.name, nb);
										bodyMap.put(nb.type+'|'+nb.name, nb);
									}
								}
							}
						} catch (ClassNotFoundException | NoSuchMethodException | SecurityException | InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
							e.printStackTrace();
						}
					}
				}
			}
		}
	}
	

	protected void println(StringBuilder sb, int indent, Object ... objs) {
		for(int i=0;i<indent;++i) {
			sb.append(' ');
		}
		for(Object o : objs) {
			sb.append(o.toString());
		}
		sb.append('\n');
	}
	
	protected void printCell(StringBuilder sb, int indent, String current, String prev) {
		if(current.equals(prev)) {
			println(sb,indent,DUPL);
		} else {
			println(sb,indent,"<td>",current,"</td>");
		}
	}

}