Skip to content

Instantly share code, notes, and snippets.

@GeorgDangl
Last active April 18, 2019 10:18
Show Gist options
  • Save GeorgDangl/63fe8f9e687366a20b542125451870db to your computer and use it in GitHub Desktop.
Save GeorgDangl/63fe8f9e687366a20b542125451870db to your computer and use it in GitHub Desktop.
AVACloud Java Examples - www.dangl-it.com
private static void executeAvaCloudExample(String[] args) {
String clientId = args[0];
String clientSecret = args[1];
String gaebFilePath = args[2];
String bearerToken;
try {
bearerToken = DanglIdentityUtils.getBearerToken(clientId, clientSecret);
} catch (IOException e) {
System.out.println("IO Exception while obtaining access token:");
System.out.println(e.toString());
return;
}
// Configure OAuth2 access token for Dangl.Identity authorization
ApiClient defaultClient = Configuration.getDefaultApiClient();
OAuth danglIdentity = (OAuth) defaultClient.getAuthentication("Dangl.Identity");
danglIdentity.setAccessToken(bearerToken);
File gaebInputFile = new File(gaebFilePath);
try {
transformGaebToExcel(gaebInputFile);
printProjectTotalPriceAndPositionCount(gaebInputFile);
} catch (ApiException e) {
System.err.println("Exception in AVACloud example");
e.printStackTrace();
}
}
private static BigDecimal getProjectTotalPrice(ProjectDto project) {
return project
.getServiceSpecifications()
.get(0)
.getTotalPrice();
}
private static Integer getProjectPositionCount(ProjectDto project) {
ServiceSpecificationDto servSpec = project
.getServiceSpecifications()
.get(0);
Integer positionsCount = getPositionsInElementList(servSpec.getElements());
return positionsCount;
}
private static Integer getPositionsInElementList(List<IElementDto> elements) {
Integer positionsCount = 0;
for(IElementDto element: elements) {
if (element instanceof PositionDto) {
positionsCount++;
} else if (element instanceof ServiceSpecificationGroupDto) {
positionsCount += getPositionsInElementList(((ServiceSpecificationGroupDto)element).getElements());
}
}
return positionsCount;
}
private static void printProjectTotalPriceAndPositionCount(File gaebFile) throws ApiException {
GaebConversionApi gaebConversionApi = new GaebConversionApi();
ProjectDto project = gaebConversionApi.gaebConversionConvertToAva(gaebFile, true, true);
BigDecimal totalPrice = getProjectTotalPrice(project);
System.out.println("Project total price (net): " + totalPrice);
Integer countOfPositions = getProjectPositionCount(project);
System.out.println("Count of positions: " + countOfPositions);
}
public static String getBearerToken(String clientId, String clientSecret) throws IOException {
String basicCredentials = Credentials.basic(clientId, clientSecret);
String requestBodyContent = "grant_type=client_credentials&scope=avacloud";
RequestBody requestBody = RequestBody.create(MediaType.parse("application/x-www-form-urlencoded"), requestBodyContent);
Request request = new Request
.Builder()
.header("Authorization", basicCredentials)
.url("https://identity.dangl-it.com/connect/token")
.post(requestBody)
.build();
OkHttpClient httpClient = new OkHttpClient();
Response response = httpClient.newCall(request).execute();
String responseBody = response.body().string();
JsonObject jsonResponse = new Gson().fromJson(responseBody, JsonObject.class);
String accessToken = jsonResponse.get("access_token").getAsString();
return accessToken;
}
private static void transformGaebToExcel(File gaebFile) throws ApiException {
GaebConversionApi gaebConversionApi = new GaebConversionApi();
File excelConversionResult = gaebConversionApi.gaebConversionConvertToExcel(gaebFile, true, true, "de");
String excelResultFilePath = gaebFile.getAbsolutePath() + ".xlsx";
System.out.println("Saving Excel conversion result to:");
System.out.println(excelResultFilePath);
try {
Files.copy(excelConversionResult.toPath(), Paths.get(excelResultFilePath).toAbsolutePath(), StandardCopyOption.REPLACE_EXISTING);
} catch (IOException e) {
System.out.println("IO Exception while saving Excel file:");
System.out.println(e.toString());
}
}
<dependency>
<groupId>io.swagger</groupId>
<artifactId>swagger-annotations</artifactId>
<version>1.5.15</version>
</dependency>
<dependency>
<groupId>com.squareup.okhttp</groupId>
<artifactId>okhttp</artifactId>
<version>2.7.5</version>
</dependency>
<dependency>
<groupId>com.squareup.okhttp</groupId>
<artifactId>logging-interceptor</artifactId>
<version>2.7.5</version>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.1</version>
</dependency>
<dependency>
<groupId>io.gsonfire</groupId>
<artifactId>gson-fire</artifactId>
<version>1.8.0</version>
</dependency>
<!-- This dependency is required in Java 11 -->
<dependency>
<groupId>javax.annotation</groupId>
<artifactId>javax.annotation-api</artifactId>
<version>1.3.2</version>
</dependency>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment