Merge "Continue addressing technical debt for ONAP-XACML"
[policy/engine.git] / POLICY-SDK-APP / src / main / java / org / onap / policy / admin / PolicyManagerServlet.java
index e62c878..54a14cb 100644 (file)
@@ -68,7 +68,6 @@ import org.onap.policy.common.logging.flexlogger.Logger;
 import org.onap.policy.components.HumanPolicyComponent;
 import org.onap.policy.controller.PolicyController;
 import org.onap.policy.controller.PolicyExportAndImportController;
-import org.onap.policy.model.Roles;
 import org.onap.policy.rest.XACMLRest;
 import org.onap.policy.rest.XACMLRestProperties;
 import org.onap.policy.rest.adapter.PolicyRestAdapter;
@@ -79,6 +78,7 @@ import org.onap.policy.rest.jpa.PolicyEntity;
 import org.onap.policy.rest.jpa.PolicyVersion;
 import org.onap.policy.rest.jpa.UserInfo;
 import org.onap.policy.utils.PolicyUtils;
+import org.onap.policy.utils.UserUtils.Pair;
 import org.onap.policy.xacml.api.XACMLErrorConstants;
 import org.onap.policy.xacml.util.XACMLPolicyScanner;
 import org.onap.portalsdk.core.web.support.UserUtils;
@@ -314,8 +314,8 @@ public class PolicyManagerServlet extends HttpServlet {
        }
 
        private JSONObject searchPolicyList(JSONObject params, HttpServletRequest request) {
-               Set<String> scopes = null;
-               List<String> roles = null;
+               Set<String> scopes;
+               List<String> roles;
                List<Object> policyData = new ArrayList<>();
                JSONArray policyList = null;
                if(params.has("policyList")){
@@ -326,24 +326,10 @@ public class PolicyManagerServlet extends HttpServlet {
                try {
                        //Get the Login Id of the User from Request
                        String userId =  UserUtils.getUserSession(request).getOrgUserId();
-                       //Check if the Role and Scope Size are Null get the values from db.
                        List<Object> userRoles = controller.getRoles(userId);
-                       roles = new ArrayList<>();
-                       scopes = new HashSet<>();
-                       for(Object role: userRoles){
-                               Roles userRole = (Roles) role;
-                               roles.add(userRole.getRole());
-                               if(userRole.getScope() != null){
-                                       if(userRole.getScope().contains(",")){
-                                               String[] multipleScopes = userRole.getScope().split(",");
-                                               for(int i =0; i < multipleScopes.length; i++){
-                                                       scopes.add(multipleScopes[i]);
-                                               }
-                                       }else{
-                                               scopes.add(userRole.getScope());
-                                       }
-                               }
-                       }
+                       Pair<Set<String>, List<String>> pair = org.onap.policy.utils.UserUtils.checkRoleAndScope(userRoles);
+                       roles = pair.u;
+                       scopes = pair.t;
                        if (roles.contains(ADMIN) || roles.contains(EDITOR) || roles.contains(GUEST) ) {
                                if(scopes.isEmpty()){
                                        return error("No Scopes has been Assigned to the User. Please, Contact Super-Admin");
@@ -442,7 +428,7 @@ public class PolicyManagerServlet extends HttpServlet {
                        policyName = removeExtension.substring(0, removeExtension.lastIndexOf('.'));
                }
 
-               String activePolicy = null;
+               String activePolicy;
                PolicyController controller = getPolicyControllerInstance();
                if(params.toString().contains("activeVersion")){
                        String activeVersion = params.getString("activeVersion");
@@ -572,24 +558,10 @@ public class PolicyManagerServlet extends HttpServlet {
                        //Get the Login Id of the User from Request
                        String testUserID = getTestUserId();
                        String userId =  testUserID != null ? testUserID : UserUtils.getUserSession(request).getOrgUserId();
-                       //Check if the Role and Scope Size are Null get the values from db.
                        List<Object> userRoles = controller.getRoles(userId);
-                       roles = new ArrayList<>();
-                       scopes = new HashSet<>();
-                       for(Object role: userRoles){
-                               Roles userRole = (Roles) role;
-                               roles.add(userRole.getRole());
-                               if(userRole.getScope() != null){
-                                       if(userRole.getScope().contains(",")){
-                                               String[] multipleScopes = userRole.getScope().split(",");
-                                               for(int i =0; i < multipleScopes.length; i++){
-                                                       scopes.add(multipleScopes[i]);
-                                               }
-                                       }else{
-                                               scopes.add(userRole.getScope());
-                                       }
-                               }
-                       }
+                       Pair<Set<String>, List<String>> pair = org.onap.policy.utils.UserUtils.checkRoleAndScope(userRoles);
+                       roles = pair.u;
+                       scopes = pair.t;
 
                        List<JSONObject> resultList = new ArrayList<>();
                        boolean onlyFolders = params.getBoolean("onlyFolders");
@@ -659,7 +631,7 @@ public class PolicyManagerServlet extends HttpServlet {
        }
 
        private List<Object> queryPolicyEditorScopes(String scopeName){
-               String scopeNamequery = "";
+               String scopeNamequery;
                SimpleBindings params = new SimpleBindings();
                if(scopeName == null){
                        scopeNamequery = "from PolicyEditorScopes";
@@ -668,7 +640,7 @@ public class PolicyManagerServlet extends HttpServlet {
                        params.put("scopeName", scopeName + "%");
                }
                PolicyController controller = getPolicyControllerInstance();
-               List<Object> scopesList = null;
+               List<Object> scopesList;
                if(PolicyController.isjUnit()){
                        scopesList = controller.getDataByQuery(scopeNamequery, null);
                }else{
@@ -692,8 +664,8 @@ public class PolicyManagerServlet extends HttpServlet {
                SimpleBindings params = new SimpleBindings();
                params.put("scopeName", scopeName + "%");
 
-               List<Object> activePolicies = null;
-               List<Object> scopesList = null;
+               List<Object> activePolicies;
+               List<Object> scopesList;
                if(PolicyController.isjUnit()){
                        activePolicies = controller.getDataByQuery(query, null);
                        scopesList = controller.getDataByQuery(scopeNamequery, null);
@@ -727,7 +699,7 @@ public class PolicyManagerServlet extends HttpServlet {
                                }
                        }
                }
-               String scopeNameCheck = null;
+               String scopeNameCheck;
                for (Object list : activePolicies) {
                        PolicyVersion policy = (PolicyVersion) list;
                        String scopeNameValue = policy.getPolicyName().substring(0, policy.getPolicyName().lastIndexOf(File.separator));
@@ -862,7 +834,7 @@ public class PolicyManagerServlet extends HttpServlet {
 
        private JSONObject policyRename(String oldPath, String newPath, String userId) throws ServletException {
                try {
-                       PolicyEntity entity = null;
+                       PolicyEntity entity;
                        PolicyController controller = getPolicyControllerInstance();
 
                        String policyVersionName = newPath.replace(".xml", "");
@@ -1004,7 +976,7 @@ public class PolicyManagerServlet extends HttpServlet {
 
        private JSONObject cloneRecord(String newpolicyName, String oldScope, String removeoldPolicyExtension, String newScope, String removenewPolicyExtension, PolicyEntity entity, String userId) throws ServletException{
                FileWriter fw = null;
-               String queryEntityName = null;
+               String queryEntityName;
                PolicyController controller = getPolicyControllerInstance();
                PolicyEntity cloneEntity = new PolicyEntity();
                cloneEntity.setPolicyName(newpolicyName);
@@ -1174,7 +1146,7 @@ public class PolicyManagerServlet extends HttpServlet {
                        path = path.substring(path.indexOf('/')+1);
                        String policyNamewithExtension = path.replace("/", File.separator);
                        String policyVersionName = policyNamewithExtension.replace(".xml", "");
-                       String query = "";
+                       String query;
                        SimpleBindings policyParams = new SimpleBindings();
                        if(path.endsWith(".xml")){
                                policyNamewithoutExtension = policyVersionName.substring(0, policyVersionName.lastIndexOf('.'));
@@ -1413,7 +1385,7 @@ public class PolicyManagerServlet extends HttpServlet {
                        SimpleBindings peParams = new SimpleBindings();
                        peParams.put("split_1", split[1]);
                        peParams.put("split_0", split[0]);
-                       List<Object> queryData = null;
+                       List<Object> queryData;
                        if(PolicyController.isjUnit()){
                                queryData = controller.getDataByQuery(query, null);
                        }else{