Building an aspect with the same annotation can be on the class or the method and can be both - spring-aop

I'm creating a custom Metric Aspect so that I don't have to do METRIC.aboutTo("save", "order") in the code. My organization uses custom metric libraries.
So, I want to create a custom metric annotation and aspect to do this for me.
I'm looking to be able to add the annotation #PostgressMetric to both the class and the method. It would look something like this:
#Retention(RetentionPolicy.RUNTIME)
#Target({ElementType.TYPE, ElementType.METHOD})
public #interface PostgressMetric {
String value() default "";
String table() default "";
}
If the metric is on the class, then I want to use those values as "defaults". So you could have something like this:
#Repository
#PostgressMetric(table = "order")
public class OrderRepository {
#PostgressMetric("save")
public void save(Order order) {
}
}
This is also a valid setup:
#Repository
public class OrderRepository {
#PostgressMetric(table="order", value="save")
public void save(Order order) {
}
}
and of course this is also a valid setup:
#Repository
#PostgressMetric(table="order", value="save")
public class OrderRepository {
public void save(Order order) {
}
}
Now, in my aspect I want to get the annotation from the class, then get the annotation from the method. If the class has the annotation then get the table and value and use those as default values. If the method has the annotation then if the values aren't blank, use them to override the default values specified in the class annotation.
Is there a standard way to get the two annotations, or is it just a brute force get the class values and then get the method values and override them?

Related

how to get range class of objectProperty by its domain class in owlapi?

In my project, I'd like to get all the range class related to the given class by an restricted(somevaluefrom or allvalues from) objectproperties. I can get the restricted subclassofAxioms expressions including the given class, but how can I get the range class in these expressions? In other word, how can I get all the related classes to the given class excluding inherited subclass.
For example:
public static void printSubClassOfAxioms(OWLOntology ontology,OWLReasoner reasoner,OWLClass owlClass){
for(OWLSubClassOfAxiom ax:ontology.getSubClassAxiomsForSubClass(owlClass)){
OWLClassExpression expression=ax.getSuperClass();
System.out.println(ax);
System.out.println(expression);
}
}
The results are:
SubClassOf(<#FourCheesesTopping> <#CheeseTopping>)
SubClassOf(<#FourCheesesTopping> ObjectSomeValuesFrom(<#hasSpiciness> <#Mild>))
SubClassOf(<#FourCheesesTopping> ObjectAllValuesFrom(<#hasCountryOfOrigin> #Country>))
How can I just get the range classes #Country and #Mild
Thank you for your attention!
Write an OWLObjectVisitor and override the visit(OWL... Type) for the restrictions you're interested in. At that point,
type.getFiller()
will yield the class you're after.
Examples are in the documentation: https://github.com/owlcs/owlapi/wiki/Documentation
public class RestrictionVisitor extends OWLClassExpressionVisitor {
#Override
public void visit(#Nonnull OWLObjectSomeValuesFrom ce) {
// This method gets called when a class expression is an existential
// (someValuesFrom) restriction and it asks us to visit it
}
}

Get spring-data-mongodb to honor getter/setter without backing field?

I have a general-purpose POJO:
public class Thing {
private String name;
private String etc;
public String getName() {
return name;
}
// other getters and setters
}
I'm using Spring 4.3.9 and Spring-data-mongodb 1.10.4. I want to store instances of this POJO in Mongodb, but I have some constraints:
I can't add Spring annotations to the base class (but I can subclass Thing and annotate that).
I want to use the name field as the Mongodb unique ID (mainly to avoid creating a separate unique index for it).
I want to (redundantly) store the name field as an actual field named "name", so that other consumers of the collection don't have to know that "name" is stored in the _id.
I started out trying this:
public class SpringThing extends Thing {
#Id
#Override
public String getName() {
return super.getName();
}
#Override
public void setName(String name) {
super.setName(name);
}
}
This causes spring to use the value of name for _id, but of course it doesn't store a field named "name" in Mongodb. The documentation says that spring will use a "property or field" named "id" or annotated with #Id. So I tried defining a redundant getter/setter which accesses the name field:
public class SpringThing extends Thing {
#Id
public String getId() {
return super.getName();
}
public void setId(String id) {
super.setName(id);
}
}
Unfortunately, spring ignores getId and setId here, and stores the object with an autogenerated ID. I also tried creating redundant getters/setters annotated with #Field("name"), but spring seems to ignore any getter/setter pair without an actual field.
Adding an actual ID field and storing a copy of the name there does work:
public class SpringThing extends Thing {
#Id
private String id;
#Override
public void setName(String id) {
this.id = id;
super.setName(id);
}
}
But it requires defining a pointless field named "id".
Is there a more reasonable way to get what I want? Is what I'm trying to do reasonable to begin with?
Thanks to a hint by #mp911de, I ended up creating a subclass of Thing that looks like this:
#TypeAlias("thing")
#Document(collection = "things")
public class SpringThing extends Thing {
#Id
#AccessType(Type.PROPERTY)
#JsonIgnore
public String getId() {
return super.getName();
}
public void setId(String taskName) {
super.setName(taskName);
}
}
The #TypeAlias annotation overrides the name which spring would use for the type, to cover up the fact that I've created a subclass just to add annotations.
#Id says that this is the getter for _id.
#AccessType says to access this field through the getter and setter rather than by directly accessing the field. This is what I needed; without it, spring looks for a private member variable named something like id.
#JsonIgnore is the Jackson (JSON library that we're using) annotation to prevent including the id field when serializing these objects to JSON.

Use of Wrapper class for deserialization in callout?

I found the following use of a wrapper class, and was wondering if it is a good practice or whether its just duplication of code for no reason.
//Class:
public class SomeClass{
public Integer someInt;
public String someString;
}
//Callout Class:
public class CalloutClass{
public SomeClass someMethod(){
//...code to do a callout to an api
SomeClass someClassObj = (SomeClass)JSON.Deserialize(APIResponse.getBody(), SomeClass.class);
return someClassObj;
}
}
//Controller:
public class SomeController {
public SomeController(){
someClassObj = calloutClassObj.someMethod();
SomeWrapper wrapperObj = new SomeWrapper();
for(SomeClass iterObj : someClassObj){
wrapperObj.someWrapperInt = iterObj.someInt;
wrapperObj.someWrapperString = iterObj.someString;
}
}
public class someWrapper{
public Integer someWrapperInt{get;set;}
public String someWrapperString{get;set;}
}
}
The wrapper class "someWrapper" could be eliminated if we just use getters and setters ({get;set;}) in "SomeClass."
Could anyone explain if there could be a reason for following this procedure?
Thanks,
James
My assumption (because, code in controller is extra pseudo) is
SomeClass is a business entity, purpose of which is to store/work with business data. By work I mean using it's values to display it (using wrapper in controller), to calculate smth in other entities or build reports... Such kind of object should be as lightweight as possible. You usually iterate through them. You don't need any methods in such kind of objects. Exception is constructor with parameter(s). You might want to have SomeObject__c as parameter or someWrapper.
someWrapper is a entity to display business entity. As for wrapper classes in controllers. Imagine, that when you display entity on edit page and enter a value for someWrapperInt property, you want to update someWrapperString property (or you can just put validation there, for example, checking if it is really Integer). Usually, as for business entity, you don't want such kind of functionality. But when user create or edit it, you may want smth like this.

How to extend abstract Entity class in RIA Services

I want to add a bool variable and property to the base Entity class in my RIA services project so that it is available throughout all the entity objects but seem unable to work out how to do this. I know that adding properties to actual entities themselves is easy using .shared.cs and partial classes but adding such properties to the Entity class using similar methods doesn't work.
For example, the following code doesn't work
namespace System.ServiceModel.DomainServices.Client
{
public abstract partial class Entity
{
private bool auditRequired;
public bool AuditRequired
{
get { return auditRequired; }
set { auditRequired = value; }
}
}
}
All that happens is that the existing Entity class gets totally overriden rather than extending the Entity class.
How do I extend the base Entity class so that functionality is available thoughout all derived entity classes?
You won't be able to add a property to the Entity class. This class is already compiled and cannot be modified (partial classes only work because your have the source code of the class in your solution and the code can be merged at compile time).
One option may be to create a class that inherits from Entity, then add your property in this class, and have your entities inherit from your custom class instead of Entity. This might not be practical for use with designers, though.
public class MyEntityBase : Entity
{
private bool auditRequired;
public bool AuditRequired
{
get { return auditRequired; }
set { auditRequired = value; }
}
}
public class Entity1 : MyEntityBase
{
}

Inheritance concept in jpa

I created one table using Inheritance concept to sore data into google app engine datastore. It contained following coding but it shows error.How to user Inheritance concept.What error in my program
Program 1:
#Entity
#Inheritance(strategy = InheritanceType.JOINED)
public class calender {
#Id
private String EmailId;
#Basic
private String CalName;
#Basic
public void setEmailId(String emailId) {
EmailId = emailId;
}
public String getEmailId() {
return EmailId;
}
public void setCalName(String calName) {
CalName = calName;
}
public String getCalName() {
return CalName;
}
public calender(String EmailId, String CalName) {
this.EmailId = EmailId;
this.CalName = CalName;
}
}
Program 2:
#Entity
public class method extends calender {
#Id
private String method;
public void setMethod(String method) {
this.method = method;
}
public String getMethod() {
return method;
}
public method(String method) {
this.method = method;
}
}
My constraint is I want output like this
Calendartable contain
Emailid
calendarname
and method table contain
Emailid
method
How to achieve this?
It shows the following error in this line public method(String method)
java.lang.Error: Unresolved compilation problem:
Implicit super constructor calender() is undefined. Must explicitly invoke another constructor
According to Using JPA with App Engine, the JOINED inheritance strategy is not supported.
Your code doesn't compile, add a default constructor in Calendar.
I don't think you should annotate the method field with #Id.
The datastore of GAE/J is not an RDBMS so consequently the only "inheritance strategy" that makes any sense is TABLE_PER_CLASS. I would expect GAE/J to throw an exception if you specify that strategy, and if it doesn't then you ought to raise an issue against them
Your error "constructor calender() is undefined" is rather straightforward. You should create constructor without parameters in calendar class (you can make it private if you don't want to use it). That's because compiler can create default constructor by himself only if there aren't another constructors in the class.

Resources