Skip to content
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

Add a way to set the Kind from Embeddable #164 #165

Closed
wants to merge 1 commit into from
Closed
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
35 changes: 35 additions & 0 deletions src/main/java/com/jmethods/catatumbo/OverrideKind.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* Copyright 2016 Sai Pullabhotla.
*
* Licensed under the Apache License, Version 2.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://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.jmethods.catatumbo;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
* A class annotated with <code>OverrideKind</code> specifies a new kind for the enclosing @{@link Entity}
* when using within an @{@link Embedded} field.
* Only @{@link Embeddable} classes can be annotated with this annotation.
*
* @author Aurelien Thieriot
*/
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface OverrideKind {

String kind();
}
47 changes: 37 additions & 10 deletions src/main/java/com/jmethods/catatumbo/impl/EntityIntrospector.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,24 +15,16 @@
*/
package com.jmethods.catatumbo.impl;

import java.lang.reflect.Field;
import java.time.OffsetDateTime;
import java.time.ZonedDateTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Collections;
import java.util.Date;
import java.util.List;

import com.jmethods.catatumbo.CreatedTimestamp;
import com.jmethods.catatumbo.DatastoreKey;
import com.jmethods.catatumbo.Embeddable;
import com.jmethods.catatumbo.Embedded;
import com.jmethods.catatumbo.Entity;
import com.jmethods.catatumbo.EntityManagerException;
import com.jmethods.catatumbo.Identifier;
import com.jmethods.catatumbo.Key;
import com.jmethods.catatumbo.MappedSuperClass;
import com.jmethods.catatumbo.OverrideKind;
import com.jmethods.catatumbo.ParentKey;
import com.jmethods.catatumbo.ProjectedEntity;
import com.jmethods.catatumbo.Property;
Expand All @@ -41,6 +33,16 @@
import com.jmethods.catatumbo.UpdatedTimestamp;
import com.jmethods.catatumbo.Version;

import java.lang.reflect.Field;
import java.time.OffsetDateTime;
import java.time.ZonedDateTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Collections;
import java.util.Date;
import java.util.List;

/**
* Introspector for model classes with the annotation of {@link Entity} or
* {@link ProjectedEntity}. The introspect method gathers metadata about the
Expand Down Expand Up @@ -228,6 +230,7 @@ private void processFields() {
processParentKeyField(field);
} else if (field.isAnnotationPresent(Embedded.class)) {
processEmbeddedField(field);
processOverrideKind(field);
} else {
processField(field);
}
Expand Down Expand Up @@ -421,6 +424,30 @@ private void processEmbeddedField(Field field) {
entityMetadata.putEmbeddedMetadata(embeddedMetadata);
}

/**
* Process an OverrideKind annotation on an embedded field and
* update the current entity metadata if needs be
*
* @param field
* the embedded field
*/
private void processOverrideKind(Field field) {
OverrideKind overrideKind = field.getType().getAnnotation(OverrideKind.class);
Embeddable embeddable = field.getType().getAnnotation(Embeddable.class);

if (overrideKind != null) {
if (embeddable == null) {
String message = String.format("A class annotated with %s must also be annotated with %s",
OverrideKind.class.getName(),
Embeddable.class.getName()
);
throw new EntityManagerException(message);
}

entityMetadata.setKind(overrideKind.kind());
}
}

/**
* Convenient method for getting the metadata of the field used for
* optimistic locking.
Expand Down
10 changes: 10 additions & 0 deletions src/main/java/com/jmethods/catatumbo/impl/EntityMetadata.java
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,16 @@ public String getKind() {
return kind;
}

/**
* Sets the metadata Kind.
*
* @param kind
* entity kind parameter
*/
public void setKind(String kind) {
this.kind = kind;
}

/**
* Returns the metadata of the identifier.
*
Expand Down
115 changes: 115 additions & 0 deletions src/test/java/com/jmethods/catatumbo/entities/WrappedZipCode.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
/*
* Copyright 2016 Sai Pullabhotla.
*
* Licensed under the Apache License, Version 2.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://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.jmethods.catatumbo.entities;

import com.jmethods.catatumbo.Embeddable;
import com.jmethods.catatumbo.Embedded;
import com.jmethods.catatumbo.OverrideKind;
import com.jmethods.catatumbo.Property;

import java.util.Objects;

/**
* @author Aurelien Thieriot
*
*/
@Embeddable
@OverrideKind(kind = "wrapped")
public class WrappedZipCode {

@Property(name = "zip")
private String fiveDigits;

@Property(name = "zipx", indexed = false, optional = true)
private String fourDigits;

@Embedded(name = "useless", indexed = true)
private AnotherEmbeddable anotherEmbeddable = new AnotherEmbeddable();

/**
*
*/
public WrappedZipCode() {
// TODO Auto-generated constructor stub
}

/**
* @return the fiveDigits
*/
public String getFiveDigits() {
return fiveDigits;
}

/**
* @param fiveDigits
* the fiveDigits to set
*/
public void setFiveDigits(String fiveDigits) {
this.fiveDigits = fiveDigits;
}

/**
* @return the fourDigits
*/
public String getFourDigits() {
return fourDigits;
}

/**
* @param fourDigits
* the fourDigits to set
*/
public void setFourDigits(String fourDigits) {
this.fourDigits = fourDigits;
}

@Override
public String toString() {
return fiveDigits + "-" + fourDigits;
}

@Override
public boolean equals(Object obj) {
if (obj == null || !(obj instanceof ZipCode)) {
return false;
}
WrappedZipCode that = (WrappedZipCode) obj;
return Objects.equals(this.fiveDigits, that.fiveDigits) && Objects.equals(this.fourDigits, that.fourDigits)
&& Objects.equals(this.anotherEmbeddable, that.anotherEmbeddable);
}

@Override
public int hashCode() {
return Objects.hash(fiveDigits, fourDigits, anotherEmbeddable);
}

/**
* @return the anotherEmbeddable
*/
public AnotherEmbeddable getAnotherEmbeddable() {
return anotherEmbeddable;
}

/**
* @param anotherEmbeddable
* the anotherEmbeddable to set
*/
public void setAnotherEmbeddable(AnotherEmbeddable anotherEmbeddable) {
this.anotherEmbeddable = anotherEmbeddable;
}

}
67 changes: 67 additions & 0 deletions src/test/java/com/jmethods/catatumbo/entities/ZipCodeWrapper.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
* Copyright 2016 Sai Pullabhotla.
*
* Licensed under the Apache License, Version 2.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://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.jmethods.catatumbo.entities;

import com.jmethods.catatumbo.Embedded;
import com.jmethods.catatumbo.Entity;
import com.jmethods.catatumbo.Identifier;

/**
* @author Aurelien Thieriot
*
*/
@Entity(kind = "generic")
public class ZipCodeWrapper {

@Identifier
private long id;

@Embedded
private WrappedZipCode wrappedZipCode;

public ZipCodeWrapper() {

}

public ZipCodeWrapper(long id, WrappedZipCode wrappedZipCode) {
this.id = id;
this.wrappedZipCode = wrappedZipCode;
}

/**
* @return the id
*/
public long getId() {
return id;
}

/**
* @param id
* the id to set
*/
public void setId(long id) {
this.id = id;
}

public WrappedZipCode getWrappedZipCode() {
return wrappedZipCode;
}

public void setWrappedZipCode(WrappedZipCode wrappedZipCode) {
this.wrappedZipCode = wrappedZipCode;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import java.util.Map;
import java.util.Objects;

import com.jmethods.catatumbo.entities.ZipCodeWrapper;
import org.junit.Test;

import com.jmethods.catatumbo.EntityManagerException;
Expand Down Expand Up @@ -153,6 +154,12 @@ public void testIntrospect_TaskName() {
assertEquals(1, entityMetadata.getPropertyMetadataCollection().size());
}

@Test
public void testIntrospect_ZipCodeWrapper() {
EntityMetadata entityMetadata = EntityIntrospector.introspect(ZipCodeWrapper.class);
assertEquals("wrapped", entityMetadata.getKind());
}

@Test(expected = EntityManagerException.class)
public void testIntrospect_Button() {
try {
Expand Down