[ https://issues.apache.org/jira/browse/AIRAVATA-2712?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16412084#comment-16412084 ]
ASF GitHub Bot commented on AIRAVATA-2712: ------------------------------------------ machristie commented on a change in pull request #185: [AIRAVATA-2712] Refactoring App Catalog Implementation - UserResourceProfile URL: https://github.com/apache/airavata/pull/185#discussion_r176863015 ########## File path: modules/registry-refactoring/src/main/java/org/apache/airavata/registry/core/repositories/appcatalog/UserResourceProfileRepository.java ########## @@ -0,0 +1,227 @@ +/** + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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. + */ +package org.apache.airavata.registry.core.repositories.appcatalog; + +import org.apache.airavata.model.appcatalog.userresourceprofile.UserComputeResourcePreference; +import org.apache.airavata.model.appcatalog.userresourceprofile.UserResourceProfile; +import org.apache.airavata.model.appcatalog.userresourceprofile.UserStoragePreference; +import org.apache.airavata.registry.core.entities.appcatalog.UserComputeResourcePreferencePK; +import org.apache.airavata.registry.core.entities.appcatalog.UserResourceProfileEntity; +import org.apache.airavata.registry.core.entities.appcatalog.UserResourceProfilePK; +import org.apache.airavata.registry.core.entities.appcatalog.UserStoragePreferencePK; +import org.apache.airavata.registry.core.utils.DBConstants; +import org.apache.airavata.registry.core.utils.ObjectMapperSingleton; +import org.apache.airavata.registry.core.utils.QueryConstants; +import org.apache.airavata.registry.cpi.AppCatalogException; +import org.apache.airavata.registry.cpi.CompositeIdentifier; +import org.apache.airavata.registry.cpi.UsrResourceProfile; +import org.dozer.Mapper; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.sql.Timestamp; +import java.util.*; + +public class UserResourceProfileRepository extends AppCatAbstractRepository<UserResourceProfile, UserResourceProfileEntity, UserResourceProfilePK> implements UsrResourceProfile { + private final static Logger logger = LoggerFactory.getLogger(UserResourceProfileRepository.class); + + public UserResourceProfileRepository() { + super(UserResourceProfile.class, UserResourceProfileEntity.class); + } + + protected String saveUserResourceProfileData(UserResourceProfile userResourceProfile) throws AppCatalogException { + UserResourceProfileEntity userResourceProfileEntity = saveUserResourceProfile(userResourceProfile); + return userResourceProfileEntity.getUserId(); + } + + protected UserResourceProfileEntity saveUserResourceProfile(UserResourceProfile userResourceProfile) throws AppCatalogException { + String userId = userResourceProfile.getUserId(); + String gatewayId = userResourceProfile.getGatewayID(); + Mapper mapper = ObjectMapperSingleton.getInstance(); + UserResourceProfileEntity userResourceProfileEntity = mapper.map(userResourceProfile, UserResourceProfileEntity.class); + + if (userResourceProfileEntity.getUserComputeResourcePreferences() != null) { + logger.debug("Populating the Primary Key UserComputeResourcePreferences objects for the User Resource Profile"); + userResourceProfileEntity.getUserComputeResourcePreferences().forEach(userComputeResourcePreferenceEntity -> userComputeResourcePreferenceEntity.setUserId(userId)); + userResourceProfileEntity.getUserComputeResourcePreferences().forEach(userComputeResourcePreferenceEntity -> userComputeResourcePreferenceEntity.setGatewayId(gatewayId)); + } + + if (userResourceProfileEntity.getUserStoragePreferences() != null) { + logger.debug("Populating the Primary Key UserStoragePreferences objects for the User Resource Profile"); + userResourceProfileEntity.getUserStoragePreferences().forEach(userStoragePreferenceEntity -> userStoragePreferenceEntity.setUserId(userId)); + userResourceProfileEntity.getUserStoragePreferences().forEach(userStoragePreferenceEntity -> userStoragePreferenceEntity.setGatewayId(gatewayId)); + } + + if (!isUserResourceProfileExists(userId, gatewayId)) { + logger.debug("Checking if the User Resource Profile already exists"); + userResourceProfileEntity.setCreationTime(new Timestamp(System.currentTimeMillis())); + } + + userResourceProfileEntity.setUpdateTime(new Timestamp(System.currentTimeMillis())); + return execute(entityManager -> entityManager.merge(userResourceProfileEntity)); + } + + @Override + public String addUserResourceProfile(UserResourceProfile userResourceProfile) throws AppCatalogException { + return saveUserResourceProfileData(userResourceProfile); + } + + @Override + public void updateUserResourceProfile(String userId, String gatewayId, UserResourceProfile updatedProfile) throws AppCatalogException { + saveUserResourceProfileData(updatedProfile); + } + + @Override + public UserResourceProfile getUserResourceProfile(String userId, String gatewayId) throws AppCatalogException { + Map<String, Object> queryParameters = new HashMap<>(); + queryParameters.put(DBConstants.UserResourceProfile.USER_ID, userId); + queryParameters.put(DBConstants.UserResourceProfile.GATEWAY_ID, gatewayId); + List<UserResourceProfile> userResourceProfileList = select(QueryConstants.GET_USER_RESOURCE_PROFILE, -1, 0, queryParameters); + + if(!userResourceProfileList.isEmpty() && userResourceProfileList != null) { + logger.debug("Return the record (there is only one record)"); + return userResourceProfileList.get(0); + } + + return null; + } + + @Override + public UserComputeResourcePreference getUserComputeResourcePreference(String userId, String gatewayId, String hostId) throws AppCatalogException { + UserComputeResourcePreferenceRepository userComputeResourcePreferenceRepository = new UserComputeResourcePreferenceRepository(); Review comment: Same comment here, should be able to do a `userComputeResourcePreferenceRepository.get()`. ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: us...@infra.apache.org > Refactoring App Catalog Implementation - User Resource Profile > -------------------------------------------------------------- > > Key: AIRAVATA-2712 > URL: https://issues.apache.org/jira/browse/AIRAVATA-2712 > Project: Airavata > Issue Type: Improvement > Components: Registry API > Reporter: Sneha Tilak > Assignee: Sneha Tilak > Priority: Major > -- This message was sent by Atlassian JIRA (v7.6.3#76005)