Creating a new node and putting object beans under it java magnolia - java

I am working in magnolia and the backend is in java. I need to check whether a node is present in the component that I do with these steps
if (!MgnlContext.getJCRSession("repository").nodeExists(path)) {
MgnlContext.getJCRSession ("repository").getRootNode().addNode("nodeName", "mgnl:content");
}
now I need to put a bean (java object bean) inside this node. for eg: as it should come as a content node under it.
Can anyone suggest any methods to do it better?

To map any java been to content in repository, your best option in Magnolia is to use the Node2Bean. It allows you to map the content of your java bean/pojo to JCR Node and back. More details in documentation.

Fixed by denoting a #Field(path = true) in the bean and set that variable in the node path which is created using created. – MgnlContext.getJCRSession ("repository").getRootNode().addNode("nodeName", "mgnl:content"). so by doing this when i persisted the bean it went inside the node which i created .

In order to manage nodes/properties in Magnolia you can also use these utils:
info.magnolia.jcr.util.NodeUtil
info.magnolia.jcr.util.SessionUtil
info.magnolia.jcr.util.PropertyUtil

Related

Object Spy using Java

I am working on a project in Java where I should be able to launch a site and then recognize a browser Web Elements with a Mouse Over, like the Object Spy in QTP. The Elements that I hope to get are Name, Class Name, ID, Tagname, Linktext,Partial LinkText, CSS Xpath..etc..
I am kind of lost on how to bridge the Browser and the Application.Can anyone help?
Thank you in Advance.
Inject a javascript/Jquery code into the web page to capture element properties. You can pass the element back to Java class and catch it in a org.w3c.dom.HTMLElement object. Using the methods in the object, you can retrieve all the attributes corresponding to the Element.

CQ5 - Sling accessing nodes

I'm a complete newb to this so I apologize in advance. I've got an instance of CQ5 set up
and I can't figure out how to access specific nodes. So say I have a component with the
path:
/project/components/content/leftsidebar
but I want to access properties of another node here:
/content/dam/campaign
I know the properties.get method works but only if your within that node
properties.get("title", "placeholder");
I'm a complete newb so please post code samples were possible.
Thanks!
You'd want to use the ResourceResolver in order to get the resource that you're looking for. From there, you can adapt it to a ValueMap & read its properties:
ResourceResolver resourceResolver = slingRequest.getResourceResolver();
Resource campaignResource = resourceResolver.getResource("/content/dam/campaign");
ValueMap campaignProperties = campaignResource.adaptTo(ValueMap.class);
String title = campaignProperties.get("title", "placeholder");
You can read more about accessing properties on the Apache Sling website. Remember, CQ5 is Sling under the hood, so it's a great resource & you're still a level of abstraction above accessing the JCR directly.

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;
...
}

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

Java - Setting context attributes (ServletContextListener)

I read a properties-file at the webapplication startup phase (contextInitialized()) and I started to think about how to make these settings 'visible' to the servlets. Do I need to loop through the keys and add each and every one to the context, like this
Iterator i = settings.keySet().iterator();
while (i.hasNext()) {
key = (String) i.next();
value = (String) settings.get(key);
context.setAttribute(key, value);
}
or are there better methods?
Thank you!
/Adam
why not store the entire contents in your servlet context?
context.setAttribute("mySettings", settings);
setAttribute's signature is:
public void setAttribute(String name, Object object)
Have you considered the possibility of defining the settings in web.xml?
Also, if that's not possible, use generics if possible:
String key = null;
Iterator<String> i = settings.keySet().iterator();
while (i.hasNext())
context.setAttribute(key = i.next(), settings.get(key));
I've been toying with an idea:
In the context initialized method, I've planned to create just one global object for the settings. Much like toolkit proposed. But instead of setting context attributes for each key/attribute/setting, would it be a terrible idea to add a settings container/wrapper object? I'm thinking this class would be responsible for holding (static?) classes of module settings. This way I can get typed references like so
//ExampleServlet.java
Settings settings = (Settings)context.getAttribute("application.settings");
String color = settings.getModule1().getColor();
String font = settings.getModule1().getFont();
int blogs = settings.getModule2().getActiveBlogCount();
Throughout the code I'll have to remember only one attribute key, the one for the entire settings container. Less risk of typos which could cause rumtime exceptions!
It will also make it easy to rename attributes.
What do you think?
/Adam
What about using the JNDI context. JNDI is a more common way to pass properties to a webapp.
Any Properties may be specified in the META-INF/context.xml for tomcat or any application specific setup.
It's something that I have contemplated, setting the entire properties object as a context attribute.
If I do not go this route, are there any guidelines for how to name these attributes or do you feel that "application.settings" or "myBlog.settings"? How do you group keys? Would this be okay:
application.module1.color=black
application.module1.font=arial
I feel, in a way, that it could become a burden to maintain such an application where the property keys are spread throughout the code? Should another developer rename a property in the properties file, we'll know only when running the application (if/when/what referenced the old key). Right?
I'll have to lookup JNDI.

Categories

Resources