欢迎浏览sook云!

网站地图

当前位置:主页 > 云存储 > 产品服务 >

天翼云_网站正在建设_好用

时间:2021-11-24 08:17

人气:

作者:sook云

标签: 正在  建设  好用  网站  天翼 

导读:This blog post is part 2 of a series of blogs on how to develop full-stack To-Do application in SAP Web IDE Full-Stack. Prerequisites Before reading this blog please make sure that you have read, understood and implemented part 0 and p...

天翼云_网站正在建设_好用

This blog post is part 2 of a series of blogs on how to develop full-stack To-Do application in SAP Web IDE Full-Stack.

Prerequisites

Before reading this blog please make sure that you have read, understood and implemented part 0 and part 1 of this series.

Create your Java Module

The Java module is responsible for:

In our app we will use it for the OData provisioning.

To create the Java module, follow these steps:

In SAP Web IDE, select the todo project folder Right-click it and select New -> Java Module Select OData V4 Service Using SAP Cloud Platform SDK template and then click on Next. Enter service as the module name and click Next. Enter Group ID, artifact ID and package or just use the default values. Make sure that Enable SAP HANA Database support checkbox is checked! It’s needed to allow connectivity from the Java service to the HANA database module. Finally, click Finish. SAP Web IDE creates a new service folder under your todo project folder. In addition, the mta.yaml file is updated and now contains the service java module in addition to the db module. Specifically notice the ‘provides’ and ‘requires’ sections in the mta.yaml file. The db and java modules both require HANA and provides a service_api which will be used by the UI module (that we will create in part 3 of this series). Next, select the service folder which is located under service > src > main > java > {your_package_path} > service Right-click it and select New > Java Class to create a new Java class Enter ToDoService in the Name field, click Next and Finish SAP Web IDE creates the ToDoService.java file in the service folder Open the ToDoService.java file in the code editor Copy the code below and paste it under the first line in the file (under the package {your_package_name} ) import java.sql.Connection; import java.util.List; import javax.naming.Context; import javax.naming.InitialContext; import javax.sql.DataSource; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import com.sap.cloud.sdk.hana.connectivity.cds.CDSQuery; import com.sap.cloud.sdk.hana.connectivity.cds.CDSSelectQueryBuilder; import com.sap.cloud.sdk.hana.connectivity.cds.CDSSelectQueryResult; import com.sap.cloud.sdk.hana.connectivity.handler.CDSDataSourceHandler; import com.sap.cloud.sdk.hana.connectivity.handler.DataSourceHandlerFactory; import com.sap.cloud.sdk.service.prov.api.EntityData; import com.sap.cloud.sdk.service.prov.api.operations.Query; import com.sap.cloud.sdk.service.prov.api.operations.Read; import com.sap.cloud.sdk.service.prov.api.request.QueryRequest; import com.sap.cloud.sdk.service.prov.api.request.ReadRequest; import com.sap.cloud.sdk.service.prov.api.response.QueryResponse; import com.sap.cloud.sdk.service.prov.api.response.ReadResponse; /** * * @author I059508 */ public class ToDoService { private static Logger logger = LoggerFactory.getLogger(ToDoService.class); @Query(entity = "Task", serviceName = "todo") public QueryResponse findTasks(QueryRequest request) { try { QueryResponse res = QueryResponse.setSuccess().setEntityData(getEntitySet(request)).response(); return res; } catch (Exception e) { return null; } } @Read(entity = "Task", serviceName = "todo") public ReadResponse getProposedBooks(ReadRequest readRequest){ try { ReadResponse readResponse = ReadResponse.setSuccess().setData(readEntity(readRequest)).response(); return readResponse; } catch (Exception e) { return null; } } private List getEntitySet(QueryRequest queryRequest) { String fullQualifiedName = queryRequest.getEntityMetadata().getNamespace() + "." + queryRequest.getEntityMetadata().getName(); CDSDataSourceHandler dsHandler = DataSourceHandlerFactory.getInstance().getCDSHandler(getConnection(), queryRequest.getEntityMetadata().getNamespace()); try { CDSQuery cdsQuery = new CDSSelectQueryBuilder(fullQualifiedName).build(); CDSSelectQueryResult cdsSelectQueryResult = dsHandler.executeQuery(cdsQuery); return cdsSelectQueryResult.getResult(); } catch (Exception e) { logger.error("==> Eexception while fetching query data from CDS: " + e.getMessage()); e.printStackTrace(); } return null; } private EntityData readEntity(ReadRequest readRequest) throws Exception { CDSDataSourceHandler dsHandler = DataSourceHandlerFactory.getInstance().getCDSHandler(getConnection(), readRequest.getEntityMetadata().getNamespace()); EntityData ed = dsHandler.executeRead(readRequest.getEntityMetadata().getName(), readRequest.getKeys(), readRequest.getEntityMetadata().getElementNames()); return ed; } private static Connection getConnection(){ Connection conn = null; Context ctx; try { ctx = new InitialContext(); conn = ((DataSource) ctx.lookup("java:comp/env/jdbc/java-hdi-container")).getConnection(); System.out.println("conn = " + conn); } catch (Exception e) { e.printStackTrace(); } return conn; } }​   The java code that you just added is doing the following: Connects to the SAP HANA database – see the getConnection() method Uses the @Read and @Query annotations to expose a task and a list of tasks in OData. This is done using the SAP Cloud Java SDK which is added automatically by SAP Web IDE as a Maven dependency in the module (to understand more, review the pom.xml). Notice: This java code handles only the Task entity’s ‘query’ and ‘read’ operations. Handling the SubTask entity or any other CUD (create, update, delete) operations can be done in the same way (using the same SDK with similar code).

温馨提示:以上内容整理于网络,仅供参考,如果对您有帮助,留下您的阅读感言吧!
相关阅读
本类排行
相关标签
本类推荐

关于我们 | 版权声明 | 广告服务 | 友情链接 | 联系我们 | 网站地图

Copyright © 2002-2020 sook云 版权所有 备案号:豫ICP备36548666号
本站资料均来源互联网收集整理,作品版权归作者所有,如果侵犯了您的版权,请跟我们联系。