summaryrefslogtreecommitdiffstats
path: root/authz-cass/src/main/java/com/att/dao/aaf/cached/CachedUserRoleDAO.java
blob: 2478759ef6a5d9cccb4d2656de17e2b8975dc312 (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
/*******************************************************************************
 * ============LICENSE_START====================================================
 * * org.onap.aaf
 * * ===========================================================================
 * * Copyright © 2017 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====================================================
 * *
 * * ECOMP is a trademark and service mark of AT&T Intellectual Property.
 * *
 ******************************************************************************/
package com.att.dao.aaf.cached;

import java.util.ArrayList;
import java.util.List;

import com.att.authz.env.AuthzTrans;
import com.att.authz.layer.Result;
import com.att.dao.CIDAO;
import com.att.dao.CachedDAO;
import com.att.dao.aaf.cass.Status;
import com.att.dao.aaf.cass.UserRoleDAO;
import com.att.dao.aaf.cass.UserRoleDAO.Data;
import com.att.inno.env.Slot;

public class CachedUserRoleDAO extends CachedDAO<AuthzTrans,UserRoleDAO, UserRoleDAO.Data> {
	private Slot transURSlot;

	public CachedUserRoleDAO(UserRoleDAO dao, CIDAO<AuthzTrans> info) {
		super(dao, info, UserRoleDAO.CACHE_SEG);
		transURSlot = dao.transURSlot;
	}

	/**
	 * Special Case.  
	 * User Roles by User are very likely to be called many times in a Transaction, to validate "May User do..."
	 * Pull result, and make accessible by the Trans, which is always keyed by User.
	 * @param trans
	 * @param user
	 * @return
	 */
	public Result<List<Data>> readByUser(AuthzTrans trans, final String user) {
		DAOGetter getter = new DAOGetter(trans,dao()) {
			public Result<List<Data>> call() {
				// If the call is for THIS user, and it exists, get from TRANS, add to TRANS if not.
				if(user!=null && user.equals(trans.user())) {
					Result<List<Data>> transLD = trans.get(transURSlot,null);
					if(transLD==null ) {
						transLD = dao.readByUser(trans, user);
					}
					return transLD;
				} else {
					return dao.readByUser(trans, user);
				}
			}
		};
		Result<List<Data>> lurd = get(trans, user, getter);
		if(lurd.isOK() && lurd.isEmpty()) {
			return Result.err(Status.ERR_UserRoleNotFound,"UserRole not found for [%s]",user);
		}
		return lurd;
	}

	
	public Result<List<Data>> readByRole(AuthzTrans trans, final String role) {
		DAOGetter getter = new DAOGetter(trans,dao()) {
			public Result<List<Data>> call() {
				return dao.readByRole(trans, role);
			}
		};
		Result<List<Data>> lurd = get(trans, role, getter);
		if(lurd.isOK() && lurd.isEmpty()) {
			return Result.err(Status.ERR_UserRoleNotFound,"UserRole not found for [%s]",role);
		}
		return lurd;
	}

	public Result<List<UserRoleDAO.Data>> readUserInRole(final AuthzTrans trans, final String user, final String role) {
		DAOGetter getter = new DAOGetter(trans,dao()) {
			public Result<List<Data>> call() {
				if(user.equals(trans.user())) {
					Result<List<Data>> rrbu = readByUser(trans, user);
					if(rrbu.isOK()) {
						List<Data> ld = new ArrayList<Data>(1);
						for(Data d : rrbu.value) {
							if(d.role.equals(role)) {
								ld.add(d);
								break;
							}
						}
						return Result.ok(ld).emptyList(ld.isEmpty());
					} else {
						return rrbu;
					}
				}
				return dao.readByUserRole(trans, user, role);
			}
		};
		Result<List<Data>> lurd = get(trans, keyFromObjs(user,role), getter);
		if(lurd.isOK() && lurd.isEmpty()) {
			return Result.err(Status.ERR_UserRoleNotFound,"UserRole not found for role [%s] and user [%s]",role,user);
		}
		return lurd;
	}
}