Merge "Sonar: Reduce cyclomatic complexity"
[portal.git] / ecomp-portal-BE-common / src / main / java / org / onap / portalapp / portal / service / EPAppCommonServiceImpl.java
index 24572fb..bbb8382 100644 (file)
@@ -4,6 +4,8 @@
  * ===================================================================
  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
  * ===================================================================
+ * Modifications Copyright (c) 2019 Samsung
+ * ===================================================================
  *
  * Unless otherwise specified, all software contained herein is licensed
  * under the Apache License, Version 2.0 (the "License");
@@ -143,12 +145,15 @@ public class EPAppCommonServiceImpl implements EPAppService {
                                EPLogUtil.logEcompError(logger, EPAppMessagesEnum.BeDaoSystemError, e);
                                return null;
                        }
+                       
                } else {
                        logger.error(EELFLoggerDelegate.errorLogger,
                                        "getUserAsAdminApps: only Account Admin may invoke this function!");
                        return new ArrayList<EPApp>();
                }
        }
+       
+       
 
        @Override
        public List<EPApp> getUserByOrgUserIdAsAdminApps(String orgUserId) {
@@ -195,7 +200,7 @@ public class EPAppCommonServiceImpl implements EPAppService {
                        ecompApp.setUrl(app.getUrl());
                        ecompApp.setAlternateUrl(app.getAlternateUrl());
                        ecompApp.setUebTopicName(app.getUebTopicName());
-                       ecompApp.setUebKey(app.getUebKey());
+                       //ecompApp.setUebKey(app.getUebKey());
                        ecompApp.setUebSecret(app.getUebSecret());
                        ecompApp.setEnabled(app.getEnabled());
                        ecompApp.setCentralAuth(app.getCentralAuth());
@@ -216,25 +221,66 @@ public class EPAppCommonServiceImpl implements EPAppService {
                }
        }
 
+       
+       
        @SuppressWarnings("unchecked")
        @Override
        public List<AppIdAndNameTransportModel> getAdminApps(EPUser user) {
-               if (adminRolesService.isAccountAdmin(user)) {
-                       String format = "SELECT app.APP_ID, app.APP_NAME, app.APP_TYPE FROM FN_APP app inner join FN_USER_ROLE userrole ON userrole.APP_ID=app.APP_ID "
-                                       + "where userrole.USER_ID = %d AND userrole.ROLE_ID=" + ACCOUNT_ADMIN_ROLE_ID
-                                       + " AND (app.ENABLED = 'Y' OR app.APP_ID=1)";
-                       String sql = String.format(format, user.getId());
-                       // sql += " AND app.APP_REST_ENDPOINT IS NOT NULL AND
-                       // app.APP_REST_ENDPOINT <> ''";
-                       logQuery(sql);
+
+               if (adminRolesService.isAccountAdmin(user) && adminRolesService.isRoleAdmin(user)) {
+                       final Map<String, Long> params = new HashMap<>();
+                       params.put("userId", user.getId());
+                       List applicationRoleswithAccountandRoleadmin = dataAccessService
+                                       .executeNamedQuery("getApplicationsofTheUserwithAdminAndRoleAdmin", params, null);
                        try {
-                               return dataAccessService.executeSQLQuery(sql, AppIdAndNameTransportModel.class, null);
+                               return applicationRoleswithAccountandRoleadmin;
                        } catch (Exception e) {
                                EPLogUtil.logEcompError(logger, EPAppMessagesEnum.BeDaoSystemError, e);
                                logger.error(EELFLoggerDelegate.errorLogger,
-                                               "Exception occurred while fetching the adminApps for user " + user.getLoginId(), e);
+                                               "Exception occurred while fetching the list of user who has type account and role approver "
+                                                               + user.getLoginId(),
+                                               e);
                        }
                }
+
+               else {
+                       if (adminRolesService.isAccountAdmin(user)) {
+                               String format = "SELECT app.APP_ID, app.APP_NAME, app.APP_TYPE FROM FN_APP app inner join FN_USER_ROLE userrole ON userrole.APP_ID=app.APP_ID "
+                                               + "where userrole.USER_ID = %d AND userrole.ROLE_ID=" + ACCOUNT_ADMIN_ROLE_ID
+                                               + " AND (app.ENABLED = 'Y' OR app.APP_ID=1)";
+                               String sql = String.format(format, user.getId());
+                               logQuery(sql);
+                               try {
+                                       return dataAccessService.executeSQLQuery(sql, AppIdAndNameTransportModel.class, null);
+                               } catch (Exception e) {
+                                       EPLogUtil.logEcompError(logger, EPAppMessagesEnum.BeDaoSystemError, e);
+                                       logger.error(EELFLoggerDelegate.errorLogger,
+                                                       "Exception occurred while fetching the adminApps for user " + user.getLoginId(), e);
+                               }
+
+                       }
+
+                       if (adminRolesService.isRoleAdmin(user)) {
+                               final Map<String, Long> params = new HashMap<>();
+                               params.put("userId", user.getId());
+                               List applicationRoles = dataAccessService.executeNamedQuery("getApplicationsofTheUserContainsApprover",
+                                               params, null);
+
+                               try {
+                                       return applicationRoles;
+                               } catch (Exception e) {
+                                       EPLogUtil.logEcompError(logger, EPAppMessagesEnum.BeDaoSystemError, e);
+                                       logger.error(EELFLoggerDelegate.errorLogger,
+                                                       "Exception occurred while fetching the list of user who has type approver "
+                                                                       + user.getLoginId(),
+                                                       e);
+                               }
+
+                       }
+               }
+               // sql += " AND app.APP_REST_ENDPOINT IS NOT NULL AND
+               // app.APP_REST_ENDPOINT <> ''";
+
                return new ArrayList<AppIdAndNameTransportModel>();
        }
 
@@ -444,17 +490,40 @@ public class EPAppCommonServiceImpl implements EPAppService {
 
        protected FieldsValidator onboardingAppFieldsChecker(OnboardingApp onboardingApp) {
                FieldsValidator fieldsValidator = new FieldsValidator();
+               if(onboardingApp.isCentralAuth){
                if (onboardingApp.name == null || onboardingApp.name.length() == 0 || onboardingApp.url == null
                                || onboardingApp.url.length() == 0 || onboardingApp.restrictedApp == null
                                || onboardingApp.isOpen == null || onboardingApp.isEnabled == null
-                               || (onboardingApp.id != null && onboardingApp.id.equals(ECOMP_APP_ID))
-                               // For a normal app (appType==1), these fields must be filled
+                               || (onboardingApp.id != null && ECOMP_APP_ID.equals(onboardingApp.id.toString()))
+                               // For a normal app (appType == PortalConstants.PortalAppId),
+                               // these fields must be filled
                                // in.
                                // For a restricted app (appType==2), they will be empty.
-                               || ((!onboardingApp.restrictedApp)
-                                               && (onboardingApp.username == null || onboardingApp.username.length() == 0
-                                                               || onboardingApp.appPassword == null || onboardingApp.appPassword.length() == 0))) {
+                               || ((!onboardingApp.restrictedApp) && (onboardingApp.myLoginsAppName == null
+                                               || onboardingApp.myLoginsAppName.length() == 0 || onboardingApp.myLoginsAppOwner == null
+                                               || onboardingApp.myLoginsAppOwner.length() == 0 || onboardingApp.username == null
+                                               || onboardingApp.username.length() == 0 ))) {
                        fieldsValidator.httpStatusCode = new Long(HttpServletResponse.SC_BAD_REQUEST);
+               }
+               }else{
+
+                       if (onboardingApp.name == null || onboardingApp.name.length() == 0 || onboardingApp.url == null
+                                       || onboardingApp.url.length() == 0 || onboardingApp.restrictedApp == null
+                                       || onboardingApp.isOpen == null || onboardingApp.isEnabled == null
+                                       || (onboardingApp.id != null && ECOMP_APP_ID.equals(onboardingApp.id.toString()))
+                                       // For a normal app (appType == PortalConstants.PortalAppId),
+                                       // these fields must be filled
+                                       // in.
+                                       // For a restricted app (appType==2), they will be empty.
+                                       || ((!onboardingApp.restrictedApp) && (onboardingApp.myLoginsAppName == null
+                                                       || onboardingApp.myLoginsAppName.length() == 0 || onboardingApp.myLoginsAppOwner == null
+                                                       || onboardingApp.myLoginsAppOwner.length() == 0 || onboardingApp.username == null
+                                                       || onboardingApp.username.length() == 0 || onboardingApp.appPassword == null
+                                                       || onboardingApp.appPassword.length() == 0))) {
+                               fieldsValidator.httpStatusCode = new Long(HttpServletResponse.SC_BAD_REQUEST);
+                       }
+                       
+                       
                }
                return fieldsValidator;
        }
@@ -1426,7 +1495,7 @@ public class EPAppCommonServiceImpl implements EPAppService {
        // Don't encrypt or decrypt the password if it is null or the empty string
        private String decryptedPassword(String encryptedAppPwd, EPApp app) {
                String result = "";
-               if (encryptedAppPwd != null & encryptedAppPwd.length() > 0) {
+               if (encryptedAppPwd != null && !encryptedAppPwd.isEmpty()) {
                        try {
                                result = CipherUtil.decryptPKC(encryptedAppPwd,
                                                SystemProperties.getProperty(SystemProperties.Decryption_Key));
@@ -1439,7 +1508,7 @@ public class EPAppCommonServiceImpl implements EPAppService {
 
        protected String encryptedPassword(String decryptedAppPwd, EPApp app) {
                String result = "";
-               if (decryptedAppPwd != null & decryptedAppPwd.length() > 0) {
+               if (decryptedAppPwd != null && !decryptedAppPwd.isEmpty()) {
                        try {
                                result = CipherUtil.encryptPKC(decryptedAppPwd,
                                                SystemProperties.getProperty(SystemProperties.Decryption_Key));
@@ -1732,4 +1801,4 @@ public class EPAppCommonServiceImpl implements EPAppService {
                return userAndRoles;
                
        }
-}
\ No newline at end of file
+}