Technology Blog Posts by SAP
cancel
Showing results for 
Search instead for 
Did you mean: 
gopalanand
Product and Topic Expert
Product and Topic Expert
1,507

Great news for Java developers!  The SAP BTP Developer's Guide now features hands-on tutorials for CAP Java-based incident management.

With the latest update, you’ll learn how to build, deploy, and optimize incident management apps using CAP Java—following SAP BTP best practices. Time to level up your skills and ship better apps!

Tutorials : Develop a Full-Stack CAP Application Following SAP BTP Developer’s Guide 
Github: Incident Management Java

Following the tutorials you will learn the following:

  • How to create a CAP project
  • How to add a domain model
  • How to create services
  • How to add data to your database

Domain Model of the application: 

using { cuid, managed, sap.common.CodeList } from '@sap/cds/common';
namespace sap.capire.incidents; 

/**
* Incidents created by Customers.
*/
entity Incidents : cuid, managed {  
customer     : Association to Customers;
title        : String  @title : 'Title';
urgency        : Association to Urgency default 'M';
status         : Association to Status default 'N';
conversation  : Composition of many {
    key ID    : UUID;
    timestamp : type of managed:createdAt;
    author    : type of managed:createdBy;
    message   : String;
};
}

/**
* Customers entitled to create support Incidents.
*/
entity Customers : managed { 
key ID        : String;
firstName     : String;
lastName      : String;
name          : String = firstName ||' '|| lastName;
email         : EMailAddress;
phone         : PhoneNumber;
incidents     : Association to many Incidents on incidents.customer = $self;
creditCardNo  : String(16) @assert.format: '^[1-9]\d{15}$';
addresses     : Composition of many Addresses on addresses.customer = $self;
}

entity Addresses : cuid, managed {
customer      : Association to Customers;
city          : String;
postCode      : String;
streetAddress : String;
}

entity Status : CodeList {
key code: String enum {
    new = 'N';
    assigned = 'A'; 
    in_process = 'I'; 
    on_hold = 'H'; 
    resolved = 'R'; 
    closed = 'C'; 
};
criticality : Integer;
}

entity Urgency : CodeList {
key code: String enum {
    high = 'H';
    medium = 'M'; 
    low = 'L'; 
};
}

type EMailAddress : String;
type PhoneNumber : String;

Here's some code snippet from the JAVA custom handler: 

package customer.incident_management.handler;

import cds.gen.processorservice.Incidents;
import cds.gen.processorservice.ProcessorService_;
import cds.gen.sap.capire.incidents.*;
import com.sap.cds.ql.Select;
import com.sap.cds.services.ErrorStatuses;
import com.sap.cds.services.ServiceException;
import com.sap.cds.services.cds.CqnService;
import com.sap.cds.services.handler.EventHandler;
import com.sap.cds.services.handler.annotations.Before;
import com.sap.cds.services.handler.annotations.ServiceName;
import com.sap.cds.services.persistence.PersistenceService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import java.util.List;
import java.util.Locale;

@Component
@ServiceName(ProcessorService_.CDS_NAME)
public class ProcessorServiceHandler implements EventHandler {

    private static final Logger logger = LoggerFactory.getLogger(ProcessorServiceHandler.class);

    private final PersistenceService db;

    public ProcessorServiceHandler(PersistenceService db) {
        this.db = db;
    }

    /*
     * Change the urgency of an incident to "high" if the title contains the word "urgent"
     */
    @Before(event = CqnService.EVENT_CREATE)
    public void ensureHighUrgencyForIncidentsWithUrgentInTitle(List<Incidents> incidents) {
        for (Incidents incident : incidents) {
            if (incident.getTitle().toLowerCase(Locale.ENGLISH).contains("urgent") &&
                    incident.getUrgencyCode() == null || !incident.getUrgencyCode().equals("H")) {
                incident.setUrgencyCode("H");
                logger.info("Adjusted Urgency for incident '{}' to 'HIGH'.", incident.getTitle());
            }

        }
    }

    /*
     * Handler to avoid updating a "closed" incident
     */
    @Before(event = CqnService.EVENT_UPDATE)
    public void ensureNoUpdateOnClosedIncidents(Incidents incident) {
        Incidents in = db.run(Select.from(Incidents_.class).where(i -> i.ID().eq(incident.getId()))).single(Incidents.class);
        if (in.getStatusCode().equals("C")) {
            throw new ServiceException(ErrorStatuses.CONFLICT, "Can't modify a closed incident");
        }

    }

}

This is your cue to clone the repo or dive into the tutorial and discover the elegance of CAP Java!

Once your application is ready, level up your development workflow by learning how to set up a robust CI/CD pipeline. This will help you automate testing, deployment, and delivery, ensuring smooth and efficient software releases!

The next part: Set Up a CI/CD Pipeline for SAP BTP, Cloud Foundry Runtime . 

Keep Learning!

Java  SAP Cloud Application Programming Model  SAP Business Application Studio