Developing Java-Based Apps on SAP Cloud Platform Week 2
JPA Model
Check Out JPA Branch
修改web.xml
Path: src/main/webapp/WEB-INF/web.xml, Project: espm-cloud-web,加入代码
<resource-ref>
<res-ref-name>jdbc/DefaultDB</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
</resource-ref>
修改JpaEntityManagerFactory.java
Path: src/main/java/>com.sap.espm.model.web>
JpaEntityManagerFactory.java, Project: espm-cloud-web加入代码
public static synchronized EntityManagerFactory getEntityManagerFactory() throws NamingException, SQLException
{
if (entityManagerFactory == null)
{
final InitialContext ctx = new InitialContext();
final DataSource ds = (DataSource) ctx.lookup(DATA_SOURCE_NAME);
final Map<String, Object> properties = new HashMap<String, Object>();
properties.put(PersistenceUnitProperties.NON_JTA_DATASOURCE, ds);
entityManagerFactory = Persistence.createEntityManagerFactory(PERSISTENCE_UNIT_NAME, properties);
Utility.setEntityManagerFactory(entityManagerFactory);
}
return entityManagerFactory;
}
修改SalesOrderHeader.java
path: Java
src/main/java>com.sap.espm.model>SalesOrderHeader.java , Project : espm-cloud-jpa, 加入code
@Entity
@Table(name = "ESPM_SALES_ORDER_HEADER")
末尾加入code (如果有error, Date 类型需要导入)
@PrePersist
private void persist()
{
final Calendar cal = Calendar.getInstance();
final Date date = new Date();
cal.setTime(date);
this.lifeCycleStatus = "N";
this.lifeCycleStatusName = "New";
int itemNumber = 10;
this.netAmount = new BigDecimal("0.00");
this.taxAmount = new BigDecimal("0.00");
this.grossAmount = new BigDecimal("0.00");
this.createdAt = cal;
for (final SalesOrderItem item : salesOrderItems)
{
item.setSalesOrderId(this.getSalesOrderId());
item.setItemNumber(itemNumber);
itemNumber += 10;
item.persist();
this.netAmount = this.netAmount.add(item.getNetAmount()).setScale(3);
this.taxAmount = this.taxAmount.add(item.getTaxAmount()).setScale(3);
this.grossAmount = this.grossAmount.add(item.getGrossAmount()).setScale(3);
}
}
修改persistence.xml
加入code
<class>com.sap.espm.model.SalesOrderHeader</class>
OData Services
Check Out OData Branch
Check Out Commit, 然后Reset
修改EspmServiceFactory
package: com.sap.espm.model.web, Project: espm-cloud-web,加入code
final ODataJPAContext oDataJPAContext = this.getODataJPAContext();
EntityManagerFactory emf;
try
{
emf = JpaEntityManagerFactory.getEntityManagerFactory();
oDataJPAContext.setEntityManagerFactory(emf);
oDataJPAContext.setPersistenceUnitName(PERSISTENCE_UNIT_NAME);
oDataJPAContext.setJPAEdmExtension(new EspmProcessingExtension());
oDataJPAContext.setJPAEdmMappingModel("EspmEdmMapping.xml");
return oDataJPAContext;
}
catch (final Exception e)
{
throw new ODataRuntimeException(e);
}
修改CustomerProcessor.java
package: com.sap.espm.model.function.impl, Project:
espm-cloud-web
@EdmFunctionImport(name = "GetCustomerByEmailAddress", entitySet = "Customers", returnType = @ReturnType(type = Type.ENTITY, isCollection = true))
public List<Customer> getCustomerByEmailAddress(@EdmFunctionImportParameter(name = "EmailAddress") final String emailAddress)
throws ODataException
修改EspmProcessingExtension.java
package: com.sap.espm.model.web, Project:
espm-cloud-web
view.registerOperations(CustomerProcessor.class, null);
SAP UI5
Check Out UI5 Branch
修改manifest.json
path: src > main > webapp > webshop , Project: espm-cloud-web
"dataSources": {
"espmDataModel": {
"uri": "/espm-cloud-web/espm.svc",
"type": "OData",
"settings": {
"odataVersion": "2.0"
}
}
},
修改index.html
path: src > main > webapp > webshop, Project: espm-cloud-web
sap.ui.getCore().attachInit(function() {
new sap.m.Shell({
app : new sap.ui.core.ComponentContainer({
height : "100%",
name : "com.sap.espm.shop"
})
}).placeAt("content");
});
修改Component.js
path: src > main > webapp > webshop, Project: espm-cloud-web
// set the device model, odata model and json model
this.setModel(models.createDeviceModel(), "device");
var sServiceUrl = this.getMetadata().getManifestEntry(
"sap.app").dataSources.espmDataModel.uri;
var oEspmModel = new sap.ui.model.odata.ODataModel(
sServiceUrl, {
json : true,
loadMetadataAsync : true
});
this.setModel(oEspmModel, "EspmModel");
var oData = {
ShoppingCart : []
};
var oModel = new sap.ui.model.json.JSONModel(oData);
this.setModel(oModel, "Cart");