Skip to content

Commit

Permalink
updated to sdk version 17.5
Browse files Browse the repository at this point in the history
  • Loading branch information
nagesh-dixit committed Feb 6, 2024
1 parent 2f05389 commit b9f0d23
Show file tree
Hide file tree
Showing 8 changed files with 98 additions and 37 deletions.
8 changes: 3 additions & 5 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ plugins {

id 'idea'

id 'maven'
id 'maven-publish'

id 'java'
Expand All @@ -26,10 +25,9 @@ ext {
]
}

allprojects {
apply plugin: 'java'
sourceCompatibility = 1.8
targetCompatibility = 1.8
java {
sourceCompatibility = JavaVersion.VERSION_1_8
sourceCompatibility = JavaVersion.VERSION_1_8
}


Expand Down
47 changes: 38 additions & 9 deletions src/main/java/handson/Task04a_STATEMACHINE.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,42 @@ public static void main(String[] args) throws IOException, ExecutionException, I

final String apiClientPrefix = ApiPrefixHelper.API_DEV_CLIENT_PREFIX.getPrefix();

Logger logger = LoggerFactory.getLogger(Task04a_STATEMACHINE.class.getName());
Logger logger = LoggerFactory.getLogger("commercetools");

final ProjectApiRoot client = createApiClient(apiClientPrefix);
final StateMachineService stateMachineService = new StateMachineService(client);

// TODO Use StateMachineService.java to create your designed order state machine
//

stateMachineService.createState("mh1OrderPacked", StateTypeEnum.ORDER_STATE, true, "MH1 Order Packed")
.thenCombineAsync(stateMachineService.createState("mh1OrderShipped", StateTypeEnum.ORDER_STATE, false, "MH1 Order Shipped"),
(orderPackedStateApiResponse, orderShippedStateApiResponse)->
stateMachineService.createState(
"mhOrderPacked1",
StateTypeEnum.ORDER_STATE,
true,
"MH Order Packed1"
)
.exceptionally( throwable -> {
logger.error("Exception: " + throwable.getMessage());
try {
return stateMachineService.getStateByKey("mhOrderPacked1").get();
}
catch (Exception e){client.close();logger.error(e.getMessage());return null;}
})
.thenCombineAsync(
stateMachineService.createState(
"mhOrderShipped1",
StateTypeEnum.ORDER_STATE,
false,
"MH Order Shipped1"
)
.exceptionally( throwable -> {
logger.error("Exception: " + throwable.getMessage());
try {
return stateMachineService.getStateByKey("mhOrderShipped1").get();
}
catch (Exception e){client.close();logger.error(e.getMessage());return null;}
}),
(orderPackedStateApiResponse, orderShippedStateApiResponse) ->
stateMachineService.setStateTransitions(
orderPackedStateApiResponse.getBody(),
Stream.of(
Expand All @@ -54,11 +80,14 @@ public static void main(String[] args) throws IOException, ExecutionException, I
)
.get()
.thenApply(ApiHttpResponse::getBody)
.thenAccept(resource -> logger.info("State info {}",resource.getId()))
.exceptionally(exception -> {
logger.info("An error occured " + exception.getMessage());
return null;}
)
.handle((state, exception) -> {
if (exception == null) {
logger.info("Initial state key {}", state.getKey());
return state;
};
logger.error("Exception: " + exception.getMessage());
return null;
})
.thenRun(() -> client.close());

// TODO Create an order in the Merchant Center and verify that custom workflow states are available
Expand Down
14 changes: 9 additions & 5 deletions src/main/java/handson/Task07b_CUSTOMOBJECTS.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,15 @@ public static void main(String[] args) throws IOException, ExecutionException, I
.container("plants-compatibility-info")
.key("tulip-seed-product")
.value(tulipObject)
)
.execute()
).execute()
.thenApply(ApiHttpResponse::getBody)
.thenAccept(resource -> logger.info("Resource ID: " + resource.getId()))
.exceptionally(exception -> { logger.info("An error occured " + exception.getMessage()); return null;})
.thenRun(() -> client.close());
.handle((customObject, exception) -> {
if (exception == null) {
logger.info("Custom Object ID: " + customObject.getId());
return customObject;
}
logger.error("Exception: " + exception.getMessage());
return null;
}).thenRun(() -> client.close());
}
}
14 changes: 9 additions & 5 deletions src/main/java/handson/Task07c_APIEXTENSION.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,16 @@ public static void main(String[] args) throws IOException, ExecutionException, I
)
.build()
)
)
.execute()
).execute()
.thenApply(ApiHttpResponse::getBody)
.thenAccept(resource -> logger.info("Resource ID: " + resource.getId()))
.exceptionally(exception -> { logger.info("An error occured " + exception.getMessage()); return null;})
.thenRun(() -> client.close());
.handle((extension, exception) -> {
if (exception == null) {
logger.info("API Extension ID: " + extension.getId());
return extension;
}
logger.error("Exception: " + exception.getMessage());
return null;
}).thenRun(() -> client.close());
}
}

17 changes: 9 additions & 8 deletions src/main/java/handson/Task08a_SUBSCRIPTION.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,14 +56,15 @@ public static void main(String[] args) throws IOException, ExecutionException, I
.build()
)

)
.execute()
).execute()
.thenApply(ApiHttpResponse::getBody)
.thenAccept(resource -> logger.info("Resource ID: " + resource.getId()))
.exceptionally(exception -> {
logger.info("An error occured " + exception.getMessage());
return null;}
)
.thenRun(() -> client.close());
.handle((subscription, exception) -> {
if (exception == null) {
logger.info("Subscription ID: " + subscription.getId());
return subscription;
}
logger.error("Exception: " + exception.getMessage());
return null;
}).thenRun(() -> client.close());
}
}
8 changes: 4 additions & 4 deletions src/main/java/handson/impl/ApiPrefixHelper.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package handson.impl;

public enum ApiPrefixHelper {
API_DEV_CLIENT_PREFIX("DEV_PREFIX"),
API_DEV_CLIENT_PREFIX("ctp."),
API_TEST_CLIENT_PREFIX("TEST_PREFIX"),
API_DEV_IMPORT_PREFIX("DEV_IMPORT_PREFIX"),
API_STORE_CLIENT_PREFIX("DEV_STORE_PREFIX"),
API_ME_CLIENT_PREFIX("DEV_ME_PREFIX"),
API_DEV_IMPORT_PREFIX("ctp-import."),
API_STORE_CLIENT_PREFIX("ctp-boston."),
API_ME_CLIENT_PREFIX("ctp-me."),
API_STORE_ME_CLIENT_PREFIX("DEV_STORE_ME_PREFIX");

private final String prefix;
Expand Down
18 changes: 17 additions & 1 deletion src/main/java/handson/impl/ClientService.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,23 @@ public class ClientService {
*/
public static ProjectApiRoot createApiClient(final String prefix) throws IOException {

projectApiRoot = null;
final Properties prop = new Properties();
prop.load(ClientService.class.getResourceAsStream("/dev.properties"));
String clientId = prop.getProperty(prefix + "clientId");
String clientSecret = prop.getProperty(prefix + "clientSecret");
String projectKey = prop.getProperty(prefix + "projectKey");

projectApiRoot = ApiRootBuilder.of()
.defaultClient(
ClientCredentials.of()
.withClientId(clientId)
.withClientSecret(clientSecret)
.build(),
ServiceRegion.GCP_EUROPE_WEST1.getOAuthTokenUrl(),
ServiceRegion.GCP_EUROPE_WEST1.getApiUrl()
)
.build(projectKey);

return projectApiRoot;
}

Expand Down
9 changes: 9 additions & 0 deletions src/main/java/handson/impl/StateMachineService.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,15 @@ public StateMachineService(final ProjectApiRoot client) {
this.apiRoot = client;
}

public CompletableFuture<ApiHttpResponse<State>> getStateByKey(final String key) {

return
apiRoot
.states()
.withKey(key)
.get()
.execute();
}
public CompletableFuture<ApiHttpResponse<State>> createState(final String key, StateTypeEnum stateTypeEnum, final Boolean initial, final String name) {

Map<String, String> myNames = new HashMap<String, String>() {
Expand Down

0 comments on commit b9f0d23

Please sign in to comment.