Wikum Weerakutti

GSOC-2023: Coding Week 04

This week, my main objective was to update the SDK to Java 8. I dedicated my time to working on various tickets, and here's a list of the ones I tackled:

Update source and target Java version from 1.7 to 1.8

This is a fairly easy ticket. I just had to bump the source and target versions of maven-compiler-plugin to 1.8

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>3.8.1</version>
    <configuration>
        <source>1.8</source>
        <target>1.8</target>
    </configuration>
</plugin>

I created this pull request for it: https://github.com/openmrs/openmrs-sdk/pull/222

Update Integration test version to 4.7.0-SNAPSHOT (Merged)

So when we were releasing the 4.6.0 we had to temporally disable the integration tests because of space issues in bamboo: https://github.com/openmrs/openmrs-sdk/commit/4a0e7b886bd75be0aa5fe9000aae5cf7e28cda96. When OpenMRS bot bumped the versions of modules to 4.7.0-SNAPSHOT it didn't bump the version of the integration tests module: https://github.com/openmrs/openmrs-sdk/commit/d0376c381583b017535b710082a707e5b608d791. This caused an error when running the integration tests.

Error: ] Some problems were encountered while processing the POMs: [FATAL] Non-resolvable parent POM for org.openmrs.maven:openmrs-sdk-integration-tests:4.6.0-SNAPSHOT: Could not find artifact org.openmrs.maven:openmrs-sdk:pom:4.6.0-SNAPSHOT and 'parent.relativePath' points at wrong local POM @ line 5, column 13 @ Error: The build could not read 1 project -> [Help 1] Error: Error: The project org.openmrs.maven:openmrs-sdk-integration-tests:4.6.0-SNAPSHOT (/home/runner/work/openmrs-sdk/openmrs-sdk/integration-tests/pom.xml) has 1 error Error: Non-resolvable parent POM for org.openmrs.maven:openmrs-sdk-integration-tests:4.6.0-SNAPSHOT: Could not find artifact org.openmrs.maven:openmrs-sdk:pom:4.6.0-SNAPSHOT and 'parent.relativePath' points at wrong local POM @ line 5, column 13 -> [Help 2] Error: Error: To see the full stack trace of the errors, re-run Maven with the -e switch. Error: Re-run Maven using the -X switch to enable full debug logging. Error: Error: For more information about the errors and possible solutions, please read the following articles: Error: [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/ProjectBuildingException Error: [Help 2] http://cwiki.apache.org/confluence/display/MAVEN/UnresolvableModelException Error: Process completed with exit code 1.  

So created a PR to bump the Integration test version to 4.7.0-SNAPSHOT: https://github.com/openmrs/openmrs-sdk/pull/225

Use lambda expressions and Streams API to modernize the SDK (Code Review (Initial))

We wanted to start using lambda expressions and [Streams API] (https://docs.oracle.com/javase/8/docs/api/java/util/stream/Stream.html) on the SDK. Using these could make the code base more efficient and readable. Here is an example of using them:

private void cleanupSqlFiles(File targetDirectory) {
    File[] sqlFiles = targetDirectory.listFiles(new FilenameFilter() {
			@Override
			public boolean accept(File dir, String name) {
				return name.endsWith(".sql");
			}
		});
    for (File sql : sqlFiles) {
        FileUtils.deleteQuietly(sql);
    }
}

can be refactored into

private void cleanupSqlFiles(File targetDirectory) {
	File[] sqlFiles = targetDirectory.listFiles((dir, name) -> name.endsWith(".sql"));
	Arrays.stream(sqlFiles).forEach(FileUtils::deleteQuietly);
}

As you can see this reduces the lines of code vastly and make the code more clear and concise.

Switching to the new Date and Time API (Code Review (Initial))

The new Date and Time API offers developers a significantly improved approach to working with dates and times when compared to the outdated java.util.Date and java.util.Calendar classes. This updated API brings greater comprehensiveness and flexibility, resulting in code that is more intuitive and easier to maintain Let's take a look at an example where we can refactor code to the new Date and Time API.

private boolean checkIfOneWeekFromLastReport(){
	Date lastReport;
	try {
		lastReport = getLastReported();
	} catch (ParseException e) {
	lastReport = null;
	}
	Date currentDate = new Date();
	if (lastReport != null) {
		return  (currentDate.getTime() - lastReport.getTime()) / (60 * 60 * 1000 * 24) % 60 > 7;
	}
	else {
	return true;
	}
}

is refactored into

private boolean checkIfOneWeekFromLastReport(){
	LocalDate lastReport = getLastReported();
	LocalDate currentDate = LocalDate.now();
	if(lastReport != null) {
		return ChronoUnit.DAYS.between(lastReport, currentDate) > 7;
	}
	else {
	return true;
	}
}

I also added a few tests for these changes. You can find the pull request here: https://github.com/openmrs/openmrs-sdk/pull/226

Using Java 8 Optional Class (Code Review (Initial))

The Optional class allows us to handle null values more elegantly and concisely. This can lead to cleaner and more robust code.

Project project = Project.loadProject(userDir);
String artifactId = project.getArtifactId();
String groupId = project.getGroupId();
String version = project.getVersion();
if ((artifactId != null) && (groupId != null) && version != null) {

}

Can be refactored using Optional class to:

Optional<Project> project = Optional.of(Project.loadProject(userDir));
Optional<String> artifactId = project.map(Project::getArtifactId);
Optional<String> groupId = project.map(Project::getGroupId);
Optional<String> version = project.map(Project::getVersion);
if ((artifactId.isPresent()) && (groupId.isPresent()) && version.isPresent()) {

}

Here is the pull request for this: https://github.com/openmrs/openmrs-sdk/pull/227

Allow overriding openmrs assemble and build tool versions (Merged)

By default, SDK uses the latest version of OpenMRS SPA core to assemble and build the frontend. Because of the current significant frontend rewrite and related changes using the default version became unstable. So added a feature that allows users to override the SPA core version using a maven argument.

mvn openmrs-sdk:setup -DspaCoreVersion={version}

I also changed the SDK to use the next version of the SPA core by default. Here is the pull request: https://github.com/openmrs/openmrs-sdk/pull/228

Future

I hope to finalize all these tickets by next week. After that we can move onto another objective of this project.

Thank you for reading, see you next week!