UserRolesController methods up
[portal.git] / portal-BE / src / main / java / org / onap / portal / service / fn / FnAppService.java
1 /*
2  * ============LICENSE_START==========================================
3  * ONAP Portal
4  * ===================================================================
5  * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
6  * ===================================================================
7  * Modifications Copyright (c) 2019 Samsung
8  * ===================================================================
9  *
10  * Unless otherwise specified, all software contained herein is licensed
11  * under the Apache License, Version 2.0 (the "License");
12  * you may not use this software except in compliance with the License.
13  * You may obtain a copy of the License at
14  *
15  *             http://www.apache.org/licenses/LICENSE-2.0
16  *
17  * Unless required by applicable law or agreed to in writing, software
18  * distributed under the License is distributed on an "AS IS" BASIS,
19  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20  * See the License for the specific language governing permissions and
21  * limitations under the License.
22  *
23  * Unless otherwise specified, all documentation contained herein is licensed
24  * under the Creative Commons License, Attribution 4.0 Intl. (the "License");
25  * you may not use this documentation except in compliance with the License.
26  * You may obtain a copy of the License at
27  *
28  *             https://creativecommons.org/licenses/by/4.0/
29  *
30  * Unless required by applicable law or agreed to in writing, documentation
31  * distributed under the License is distributed on an "AS IS" BASIS,
32  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
33  * See the License for the specific language governing permissions and
34  * limitations under the License.
35  *
36  * ============LICENSE_END============================================
37  *
38  *
39  */
40
41 package org.onap.portal.service.fn;
42
43 import java.util.ArrayList;
44 import java.util.List;
45 import java.util.Optional;
46 import java.util.TreeSet;
47 import javax.persistence.EntityExistsException;
48 import javax.persistence.EntityManager;
49 import javax.persistence.Query;
50 import org.onap.portal.dao.fn.FnAppDao;
51 import org.onap.portal.domain.db.fn.FnApp;
52 import org.onap.portal.domain.dto.transport.OnboardingApp;
53 import org.onap.portal.utils.EPCommonSystemProperties;
54 import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate;
55 import org.onap.portalsdk.core.onboarding.util.CipherUtil;
56 import org.onap.portalsdk.core.util.SystemProperties;
57 import org.springframework.beans.factory.annotation.Autowired;
58 import org.springframework.stereotype.Service;
59
60 @Service
61 public class FnAppService {
62
63   private static final String SUPER_ADMIN_ROLE_ID = "1";
64
65   private EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(FnAppService.class);
66
67   private final FnAppDao fnAppDao;
68   private final EntityManager entityManager;
69
70   @Autowired
71   public FnAppService(final FnAppDao fnAppDao, EntityManager entityManager) {
72     this.fnAppDao = fnAppDao;
73     this.entityManager = entityManager;
74   }
75
76   public List<FnApp> getAppsFullList() {
77     return fnAppDao.findAll();
78   }
79
80   public FnApp getById(final Long id) {
81     return Optional.of(fnAppDao.getOne(id)).orElseThrow(EntityExistsException::new);
82   }
83
84   public void createOnboardingFromApp(FnApp app, OnboardingApp onboardingApp) {
85     onboardingApp.setId(app.getId());
86     onboardingApp.setName(app.getAppName());
87     onboardingApp.setImageUrl(app.getAppImageUrl());
88     onboardingApp.setDescription(app.getAppDescription());
89     onboardingApp.setNotes(app.getAppNotes());
90     onboardingApp.setUrl(app.getAppUrl());
91     onboardingApp.setAlternateUrl(app.getAppAlternateUrl());
92     onboardingApp.setRestUrl(app.getAppRestEndpoint());
93     onboardingApp.setIsOpen(app.getOpen());
94     onboardingApp.setIsEnabled(app.getEnabled());
95     onboardingApp.setUsername(app.getAppUsername());
96     onboardingApp.setAppPassword((app.getAppPassword().equals(EPCommonSystemProperties.APP_DISPLAY_PASSWORD))
97         ? EPCommonSystemProperties.APP_DISPLAY_PASSWORD : decryptedPassword(app.getAppPassword(), app));
98     onboardingApp.setUebTopicName(app.getUebTopicName());
99     onboardingApp.setUebKey(app.getUebKey());
100     onboardingApp.setUebSecret(app.getUebSecret());
101     onboardingApp.setIsCentralAuth(app.getAuthCentral());
102     onboardingApp.setNameSpace(app.getAuthNamespace());
103     onboardingApp.setRestrictedApp(app.isRestrictedApp());
104   }
105
106   private String decryptedPassword(String encryptedAppPwd, FnApp app) {
107     String result = "";
108     if (encryptedAppPwd != null && !encryptedAppPwd.isEmpty()) {
109       try {
110         result = CipherUtil.decryptPKC(encryptedAppPwd,
111             SystemProperties.getProperty(SystemProperties.Decryption_Key));
112       } catch (Exception e) {
113         logger.error(EELFLoggerDelegate.errorLogger,
114             "decryptedPassword failed for app " + app.getAppName(), e);
115       }
116     }
117     return result;
118   }
119
120   List<FnApp> getUserRemoteApps(String id) {
121 /*    StringBuilder sb = new StringBuilder();
122     sb.append("SELECT * FROM FnApp join FN_USER_ROLE ON FN_USER_ROLE.APP_ID = FN_APP.APP_ID where ");
123     sb.append("FN_USER_ROLE.USER_ID = ").append(id).append(" AND FN_USER_ROLE.ROLE_ID != ")
124         .append(SUPER_ADMIN_ROLE_ID);
125     sb.append(" AND FN_APP.ENABLED = 'Y'");
126
127     Query query = entityManager.createQuery(sb.toString());
128     List<FnApp> adminApps = query.getResultList();*/
129     return new ArrayList<>();
130   }
131 }