Sunday, May 17, 2015

MongoDB aggregate: Cursor example

Sometime necessary to access the MongoDb cursor and process the cursor data one by one. I had the similar situation

var cur = db.users.aggregate(
{$group : {
_id : {un:{userName: '$userName'}},
count : {$sum : 1}
}},
{$match: {
count : {$gt: 1}
}}

)
cur['result'].forEach(function(doc){
var users = db.users.find({userName:doc._id.un.userName});

users.forEach(function(doc){
if (doc.status == 'archived'){ print(doc.userName)}
});

});

as shown in the above, I create the cursor with the aggregation of all the users who’s user name is occurred more than once. Then that cur is iterated (using forEach function) and execute another database find to find the users whose status is ‘archived’ and only print them.

Sunday, March 08, 2015

RAD 2– Spring for Vaadin without Spring Boot

In my previous blog RAD 1 – Spring for Vaadin, I have introduced the recommended way of integrating Spring and Vaadin. In this blog, I am going to use the alternative way which is without Spring Boot explained in the article“Getting started with Vaadin Spring without Spring Boot”.  This is the hardest way. I’ve configured this in the Intellij Idea 14.

  1. Create a maven archetype as explained in the Creating a Project with Intellij Idea:2.8.3 Create Maven Project.
  2. Create a new folder WEB-INF under the webapp folder.
  3. Create a applicationContext.xml under the WEB-INF folder.
  4. Now you have to add the Spring Facet to the project. You have to add the Spring Context created in the step 3. However, I first add the Spring module.

image

My applicationContext.xml is as follows

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.1.xsd">

<bean class="au.com.blogspot.ojitha.MyUI.MyConfiguration"/>
<context:component-scan base-package="au.com.blogspot.ojitha"/>

</beans>

Here is the configuration file

package au.com.blogspot.ojitha;

import javax.servlet.annotation.WebListener;
import javax.servlet.annotation.WebServlet;

import com.vaadin.annotations.Theme;
import com.vaadin.annotations.VaadinServletConfiguration;
import com.vaadin.annotations.Widgetset;
import com.vaadin.server.VaadinRequest;
import com.vaadin.server.VaadinServlet;
import com.vaadin.spring.annotation.EnableVaadin;
import com.vaadin.spring.annotation.SpringUI;
import com.vaadin.spring.servlet.SpringAwareVaadinServlet;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Label;
import com.vaadin.ui.UI;
import com.vaadin.ui.VerticalLayout;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.context.ContextLoaderListener;

/**
*
*/
@Theme("valo")
//@Widgetset("au.com.blogspot.ojitha.MyAppWidgetset")
@SpringUI("")
public class MyUI extends UI {

@Override
protected void init(VaadinRequest vaadinRequest) {
final VerticalLayout layout = new VerticalLayout();
layout.setMargin(true);
setContent(layout);

Button button = new Button("Click");
button.addClickListener(new Button.ClickListener() {
@Override
public void buttonClick(ClickEvent event) {
layout.addComponent(new Label("Thank you for clicking"));
}
});
layout.addComponent(button);

}

@WebServlet(urlPatterns = "/*", name = "MyUIServlet", asyncSupported = true)
//@VaadinServletConfiguration(ui = MyUI.class, productionMode = false)
public static class MyUIServlet extends SpringAwareVaadinServlet {
}

@WebListener
public static class MyContextLoaderListener extends ContextLoaderListener{}

@Configuration
@EnableVaadin
public static class MyConfiguration {}


}

Here the pom.xml file

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>au.com.blogspot.ojitha</groupId>
<artifactId>vaadinex2</artifactId>
<packaging>war</packaging>
<version>1.0-SNAPSHOT</version>
<name>vaadinex2</name>

<properties>
<vaadin.version>7.4.0</vaadin.version>
<vaadin.plugin.version>${vaadin.version}</vaadin.plugin.version>
<jetty.plugin.version>9.2.3.v20140905</jetty.plugin.version>
<project.source.version>1.7</project.source.version>
<project.target.version>1.7</project.target.version>
<project.encoding>UTF-8</project.encoding>
</properties>

<repositories>
<repository>
<id>vaadin-addons</id>
<url>http://maven.vaadin.com/vaadin-addons</url>
</repository>
<repository>
<id>vaadin-snapshots</id>
<url>https://oss.sonatype.org/content/repositories/vaadin-snapshots/</url>
<releases>
<enabled>false</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>

<dependencies>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.0.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.vaadin</groupId>
<artifactId>vaadin-server</artifactId>
<version>${vaadin.version}</version>
</dependency>
<dependency>
<groupId>com.vaadin</groupId>
<artifactId>vaadin-push</artifactId>
<version>${vaadin.version}</version>
</dependency>
<dependency>
<groupId>com.vaadin</groupId>
<artifactId>vaadin-client-compiled</artifactId>
<version>${vaadin.version}</version>
<!--scope>provided</scope-->
</dependency>
<!--
Needed when using the widgetset optimizer (custom ConnectorBundleLoaderFactory).

For widgetset compilation, vaadin-client-compiler is automatically added on the
compilation classpath by vaadin-maven-plugin so normally there is no need for an
explicit dependency.
-->
<!--
<dependency>
<groupId>com.vaadin</groupId>
<artifactId>vaadin-client-compiler</artifactId>
<version>${vaadin.version}</version>
<scope>provided</scope>
</dependency>
-->
<dependency>
<groupId>com.vaadin</groupId>
<artifactId>vaadin-themes</artifactId>
<version>${vaadin.version}</version>
</dependency>
<dependency>
<groupId>com.vaadin</groupId>
<artifactId>vaadin-spring</artifactId>
<version>1.0.0.alpha2</version>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.0</version>
<configuration>
<encoding>${project.encoding}</encoding>
<source>${project.source.version}</source>
<target>${project.target.version}</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>2.6</version>
<configuration>
<encoding>${project.encoding}</encoding>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.3</version>
<configuration>
<failOnMissingWebXml>false</failOnMissingWebXml>
<!-- Exclude some unnecessary files generated by the GWT compiler. -->
<packagingExcludes>WEB-INF/classes/VAADIN/gwt-unitCache/**,
WEB-INF/classes/VAADIN/widgetsets/WEB-INF/**</packagingExcludes>
</configuration>
</plugin>
<plugin>
<groupId>com.vaadin</groupId>
<artifactId>vaadin-maven-plugin</artifactId>
<version>${vaadin.plugin.version}</version>
<configuration>
<extraJvmArgs>-Xmx512M -Xss1024k</extraJvmArgs>
<webappDirectory>${basedir}/target/classes/VAADIN/widgetsets</webappDirectory>
<draftCompile>false</draftCompile>
<compileReport>false</compileReport>
<style>OBF</style>
<strict>true</strict>
</configuration>
<executions>
<execution>
<goals>
<goal>update-widgetset</goal>
<goal>compile</goal>
<!-- disabled by default to use on-the-fly theme compilation -->
<!-- <goal>compile-theme</goal> -->
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<version>2.4</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-clean-plugin</artifactId>
<version>2.6.1</version>
<!-- Clean up also any pre-compiled themes -->
<configuration>
<filesets>
<fileset>
<directory>src/main/webapp/VAADIN/themes</directory>
<includes>
<include>**/styles.css</include>
<include>**/styles.scss.cache</include>
</includes>
</fileset>
</filesets>
</configuration>
</plugin>

<!-- The Jetty plugin allows us to easily test the development build by
running jetty:run on the command line. -->
<plugin>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
<version>${jetty.plugin.version}</version>
<configuration>
<scanIntervalSeconds>2</scanIntervalSeconds>
</configuration>
</plugin>
</plugins>

<pluginManagement>
<plugins>
<!--This plugin's configuration is used to store Eclipse m2e settings only. It has no influence on the Maven build itself.-->
<!-- TODO Remove when http://dev.vaadin.com/ticket/14924 is resolved -->
<plugin>
<groupId>org.eclipse.m2e</groupId>
<artifactId>lifecycle-mapping</artifactId>
<version>1.0.0</version>
<configuration>
<lifecycleMappingMetadata>
<pluginExecutions>
<pluginExecution>
<pluginExecutionFilter>
<groupId>com.vaadin</groupId>
<artifactId>
vaadin-maven-plugin
</artifactId>
<versionRange>[7.1.11,)</versionRange>
<goals>
<goal>resources</goal>
<goal>update-widgetset</goal>
<goal>compile</goal>
<goal>compile-theme</goal>
</goals>
</pluginExecutionFilter>
<action>
<ignore></ignore>
</action>
</pluginExecution>
</pluginExecutions>
</lifecycleMappingMetadata>
</configuration>
</plugin>
</plugins>
</pluginManagement>

</build>

</project>

Comments are welcome.

RAD–1: Spring for Vaadin 7.4.0

My whole effort to create rapid application development (RAD) stack for Java Web Development. I chose Vaadin for front end development. My development environment is Intellij Idea 14. In this example, I am going to use the recently released Vaadin Spring Add on.

As a first, create a spring boot web application. You have to select the “war” option for the packaging and select the option “web” for the dependencies.

        <dependency>
<groupId>com.vaadin</groupId>
<artifactId>vaadin-spring-boot</artifactId>
<version>1.0.0.alpha2</version>
</dependency>
<dependency>
<groupId>com.vaadin</groupId>
<artifactId>vaadin-themes</artifactId>
<version>7.4.0</version>
</dependency>
<dependency>
<groupId>com.vaadin</groupId>
<artifactId>vaadin-client-compiled</artifactId>
<version>7.4.0</version>
</dependency>

It is important to add the above dependencies to the newly created project.  Let us create just a simple view

package au.com.blogspot.ojitha.vaadin.view;

import com.vaadin.annotations.Theme;
import com.vaadin.server.VaadinRequest;
import com.vaadin.spring.annotation.SpringUI;
import com.vaadin.ui.Label;
import com.vaadin.ui.UI;

/**
* Created by Ojitha on 8/03/2015.
*/
@Theme("valo")
@SpringUI("")
public class RootUI extends UI {

@Override
protected void init(VaadinRequest vaadinRequest) {
getPage().setTitle("Root UI");
setContent(new Label("Hello ojtha, World!"));
}
}

Application is ready to deploy to tomcat 8. You can configure the tomcat in the File->Settings->Build, Execution, Deployment->Application Server. After that you can create a new “Run Configuration…” and create tomcat local configuration and add the web application (war or exploded) to that. Good to note that, under the Deployment tab, Application context should be specify. 

Here the complete pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>au.com.blogspot.ojitha</groupId>
<artifactId>vaadinex1</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>

<name>vaadin-example-1</name>
<description>Vaadin spring example</description>

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.2.2.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>

<repositories>
<repository>
<id>vaadin-addons</id>
<url>http://maven.vaadin.com/vaadin-addons</url>
</repository>
</repositories>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<start-class>au.com.blogspot.ojitha.vaadin.VaadinExample1Application</start-class>
<java.version>1.8</java.version>
</properties>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!--dependency>
<groupId>com.vaadin</groupId>
<artifactId>vaadin-spring</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency-->
<!--dependency>
<groupId>javax.el</groupId>
<artifactId>javax.el-api</artifactId>
<version>2.2.4</version>
</dependency-->
<dependency>
<groupId>com.vaadin</groupId>
<artifactId>vaadin-spring-boot</artifactId>
<version>1.0.0.alpha2</version>
</dependency>
<dependency>
<groupId>com.vaadin</groupId>
<artifactId>vaadin-themes</artifactId>
<version>7.4.0</version>
</dependency>
<dependency>
<groupId>com.vaadin</groupId>
<artifactId>vaadin-client-compiled</artifactId>
<version>7.4.0</version>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>

<!--plugin>
<groupId>com.vaadin</groupId>
<artifactId>vaadin-spring-boot</artifactId>
</plugin-->
</plugins>
</build>

</project>

Comments are welcome.