Upgrade to latest oparent
[aaf/authz.git] / authz-batch / src / main / java / com / att / authz / actions / URFutureApprove.java
1 /*******************************************************************************
2  * Copyright (c) 2016 AT&T Intellectual Property. All rights reserved.
3  *******************************************************************************/
4 package com.att.authz.actions;
5
6 import java.io.IOException;
7 import java.util.Date;
8 import java.util.GregorianCalendar;
9 import java.util.List;
10
11 import com.att.authz.env.AuthzTrans;
12 import com.att.authz.helpers.UserRole;
13 import com.att.authz.layer.Result;
14 import com.att.authz.org.Organization.Expiration;
15 import com.att.authz.org.Organization.Identity;
16 import com.att.dao.aaf.cass.FutureDAO;
17 import com.att.dao.aaf.cass.NsDAO;
18 import com.att.dao.aaf.hl.Function;
19 import com.att.dao.aaf.hl.Question;
20 import org.onap.aaf.inno.env.APIException;
21 import org.onap.aaf.inno.env.util.Chrono;
22 import com.datastax.driver.core.Cluster;
23
24 public class URFutureApprove extends ActionDAO<UserRole, List<Identity>> implements Action<UserRole,List<Identity>>, Key<UserRole> {
25         private final Date start, expires;
26
27         public URFutureApprove(AuthzTrans trans, Cluster cluster) throws APIException, IOException {
28                 super(trans,cluster);
29                 GregorianCalendar gc = new GregorianCalendar();
30                 start = gc.getTime();
31                 expires = trans.org().expiration(gc, Expiration.Future).getTime();
32         }
33         
34         public URFutureApprove(AuthzTrans trans, ActionDAO<?,?> adao) {
35                 super(trans, adao);
36                 GregorianCalendar gc = new GregorianCalendar();
37                 start = gc.getTime();
38                 expires = trans.org().expiration(gc, Expiration.Future).getTime();
39         }
40
41         @Override
42         public Result<List<Identity>> exec(AuthzTrans trans, UserRole ur) {
43                 Result<NsDAO.Data> rns = q.deriveNs(trans, ur.ns);
44                 if(rns.isOK()) {
45                         
46                         FutureDAO.Data data = new FutureDAO.Data();
47                         data.id=null; // let Create function assign UUID
48                         data.target=Function.FOP_USER_ROLE;
49                         
50                         data.memo = key(ur);
51                         data.start = start;
52                         data.expires = expires;
53                         try {
54                                 data.construct = ur.to().bytify();
55                         } catch (IOException e) {
56                                 return Result.err(e);
57                         }
58                         Result<List<Identity>> rapprovers = f.createFuture(trans, data, Function.FOP_USER_ROLE, ur.user, rns.value, "U");
59                         return rapprovers;
60                 } else {
61                         return Result.err(rns);
62                 }
63         }
64         
65         @Override
66         public String key(UserRole ur) {
67                 String expire;
68                 if(expires.before(start)) {
69                         expire = "' - EXPIRED ";
70                 } else {
71                         expire = "' - expiring ";
72                 }
73                 
74                 if(Question.OWNER.equals(ur.rname)) {
75                         return "Re-Validate Ownership for AAF Namespace '" + ur.ns + expire + Chrono.dateOnlyStamp(ur.expires);
76                 } else if(Question.ADMIN.equals(ur.rname)) {
77                         return "Re-Validate as Administrator for AAF Namespace '" + ur.ns + expire + Chrono.dateOnlyStamp(ur.expires);
78                 } else {
79                         return "Re-Approval in Role '" + ur.role + expire + Chrono.dateOnlyStamp(ur.expires);
80                 }
81         }
82
83 }