spring-data-envers -2.0.0.RELEASE - spring-boot

Right now we are using 0.3.0.RELEASE to get the revision data and noticed that the revisions are fetched in ascending order.
the existing code
#EnableJpaRepositories( value = "org.xxx.xxx.xxx.repository", repositoryFactoryBeanClass = RevisionRepositoryFactoryBean.class )
#EnableJpaAuditing( auditorAwareRef = "springSecurityAuditorAware" )
#EnableTransactionManagement
public class DatabaseConfiguration {
.......
}
public class RevisionRepositoryFactoryBean extends EnversRevisionRepositoryFactoryBean {
public RevisionRepositoryFactoryBean() {
setRevisionEntityClass( Revision.class );
}
}
Found that the newer version 2.0.0.RELEASE has functionality to sort based on sort in pageable object. So wanted to use 2.0.0.RELEASE.
the following is the modifications
public class RevisionRepositoryFactoryBean<T extends RevisionRepository<S, ID, N>, S, ID extends Serializable, N extends Number & Comparable<N>> extends EnversRevisionRepositoryFactoryBean<T, S, ID,N> {
public RevisionRepositoryFactoryBean(Class<? extends T> repositoryInterface) {
super(repositoryInterface);
setRevisionEntityClass( Revision.class );
// TODO Auto-generated constructor stub
}
}
but did not work...
compile error in DatabaseConfiguration file
No constructor with 0 arguments defined in class
'org.xxx.xxx.xxx.config.RevisionRepositoryFactoryBean'
Do anyone has an example how to use spring-data-envers -2.0.0.RELEASE in spring boot application.
Thanks
Srini

Related

Deserialise JSON fields based on user role

I have some fields in a model that I only want to be returned when the logged in user has the role ROLE_ADMIN. I can use #JsonIgnore but that hides it for everyone. How can I make it hide dynamically?
You should use Jackson Json Views technology to acheive it - it allows to choose a different set of fields to be serialized programatically. It is also supported by Spring
Consider you have a class Model with two properties: commonField which should be available for everyone and secretField which should be available only for certain users. You should create an hierarchy of views (any classes would work) and specify which field is available in which view using #JsonView annotation
package com.stackoverflow.jsonview;
import com.fasterxml.jackson.annotation.JsonView;
public class Model {
public static class Public {}
public static class Secret extends Public {}
#JsonView(Public.class)
private String commonField;
#JsonView(Secret.class)
private String secretField;
public Model() {
}
public Model(String commonField, String secretField) {
this.commonField = commonField;
this.secretField = secretField;
}
public String getCommonField() {
return commonField;
}
public void setCommonField(String commonField) {
this.commonField = commonField;
}
public String getSecretField() {
return secretField;
}
public void setSecretField(String secretField) {
this.secretField = secretField;
}
}
Now you can specify the view you want to use in concrete ObjectMapper
package com.stackoverflow.jsonview;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.Test;
import static org.junit.Assert.*;
/**
*/
public class ModelTest {
#Test
public void testSecretField() throws JsonProcessingException {
Model model = new Model("commonField","secretField");
assertEquals("{\"commonField\":\"commonField\",\"secretField\":\"secretField\"}", new ObjectMapper().writerWithView(Model.Secret.class).writeValueAsString(model));
assertEquals("{\"commonField\":\"commonField\"}", new ObjectMapper().writerWithView(Model.Public.class).writeValueAsString(model));
}
}
I am not sure if you can use declaratie approach to make spring choose the right view based on user role out of the box, so probably you will have to write some code like this:
#RequestMapping("/data")
public String getData(HttpServletRequest request) {
Model model = service.getModel();
ObjectMapper objectMapper = new ObjectMapper();
objectMapper = request.isUserInRole("ROLE_ADMIN") ? objectMapper.writerWithView(Model.Secret.class) : objectMapper.writerWithView(Model.Public.class);
return objectMapper.writeValueAsString(model);
}
I solved this after literally a full month of trying various things. I'm working with Spring 4.3.1 and boot, with data being returned in Hal using a pagedrepository.
extend RepositoryRestMvcConfiguration as MyRepositoryRestMvcConfiguration and add #Configuration to the class, make sure your starter class has #EnableWebMvc
add this to MyRepositoryRestMvcConfiguration- extend TypeConstrainedMappingJackson2HttpMessageConverter as MyResourceSupportHttpMessageConverter
add this to MyRepositoryRestMvcConfiguration
#Override
#Bean
public TypeConstrainedMappingJackson2HttpMessageConverter halJacksonHttpMessageConverter() {
ArrayList<MediaType> mediaTypes = new ArrayList<MediaType>();
mediaTypes.add(MediaTypes.HAL_JSON);
if (config().useHalAsDefaultJsonMediaType()) {
mediaTypes.add(MediaType.APPLICATION_JSON);
}
int order = config().useHalAsDefaultJsonMediaType() ? Ordered.LOWEST_PRECEDENCE - 10
: Ordered.LOWEST_PRECEDENCE - 1;
TypeConstrainedMappingJackson2HttpMessageConverter converter = new MyResourceSupportHttpMessageConverter(
order);
converter.setObjectMapper(halObjectMapper());
converter.setSupportedMediaTypes(mediaTypes);
converter.getObjectMapper().addMixIn(Object.class, MyFilteringMixin.class);
final FilterProvider myRestrictionFilterProvider = new SimpleFilterProvider()
.addFilter("MyFilteringMixin", new MyPropertyFilter()).setFailOnUnknownId(false);
converter.getObjectMapper().setFilterProvider(myRestrictionFilterProvider);
return converter;
}
Create an empty Mixin
package filters;
import com.fasterxml.jackson.annotation.JsonFilter;
#JsonFilter("MyFilteringMixin")
public class MyFilteringMixin {}
Create an empty Mixin
create class MyPropertyFilter extending SimpleBeanPropertyFilter and override adapt this method
serializeAsField(Object, JsonGenerator, SerializerProvider, PropertyWriter)you need to call either super.serializeAsField(pPojo, pJgen, pProvider, pWriter) or pWriter.serializeAsOmittedField(pPojo, pJgen, pProvider) depending on whether you wish to include or discard this particular field.
I added an annotation to the particular fields I wanted to alter and interrogated that annotation when deciding which of these two to call. I injected the security role and stored permitted roles in the annotation.
This alters what Hal shares out to the caller, not what Hal is holding in its repository. Thus you can morph it depending on who the caller is.

Declare parents aspectj

Firstly I try xml configuration:
<aop:aspect>
<aop:declare-parents types-matching="DBOperations.ILearningData+"
implement-interface="DBOperations.ISaveResults"
delegate-ref="saverExtension"/>
</aop:aspect>
and it works good.
Now I try to make aspectj, which should do the same:
public aspect ASaveResults {
public ASaveResults() { }
declare parents : TSaveResults implements ILearningData;
}
where TSaveResults is the same as the bean "saverExtension".
I run my code:
...
#Value("#{learningData}")
protected ILearningData saver;
...
((ISaveResults)saver).saveResults();
and get the error:
Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: sun.proxy.$Proxy12 cannot be cast to DBOperations.ISaveResults
what is the problem with my aspectj?
Also I tried this code unsuccessfully:
public aspect ASaveResults {
public ASaveResults() { }
declare parents : ISaveResults implements ILearningData;
public void saveResults() {
System.out.println("saver aspect");
}
}
What you used there is core aspectj, so if you want to use Spring AOP, but not xml config then this is what you should do (not tested):
#Aspect
public class ASaveResults {
#DeclareParents(value="ISaveResults")
public static ILearningData interf;
public void saveResults() {
System.out.println("saver aspect");
}
}
public aspect ASaveResults {
public ASaveResults() { }
declare parents : LearningData extends TSaveResults;
}
where LearningData and TSaveResults - classes. So now TSaveResults extends LearningData - this was my goal

using spring test context to initialize data

I was wondering if it's possible to initialize test data by implementing the TestExecutionListener interface and use the beforeTestClass and afterTestClass to load/dispose data. The test data will be available in a flat file and I would like the data file location to be as part of the test class annotation
#RunWith(SpringJUnit4ClassRunner.class)
#ContextConfiguration(locations={"classpath:spring/test-dao.xml"})
#TestExecutionListeners(
{
DependencyInjectionTestExecutionListener.class,
InsertTestDataExecutionListener.class
})
#DataSetLocation("classpath:data/test-dao-dataset.xml")
public abstract class AbstractDaoTests {
public List testdata....
}
In the above pseudocode, the InsertTestDataExecutionListener will implement the TestExecutionListener interface and in the beforeClass method, get the dataset location from the annotation. I am trying to find out how I could setup the contents of the property 'testdata' using the TestContext.
public class InsertTestDataExecutionListener implements TestExecutionListener {
public void beforeTestClass(TestContext aContext) {
DataSetLocation dsLocation = aContext.getTestClass().getAnnotation(
DataSetLocation.class
);
//Load the contents of the file using the dataset location.
?? How to set the property of 'testdata' from the Abstract class
}
}
Should I be using reflection to do the work?
As I undestand it is not required to access Spring context during data load (it is just plain file in classpath). So, you may do the work without listeners:
public abstract class AbstractDaoTests {
public List testdata;
public List getTestData() {...}
public abstract String getDataLocation();
public AbstractDaoTests () {
testData = loadDataFromLocation(getTestData());
}
}
public class ConcreteTest extend AbstractDaoTests {
#Override
public String getDataLocation() {return "classpath:data/test-dao-dataset.xml";}
}
Of course you may use annotation instead of abstract method and get it from this.getClass().getAnnotation in constuctor.

How to create count query on MongoRepository

I am creating a MongoRepository and need to create a count query. Can someone provide an example of what is the best way to do this via the SpringData MongoDB MongoRepository facility? All the examples I was able to find reference returning a List but not counts.
Here is what I am trying to do (obviously it does not work):
public interface SchoolRepository extends MongoRepository<School, String> {
#Query("db.school.count({studentStatus: ?0});")
int getCountOfStudents(int studentStatus);
}
Thanks.
-AP_
I found this question as I was trying to do something similar. Unfortunately, given what I see in org.springframework.data.repository.query.parser.PartTree:
private static final Pattern PREFIX_TEMPLATE = Pattern.compile("^(find|read|get)(\\p{Upper}.*?)??By");
It does not appear to be supported.
Instead, we can add custom behaviour to the repository (see reference manual section 1.4.1) by creating a new interface and a class that implements it.
public interface SchoolRepository extends CrudRepository<School, String>, SchoolRepositoryCustom {
// find... read... get...
}
public interface SchoolRepositoryCustom {
int getCountOfStudents(int studentStatus);
}
#Service
public class SchoolRepositoryImpl implements SchoolRepositoryCustom {
#Autowired
private SchoolRepository schoolRepository;
public int getCountOfStudents(int studentStatus) {
// ...
}
}
Note that the class is named SchoolRepositoryImpl, not SchoolRepositoryCustomImpl.

NullPointerException in EnumMap when auto generating wadl with Jersey

I am using Tomcat 7, Jaxb2 and Jersey1.11.
I have a class EnumProperty which inherits from an abstract class Property.
#XmlAccessorType(XmlAccessType.FIELD)
public class EnumProperty extends Property<Enum> {
#XmlElement(name = "property_value", nillable = true)
private Enum value;
public EnumProperty() {
setValueType(PropertyValueTypeEnum.ENUM);
}
#Override
public Enum getValue() {
return value;
}
#Override
public void setValue(Enum value) {
this.value = value;
}
}
There are other sub classes for the Property class. In addition, I have another class, Entity, which holds a collection of properties. I have also a resource which returns in one of its sub resources a Collection. When I try to generate my application wadl I receive a NullPointerException. I isolated the problem to the EnumProperty class. Can anyone please help me understand where the problem is?
java.lang.NullPointerException
java.util.EnumMap.<init>(Unknown Source)
com.sun.xml.bind.v2.model.impl.RuntimeEnumLeafInfoImpl.<init>(RuntimeEnumLeafInfoImpl.java:87)
com.sun.xml.bind.v2.model.impl.RuntimeModelBuilder.createEnumLeafInfo(RuntimeModelBuilder.java:109)
com.sun.xml.bind.v2.model.impl.RuntimeModelBuilder.createEnumLeafInfo(RuntimeModelBuilder.java:85)
com.sun.xml.bind.v2.model.impl.ModelBuilder.getClassInfo(ModelBuilder.java:228)
com.sun.xml.bind.v2.model.impl.RuntimeModelBuilder.getClassInfo(RuntimeModelBuilder.java:104)
com.sun.xml.bind.v2.model.impl.RuntimeModelBuilder.getClassInfo(RuntimeModelBuilder.java:85)
com.sun.xml.bind.v2.model.impl.ModelBuilder.getClassInfo(ModelBuilder.java:213)
com.sun.xml.bind.v2.model.impl.RuntimeModelBuilder.getClassInfo(RuntimeModelBuilder.java:99)
com.sun.xml.bind.v2.model.impl.RuntimeModelBuilder.getClassInfo(RuntimeModelBuilder.java:85)
com.sun.xml.bind.v2.model.impl.ModelBuilder.getTypeInfo(ModelBuilder.java:319)
com.sun.xml.bind.v2.model.impl.TypeRefImpl.calcRef(TypeRefImpl.java:96)
com.sun.xml.bind.v2.model.impl.TypeRefImpl.getTarget(TypeRefImpl.java:73)
com.sun.xml.bind.v2.model.impl.RuntimeTypeRefImpl.getTarget(RuntimeTypeRefImpl.java:62)
com.sun.xml.bind.v2.model.impl.RuntimeTypeRefImpl.getTarget(RuntimeTypeRefImpl.java:55)
com.sun.xml.bind.v2.model.impl.ElementPropertyInfoImpl$1.get(ElementPropertyInfoImpl.java:78)
com.sun.xml.bind.v2.model.impl.ElementPropertyInfoImpl$1.get(ElementPropertyInfoImpl.java:81)
java.util.AbstractList$Itr.next(Unknown Source)...

Resources