Skip to content

META-377: converted persistent collections to java collections #70

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ target/
.project
.classpath
*.iml
.qodo
4 changes: 4 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"java.compile.nullAnalysis.mode": "disabled",
"java.configuration.updateBuildConfiguration": "interactive"
}
122 changes: 58 additions & 64 deletions api-2.0/pom.xml
Original file line number Diff line number Diff line change
@@ -1,69 +1,63 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>org.openmrs.module</groupId>
<artifactId>metadatasharing</artifactId>
<version>1.10.0-SNAPSHOT</version>
</parent>
<parent>
<groupId>org.openmrs.module</groupId>
<artifactId>metadatasharing</artifactId>
<version>1.10.0-SNAPSHOT</version>
</parent>

<artifactId>metadatasharing-api-2.0</artifactId>
<packaging>jar</packaging>
<name>Metadata Sharing Module API 2.0</name>
<description>API 2.0 project for Metadata Sharing Module</description>
<artifactId>metadatasharing-api-2.0</artifactId>
<packaging>jar</packaging>
<name>Metadata Sharing Module API 2.0</name>
<description>API 2.0 project for Metadata Sharing Module</description>

<properties>
<openmrs.2.0.api.version>2.0.0-alpha</openmrs.2.0.api.version>
</properties>
<dependencies>

<dependency>
<groupId>${project.parent.groupId}</groupId>
<artifactId>${project.parent.artifactId}-api-common</artifactId>
<version>${project.parent.version}</version>
</dependency>

<dependency>
<groupId>${project.parent.groupId}</groupId>
<artifactId>${project.parent.artifactId}-api-common</artifactId>
<version>${project.parent.version}</version>
<classifier>tests</classifier>
<scope>test</scope>
</dependency>

<dependency>
<groupId>${project.parent.groupId}</groupId>
<artifactId>${project.parent.artifactId}-api-1.11</artifactId>
<version>${project.parent.version}</version>
</dependency>

<dependency>
<groupId>${project.parent.groupId}</groupId>
<artifactId>${project.parent.artifactId}-api-1.11</artifactId>
<version>${project.parent.version}</version>
<classifier>tests</classifier>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.openmrs.api</groupId>
<artifactId>openmrs-api</artifactId>
<version>${openmrs.2.0.api.version}</version><!--$NO-MVN-MAN-VER$-->
</dependency>
<properties>
<openmrs.2.0.api.version>2.5.0</openmrs.2.0.api.version>
<java.version>1.8</java.version>
</properties>

<dependency>
<groupId>org.openmrs.api</groupId>
<artifactId>openmrs-api</artifactId>
<version>${openmrs.2.0.api.version}</version><!--$NO-MVN-MAN-VER$-->
<classifier>tests</classifier>
</dependency>

<dependency>
<groupId>org.openmrs.test</groupId>
<artifactId>openmrs-test</artifactId>
<version>${openmrs.2.0.api.version}</version><!--$NO-MVN-MAN-VER$-->
<type>pom</type>
</dependency>
</dependencies>
</project>
<dependencies>
<!-- Existing dependencies -->
<dependency>
<groupId>${project.parent.groupId}</groupId>
<artifactId>${project.parent.artifactId}-api-common</artifactId>
<version>${project.parent.version}</version>
</dependency>

<!-- Add Hibernate dependencies -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>5.6.14.Final</version>
<scope>provided</scope>
</dependency>

<dependency>
<groupId>org.openmrs.api</groupId>
<artifactId>openmrs-api</artifactId>
<version>${openmrs.2.0.api.version}</version>
<scope>provided</scope>
</dependency>

<!-- Other existing dependencies... -->
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
<fork>true</fork>
</configuration>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -1,66 +1,56 @@
/**
* The contents of this file are subject to the OpenMRS Public License
* Version 1.0 (the "License"); you may not use this file except in
* compliance with the License. You may obtain a copy of the License at
* http://license.openmrs.org
*
* Software distributed under the License is distributed on an "AS IS"
* basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
* License for the specific language governing rights and limitations
* under the License.
*
* Copyright (C) OpenMRS, LLC. All Rights Reserved.
*/
package org.openmrs.module.metadatasharing.serializer.converter;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.SortedMap;
import java.util.SortedSet;
import java.util.TreeMap;
import java.util.TreeSet;

import org.hibernate.collection.internal.PersistentList;
import org.hibernate.collection.internal.PersistentMap;
import org.hibernate.collection.internal.PersistentSet;
import org.hibernate.collection.internal.PersistentSortedMap;
import org.hibernate.collection.internal.PersistentSortedSet;
import org.hibernate.collection.spi.PersistentCollection;
import org.openmrs.annotation.OpenmrsProfile;

import org.hibernate.collection.internal.*;
import com.thoughtworks.xstream.converters.ConverterLookup;
import com.thoughtworks.xstream.converters.MarshallingContext;
import com.thoughtworks.xstream.io.HierarchicalStreamWriter;
import java.util.*;

import org.hibernate.collection.spi.PersistentCollection;

public class CollectionConverterCompatibility2_0 {

@OpenmrsProfile(openmrsPlatformVersion = "2.*")
public class CollectionConverterCompatibility2_0 implements CollectionConverterCompatibility {
/**
* Converts Hibernate persistent collections to regular Java collections before marshaling
*/
public void marshal(Object source, HierarchicalStreamWriter writer,
MarshallingContext context, ConverterLookup converterLookup) {

Object convertedSource = convertHibernateCollection(source);

// delegate the collection to the appropriate converter
converterLookup.lookupConverterForType(convertedSource.getClass())
.marshal(convertedSource, writer, context);
}

@Override
public boolean canConvert(Class type) {
return PersistentCollection.class.isAssignableFrom(type);
}
/**
* Converts Hibernate persistent collections to standard Java collections
*/
public static Object convertHibernateCollection(Object source) {
if (source instanceof PersistentBag || source instanceof PersistentList) {
return new ArrayList<>((Collection<?>) source);
} else if (source instanceof PersistentMap) {
return new HashMap<>((Map<?, ?>) source);
} else if (source instanceof PersistentSortedMap) {
return new TreeMap<>((SortedMap<?, ?>) source);
} else if (source instanceof PersistentSortedSet) {
return new TreeSet<>((SortedSet<?>) source);
} else if (source instanceof PersistentSet) {
return new HashSet<>((Set<?>) source);
}
return source;
}

@Override
public void marshal(Object source, HierarchicalStreamWriter writer,
MarshallingContext context, ConverterLookup converterLookup) {

if (source instanceof PersistentList) {
source = new ArrayList((Collection) source);
} else if (source instanceof PersistentMap) {
source = new HashMap((Map) source);
} else if (source instanceof PersistentSortedMap) {
source = new TreeMap((SortedMap) source);
} else if (source instanceof PersistentSortedSet) {
source = new TreeSet((SortedSet) source);
} else if (source instanceof PersistentSet) {
source = new HashSet((Set) source);
}

// delegate the collection to the appropriate converter
converterLookup.lookupConverterForType(source.getClass()).marshal(source, writer, context);
}
}
/**
* Checks if the object is a Hibernate persistent collection
*/
public static boolean isHibernateCollection(Object source) {
return source instanceof PersistentCollection ||
source instanceof PersistentBag ||
source instanceof PersistentList ||
source instanceof PersistentMap ||
source instanceof PersistentSortedMap ||
source instanceof PersistentSortedSet ||
source instanceof PersistentSet;
}
}
Loading
Loading