Need to understand broadleaf commerce custom annotation implementation - java

I have been trying go through code of Broadleaf Commerce. There were multiple Custom Annotations used however I was not able to locate there Processor. Can anyone help me here. To take example #AdminPresentation it a custom annotation in package org.broadleafcommerce.common.presentation;
However how this is processed throughout the app, I was not able to locate. What I have understood till now we can use Reflection or AOP for its processing. But There was nothing for this.
Please help.
Source code - https://github.com/BroadleafCommerce/BroadleafCommerce

For a short answer, org.broadleafcommerce.openadmin.server.dao.provider.metadata.BasicFieldMetadataProvider#addMetadata is one place that processes those annotations.
On a broader level, the controllers in the openadmin will use the AdminEntityService to get ClassMetaData (all data about how a class should be displayed to an admin user). The #AdminPresentation annotation is one source of this data. The method AdminEntityServiceImpl#getClassMetadata is the main gateway for getting the ClassMetaData.
#getClassMetadata calls #inspect and eventually gets to PersistenceManager#inspect. This method uses the DynamicEntityDao to eventually get to Metadata#getFieldMetadataForTargetClass. That method gets each field of a class via reflection, and then each of those Fields is processed through the available FieldMetadataProviders. The FieldMetadataProviders turn a java.lang.reflect.Field into org.broadleafcommerce.openadmin.dto.FieldMetadata.
Any provided FieldMetadataProvider can contribute field metadata. This metadata is used in the FormBuilderService to construct the admin page.
Class References:
AdminEntityService - org.broadleafcommerce.openadmin.server.service.AdminEntityServiceImpl
PersistenceManager - org.broadleafcommerce.openadmin.server.service.persistence.PersistenceManagerImpl#inspect
DynamicEntityDao - org.broadleafcommerce.openadmin.server.dao.DynamicEntityDaoImpl#getPropertiesForEntityClass
Metadata - org.broadleafcommerce.openadmin.server.dao.Metadata#getFieldMetadataForTargetClass
FieldMetadataProvider - org.broadleafcommerce.openadmin.server.dao.provider.metadata.FieldMetadataProvider, org.broadleafcommerce.openadmin.server.dao.DynamicEntityDaoImpl#fieldMetadataProviders
FormBuilderService - org.broadleafcommerce.openadmin.web.service.FormBuilderServiceImpl

Have a look to :
https://www.baeldung.com/java-custom-annotation
You will get explanations about : "default" in the Custom Annotations.
Florent COUDERT.

Related

Use placeholders in feature files

I would like to use placeholders in a feature file, like this:
Feature: Talk to two servers
Scenario: Forward data from Server A to Server B
Given MongoDb collection "${db1}/foo" contains the following record:
"""
{"key": "value"}
"""
When I send GET "${server1}/data"
When I forward the respone to PUT "${server2}/data"
Then MongoDB collection "${db2}/bar" MUST contain the following record:
"""
{"key": "value"}
"""
The values of ${server1} etc. would depend on the environment in which the test is to be executed (dev, uat, stage, or prod). Therefore, Scenario Outlines are not applicable in this situation.
Is there any standard way of doing this? Ideally there would be something which maintains a Map<String, String> that can be filled in a #Before or so, and runs automatically between Cucumber and the Step Definition so that inside the step definitions no code is needed.
Given the following step definitions
public class MyStepdefs {
#When("^I send GET "(.*)"$)
public void performGET(final String url) {
// …
}
}
And an appropriate setup, when performGET() is called, the placeholder ${server1} in String uri should already be replaced with a lookup of a value in a Map.
Is there a standard way or feature of Cucumber-Java of doing this? I do not mind if this involves dependency injection. If dependency injection is involved, I would prefer Spring, as Spring is already in use for other reasons in my use case.
The simple answer is that you can't.
The solution to your problem is to remove the incidental details from your scenario all together and access specific server information in the step defintions.
The server and database obviously belong together so lets describe them as a single entity, a service.
The details about the rest calls doesn't really help to convey what you're
actually doing. Features don't describe implementation details, they describe behavior.
Testing if records have been inserted into the database is another bad practice and again doesn't describe behavior. You should be able to replace that by an other API call that fetches the data or some other process that proves the other server has received the information. If there are no such means to extract the data available you should create them. If they can't be created you can wonder if the information even needs to be stored (your service would then appear to have the same properties as a black hole :) ).
I would resolve this all by rewriting the story such that:
Feature: Talk to two services
Scenario: Forward foobar data from Service A to Service B
Given "Service A" has key-value information
When I forward the foobar data from "Service A" to "Service B"
Then "Service B" has received the key-value information
Now that we have two entities Service A and Service B you can create a ServiceInformationService to look up information about Service A and B. You can inject this ServiceInformationService into your step definitions.
So when ever you need some information about Service A, you do
Service a = serviceInformationService.lookup("A");
String apiHost = a.getApiHost():
String dbHost = a.getDatabaseHOst():
In the implementation of the Service you look up the property for that service System.getProperty(serviceName + "_" + apiHostKey) and you make sure that your CI sets A_APIHOST and A_DBHOST, B_APIHOST, B_DBHOST, ect.
You can put the name of the collections in a property file that you look up in a similar way as you'd look up the system properties. Though I would avoid direct interaction with the DB if possible.
The feature you are looking for is supported in gherkin with qaf. It supports to use properties defined in properties file using ${prop.key}. In addition it offers strong resource configuration features to work with different environments. It also supports web-services

REQ: Retrieving properties in my java app. collected by "PropertyPlaceholderConfigurer" -- properties are stored in a database as key/value

PUsing Spring 3.2.0.release and JDK 1.6. I've a standalone Java program (NOT running inside tomcat etal) and I'm loading properties from a database.
I've used this excellent article as a base and it works perfectly. Using the PropertiesPrinter bean (defined there) as a base and adding getters I can do stuff like getFileLocation(), getPetDogsName() but then I need to have/create setter/getters for every property.
What I would like to have is a Spring Bean or normal Java class called DatabaseProperties with a method like getProperty("filelocation"); which I can use in my application (main)and so I can retrieve/get the value of the property filelocation which is somewhere inside the information collected by PropertyPlaceholderConfigurer.
I've done a lot of digging but can't seem to find the information I need or at least I'm not able to combine the gathered info into a working program as I'm not fluent with Spring....
Any hint/pointers/urls/code is higly appreciated. It's probably relative easy but it is still out of reach for me atm.
One solution for reading values set by the PropertyPlaceholderConfigurer, is to use the #Value annotation rather than a method for setting class member variables:
class MyClass {
#Value("${file.location}")
private String fileLocation;
...
}

intellij not resolving el variables within JSP code inspection or autocomplete

To summarize the answer shown here Code assist in (jsp /jstl) view for Spring MVC model objects in Eclipse
is not working for me at all, is there a setting that I need to change ?
I have just downloaded the sample spring-mvc-showcase on github, and it doesn't work out of the box on that project (with either 11.1.3 or EAP 12 version both full enterprise editions), see below (I have no idea where it gets formBean from) :
Here is an example from my own project,the screen shot below (bottom frame) shows my controller adding a string attribute to model and returning correct view name. I would then expect shopString to be offered up as autocomplete option when editing that view, however it is not :
sg is a javascript variable - so great it should be there, but where is "shopString" ?.
Is there a setting I need to change or something else I am missing to get this functionality (using 11.1.3 enterprise edition with all the spring plugins).
It is also failing on spring specific variables :
IS their an open source (one of the spring tutorial projects?) where this definitely works ... or is there a setting I need change in my Intellij install (I have tested with a brand new download of the version 12 EAP) ?
One more screenshot below shows all my spring coifg files set up correctly via autodetection, but the code inspections fails ... this is the spring-mvc-showcase project :
There's a standard way to do this, which is not IntelliJ-specific.
<jsp:useBean id="someModel" scope="request" type="foo.bar.SomeModelClass"/>
The type attribute here does not need to be a concrete class, it can be an interface type as well. Typically you'd put these declarations at the start of your JSP/JSPX files, to provide something like a "declaration of model inputs".
Using JSPs in such a declarative way was recommended in the original book on Spring, in fact (Expert One-on-One J2EE Design and Development.). IntelliJ has been providing full code completion for such pages since at least 7 years.
Note that there are additional relevant convenience features in IntelliJ: if an EL variable reference is marked as undefined, you can press Alt-Enter to select a QuickFix, which will insert a declaration like above. It will even try to figure out the actual type, based on the properties you're accessing.
As I understand Spring, there is no declaration for definitions of variables that you may put into your model. The call model.addAttribute() may add an object to the model, either identified by a parameter or automatically generated by the class name of the object.
So imagine the following case where you have more than one method:
#RequestMapping("foo") public String foo(Model model) {
model.addAttribute("model", new Foo());
return new Random().nextBoolean() ? "page" : "someOtherPage";
}
#RequestMapping("bar") public String bar(Model model) {
model.addAttribute("model", new Bar());
model.addAttribute("model", new Foo());
model.addAttribute("model", new Bar());
return new Random().nextBoolean() ? "page" : "someOtherPage";
}
and the JSP would be something like
<c:out ${model.value} />
Since there is no proper mapping of which controllers may under some circumstances forward to which views, nor what exactly lies within the model, your IDE has no real chance to provide you with proper information.
But to support the IDE in suggesting you some useful information, you can use type hints. Therefore, you have to copy the whole reference of an object, e. g. foo and add a JSP comment like:
<%--#elvariable id="foo" type="com.mycompany.SomeObject"--%>
The warning will vanish and the full IDE support is on your side, allowing you to traverse the fields of foo.
One of the nicest things is that the unused getter warnings will vanish, too. You can directly call the show usages action directly from the JSP or the POJO.
This also works with JSF and particularly within JSF components. Pretty neat feature to have this kind of code completion, showing warnings and errors.
Hope that helps you with your switch to Intellij Idea.
Edit: I also reported this finding to a friend wo wrapped the whole thing into a nice blog entry. Maybe you're interested in reading it: open link
This got fixed in the latest release of intellij 122.694
I faced with similar issue when start writing my own interceptor. Problem was that I start using refference in my view resolver configuration
don't use contruction like this
<bean id="internalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" ref="prefix"/>
<property name="suffix" ref="suffix"/>
</bean>-

How do I make session/cache variables available to my main.html template?

I'm using Play Framework and setting a cache value as such:
String sessionId = Scope.Session.current().getId();
Cache.set(sessionId + "_user", "Doser");
and I want to ouput the value in my main.html without adding the value to every single controller in my application.
How do I achieve this in Play?
The other option you have for this, is to create an action in your controller that uses the #Before annotation, and then add the value using renderArgs().
I answered a previous question which I think is very similar to your requirements.
Does Play Framework support "snippets"?
You should also be aware that all session variables are available within your template, by default. You can see all the implicit objects that are available in the template in the Template Documentation here -- http://www.playframework.org/documentation/1.2.2/templates#implicits.
I need to stop answering my own questions.
I've created a tag as described in the link below, and it works perfectly:
http://www.playframework.org/documentation/1.2.2/templates#tags

Why do we have to specify "methodParams" in struts validation plugin?

I have checked the validator source code and the examples of user-defined rule in the book "Jakarta Struts Live." When people define validator rules in validator-rules.xml, the following declaration for "methodParams" seems to be the only choice:
methodParams="java.lang.Object,
org.apache.commons.validator.ValidatorAction,
org.apache.commons.validator.Field,
org.apache.struts.action.ActionMessages,
org.apache.commons.validator.Validator,
javax.servlet.http.HttpServletRequest"
Okay, my question is that if this is the only possibility, why do we bother to specify it? Or please provide me with an example if this declaration is not the only choice.
Thanks.
Please take a look at this link:
http://struts.apache.org/1.1/userGuide/dev_validator.html
It says:
The methodParams attribute takes a
comma separated list of class names.
The method attribute needs to have a
signature complying with the above
list. The list can be comprised of any
combination of the following:
java.lang.Object - Bean validation is
being performed on.
org.apache.commons.validator.ValidatorAction
- The current ValidatorAction being performed.
org.apache.commons.validator.Field -
Field object being validated.
org.apache.struts.action.ActionErrors
- The errors objects to add an ActionError to if the validation
fails.
javax.servlet.http.HttpServletRequest
- Current request object. javax.servlet.ServletContext - The
application's ServletContext.
org.apache.commons.validator.Validator
- The current org.apache.commons.validator.Validator
instance. java.util.Locale - The
Locale of the current user.
It seems that "methodParams" can be any combination of the listed types, and Strurs Validator Plugin will inject the instances into the validation function call accordingly.
"methodParams" can be changed.

Categories

Resources