Skip to main content

Posts

Showing posts from 2012

Using MongoDB Scala $nin

Hi guys, Today I needed to execute a query to filter some records. Lets say I have a Product Collection(Tables are called Collection in MongoDB) and I have following two fields in my product collection for simplicity. Product ---------------- Id sku (store keeping unit) I retrieve data from collection like this. var products = models.Product.find(MongoDBObject("title" -> "atitle") ) .skip(0).limit(10).toList I have filtered data by 'title' already and I have I want 10 records from 0 by specifying skip and limit. Now I have another list ('sku' list) that is list contains sku values. I want to retrieve Product list from DB that is not in SKU list. To do this I have used $nin which is a query operator of MongoDB. var products = models.Product.find( {"sku" $nin descendents}, {MongoDBObject("title" -> "atitle")} ) .skip(offest).limit(10).toList more...

How to save Large html(data) content with Play framewrok, MongoDB and Scala

Hi all, While I was working on a project I had to save large content of data from text area filed in a form. I'm using Play Framework 2.0.2 as a web framework and using scala html templates as views. I have text area filed in my form to add html data (Actually here I'm using ace.js   that is high quality code editor. Ill give brief introduction to ace later). Ok Here I do not post form binding with model class but just think that there is a simple model class lets say Page. Most important thing is the saving part of this program. Here I have posted my save definition(function). def save = Action(parse.urlFormEncoded(maxLength = 1024 * 1000))  { implicit request =           //More code goes here.. I have not posted           Page.save(pageForm)           Redirect(Pages.show(        ...

Upload CSV file using opencsv with Play 2.0.2, Scala and MongoDB

Hi Everyone... Today I'm going to explain you that how we could upload a CSV file with list of products to the MongoDB table. The way I'm describing here might me inefficient but still it is useful. We know that Scala is running on JVM and so that we can use any java library with Scala . I will use OpenCSV as a CSV parser for this program. OpenCSV is a CSV parser library for Java. No matter it is for java here we will use it with Scala. First we should have MongoDB up and running. Include following opencsv dependency in your build file. I'm using SBT as building tool. "net.sf.opencsv" % "opencsv" % "2.1" Here next I will create view file. Name of this file will be 'index.scala.html'. @(message: String)(implicit request: RequestHeader, flash:Flash, l: Lang=Lang("en")) @import play.api.i18n._ @import views.html.helper._ @main(message, null) { //print messages @if(flash.data.contains("message")){ ...

CSV Parsing with Scala

Good new about Scala is, actually not only about Scala but all other JVM based languages is that they can take advantages of Java libraries. Since the Scala is a JVM based programming language its easy to use Java libraries and classes with it. I needed to parse complex CSV file with Scala and create MongoDB object to represent an each row of the CSV file. This would have been easy if I was asked to create MongoDB objects using a JSON file because the JSON is highly supported by the MongoDB. But here I only got complex CSV file with loads of data. First I found some good tutorial about Scala CSV parsing and started my work. Yes I'm using Play framework 2.0.2 with Scala and MongoDB. I found opencsv is the best solution for parsing CSV files with Scala. Opencsv is for Java but as we already know that Scala is a JVM based language we can go with it. First I updated my build configuration file to reflect the application dependencies for opencsv. add this dependencies to your ...

SCALA A Scalable Language...

Scala is called scalable programming language built on the top of Java Virtual Machine. This is good for writing small programming scripts and also complex software systems. Scala runs on the standard Java platform and we can use most of the all Java libraries. Scala Is a combinations of Functional programming language and Object Oriented Language. Scala is easy to use because its simple. Sometimes this simplicity might seems to be as complex. Everything depends on the way of you are looking at it so if you are are a Java programmer it might be easy to use Scala without putting huge effort. Ofcourse Scala is better start as a first programming language. I still love programming in Java but now Im working on a Scala project which is using latest technology stack. Im still new to discuss about Scala but I experienced that the Scala is a really scalable and functional language. Ill here introduce a associative map of Scala language. Semicolons are not needed in Scala and with this...

How to get form value in scala html file in play 2.0.2

Its really easy to get form value in scala html file (in view). First take a look at how form is defined in the controller. I have only added related code snippet. mapping( "courseId" -> nonEmptyText, "name" -> nonEmptyText) Now look at my scala.html file. Following code will accept the course form as a parameter. @(courseForm: Form[Course])(implicit request: RequestHeader, flash: Flash) Now you want to access the 'courseId' in courseForm. First I print the value like this. @courseForm("courseId") But this will not work because it prints list of values because it is reference to the Filed object itself not to the real Id value. Now I do like this. @courseForm("courseId").value This will print the real Id value. (@ is to print something in scala html file)

Play 2.0.2 and Scala Mapping Form to Case Class with List of Objects

Hi guys.. Here I'm going to explain you how to map play form data to a Case class object. Speciality of this case class is it contains list of object as a field. Here is my class Course (Course.scala). Assume that Course can contain number of Subjects. Course ------------> Subjects case class Course( id: ObjectId = new ObjectId, var name: String = "", var description: String = "", var price: Int = 0, var subjects: List[Subject] = List.empty ){ } case class Subject( var name: String = "", var description: String = "" ){} //This line is for database table creation //I'm Using MongoDB so table will be created at runtime object Course extends ModelCompanion[Course, ObjectId] { val collection = mongoCollection("course") //table name course val dao = new SalatDAO[Course, ObjectId](collection = collection) {} } Now I'm going to map this object to play form (play.api.mvc.Form). First I have Co...

Scala : not enough arguments for constructor : ()

not enough arguments for constructor : ( ) Here Im trying to create new Object from case class in scala, but I get this error withing my following code. def getTax(id: ObjectId): models.Tax = { models.Tax.findOne(MongoDBObject("_id" -> id)) .getOrElse(new models.Tax) // this line is generating error } It says that it cannot build a new Tax object without providing the constructor arguments. That is correct, If we want to buld an object we should provide constructor arguments. Just take a look at my Tax class. case class Tax( id: ObjectId = new ObjectId, var name: String, var description: String, var defaultFlag: Boolean, var rate: List[Rate] = List.empty ){ } I have specified default values for id field and rate field but what about name, description and defaultFlag field. There are no default values for them. So if we want to create and object like this 'new models.Tax' (which means without passing arguments to constructor), you should pr...

Simple Java Project with DB

Hi Guys.. Recently I have got question from my friend who is preparing for java test. He asked me to develop simple Java database project to retrieve and display table records from a particular table. Here I have used MySql as the DB and used JSTL as well. To run this project java mysql driver and jstl jar files should be available in the WEB-INF/lib folder. Here is DAO class package tc.com.dao; import java.sql.Connection; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.util.ArrayList; import java.util.List; import tc.com.db.ConnectionFactory; import tc.com.dto.Employee; public class EmployeeDAOImpl { Connection connection; Statement stmt; public EmployeeDAOImpl() { } private static Connection getConnection() throws SQLException, ClassNotFoundException { Connection con = ConnectionFactory. getInstance().getConnection(); return con; } p...

Permission denied to access property 'document' With Facebook

Hi Guys, Recently I was using Facebook application 'RSS Graffiti' to feed rss feeds into my profile. 'RSS Graffiti' excellent application and can be configured within few steps. Here I'm not going to explain how it is configured but I'm going to discuss here different issue. This is not related to RSS Graffiti. I configured RSS Graffiti and feeds are successfully publishing in my profile wall. It publishes brief description and a link to the another site(RSS feeds originally come from this site). Let say Facebook is SiteA and the second site which is opened from the link clicked on the Facebook is SiteB. In Javascript term we can say SiteA(FB) is a opener and SiteB(opening window) is the child window. So Opener opens a child window. Hereafter Ill use these Javascript terms on behalf of non-technical terms. I clicked on the link in the opener and it opens child. And now within child I have a button and clicking on this button it should be refreshed the opener and...

Feedburner depricated.

Hi guys, Today I faced a big problem with Google's feedburner API. Yes that is true that Google has announced that feedburner API is deprecated. http://code.google.com/apis/feedburner/ . But I think it should not mean that its existing way of doing is not stopped. I was using feedburner from couple of months before to feed some rss feeds to a twitter account. From the feedburner they have given excellent API to do this. Actually this was excellent. It functioned really well till 2012-02-22. But today I wanted to modify some of my feeds and logged into my feedburner account and found Socialize menu was missed from the Publicize menu. I could not do anything. I did not try to get support from google becase this post. http://groups.google.com/group/feedburner-statistics/browse_thread/thread/6d5c8d8131ba0936 Only I have to do is finding another good API like feedburner.

Way to Resolve 'no persistent classes found for query class'

Last few days I had experience about this issue. I was developing test application with Spring 3, Hibernate 3.3.1, Maven 3 and Jboss 5.0. I had a class called Employer and it was annotated. Here I have given sample. This is under the package hrm.com.domain. @Entity @Table(name="employer") public class Employer { @Id @Column(name="ID") @GeneratedValue private Integer id; My application was Spring MVC application and Im not going to post all the source code here. I had following configuration files. 1. hibernate.cfg.xml 2. spring-servlet.xml 3. web.xml Here is javax.persistence dependency in my POM.xml file. (I have only post here is important part) javax.persistence persistence-api 1.0 Then I built the application and copy the war file into jboss_home/server/default/deploy directory. Then Server is started. Server is started without any issue. Interested thing happened when I run the application in the web browser. Application needs to list down all the employ...