Friday 5 July 2013

NoSQL, DBRefs : MongoDB

> db.customers.findOne()
{
        "_id" : ObjectId("4ef4a61a90eec3e3c748263c"),
        "uid" : 1,
        "name" : "Andrey",
        "lastname" : "Knupp Vital"
}
> db.orders.findOne()
{
        "_id" : ObjectId("4ef4a66490eec3e3c748263d"),
        "oid" : 1,
        "uid" : 1,
        "price" : "149.90"
}
> db.orders.remove()
> order = { oid : 1 , price : 149.90 , uid : new DBRef ( 'customers' , ObjectId("4ef4a61a90eec3e3c748263c") ) } ;
{
        "oid" : 1,
        "price" : 149.9,
        "uid" : {
                "$ref" : "customers",
                "$id" : ObjectId("4ef4a61a90eec3e3c748263c")
        }
}
> db.orders.save(order)
> order.uid.fetch()
{
        "_id" : ObjectId("4ef4a61a90eec3e3c748263c"),
        "uid" : 1,
        "name" : "Andrey",
        "lastname" : "Knupp Vital"
}

Thursday 4 July 2013

Multiple File Uploading With Spring

//#####################################Screen Shot









//##################################### Maven Dependency

<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3</version>
</dependency>

<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.4</version>
</dependency>

//##################################### Jsp Code

 <script type="text/javascript" src="http://www.technicalkeeda.com/js/javascripts/plugin/jquery.js"></script>
<script type="text/javascript">
var i=1;
$(document).ready(function(){
$('#add').click(function(){
$('#files').append('<input type="file" name="file['+i+']"/><br>');
i++;
});
$('#upload').click(function(){
$('#files').append('<input type="hidden" name="count" value='+i+'>');
console.log(i);
});
});
</script>

 -------------------------------------------------------------------------------------------------

<p>Click Add Button To Browse More Files</p>
<form action="uploadMultipleFiles" enctype="multipart/form-data" method="post">
<div id="files">
<input type="file" name="file[0]" />
<input type="submit" value="upload" id="upload"/>
<input type="button" id="add" value="Add" /><br>
</div>
</form>


//##################################### Controller Code

 @RequestMapping("/uploadMultipleFiles")
public String uploadFiles(HttpServletRequest request) throws IOException{

System.out.println("inside upload multiple files..");
int total=Integer.parseInt(request.getParameter("count"));
//System.out.println(total);
MultipartHttpServletRequest req=(MultipartHttpServletRequest)request;
for(int i=0;i<total;i++){
MultipartFile files=req.getFile("file["+i+"]");
String filenameToCreate="D:\\harsh.patel\\SpringFileUpload\\UploadedFiles\\"+files.getOriginalFilename();
System.out.println(filenameToCreate);
File file = new File(filenameToCreate);
FileUtils.writeByteArrayToFile(file, files.getBytes());
}
System.out.println("after upload multiple files");
return "result";

}
 

Simple File Upload in Spring



//##################################### Maven Dependency

<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3</version>
</dependency>

<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.4</version>
</dependency>

//##################################### Jsp Code

<form action="uploadRequest" enctype="multipart/form-data" method="post">
<input type="file" id="file" name="file"/>
<input type="submit" value="upload" />
</form>

//######################################  Controller Code
@RequestMapping("uploadRequest")
public String upload(HttpServletRequest request) throws IOException{
System.out.println("inside upload");
MultipartHttpServletRequest req = (MultipartHttpServletRequest) request;
MultipartFile files = req.getFile("file");
String filenameToCreate="Path Up to Folder\\"+files.getOriginalFilename();
//(Don't forgot to put \\ )
System.out.println(filenameToCreate);
File file = new File(filenameToCreate);
FileUtils.writeByteArrayToFile(file, files.getBytes());
System.out.println("after upload");
return "result";
}

Get all error message of hibernate annotation in Map (Field,Error Message)


import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import javax.validation.ConstraintViolation;
import javax.validation.Validation;
import javax.validation.Validator;
import javax.validation.ValidatorFactory;
import javax.validation.constraints.NotNull;

import org.hibernate.validator.constraints.Email;
import org.hibernate.validator.constraints.Length;
import org.hibernate.validator.constraints.NotBlank;
import org.hibernate.validator.constraints.NotEmpty;
import org.springframework.data.mongodb.core.mapping.Document;

@Document(collection = "user")
public class User1 extends AbstractDocument {

public User1() {

}

public User1(String displayName, String username, String password) {
this.displayName = displayName;
this.username = username;
this.password = password;
}

@NotNull
@NotBlank
@NotEmpty
@Length(min = 6, max = 8)
private String displayName;

@NotNull
@NotBlank
@NotEmpty
@Length(min = 6, max = 8)
private String username;

@NotNull
@NotBlank
@NotEmpty
@Email
@Length(min = 6, max = 8)
private String password;

public String getDisplayName() {
return displayName;
}

public void setDisplayName(String displayName) {
this.displayName = displayName;
}

public String getUsername() {
return username;
}

public void setUsername(String username) {
this.username = username;
}

public String getPassword() {
return password;
}

public void setPassword(String password) {
this.password = password;
}

@Override
public String toString() {
return "User [displayName=" + displayName + ", username=" + username
+ ", password=" + password + ", toString()=" + super.toString()
+ "]";
}

public static void main(String[] args) {

User1 user = new User1("", "", "qqq");

ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
Validator validator = factory.getValidator();

Set<ConstraintViolation<User1>> constraintViolations = validator
.validate(user);

System.out.println("size:" + constraintViolations.size());

Map<String, String> map = new HashMap<String, String>();

for (ConstraintViolation<User1> obj : constraintViolations) {

String key = obj.getPropertyPath().toString();

if (!map.containsKey(key)) {
map.put(key, obj.getMessage());
} else {
map.put(key, map.get(key) + "," + obj.getMessage());
}
}
System.out.println(map);

}

}

OUTPUT:

size:6
{password=not a well-formed email address,length must be between 6 and 8, username=may not be empty,length must be between 6 and 8, displayName=length must be between 6 and 8,may not be empty}


Wednesday 3 July 2013

Some interesting Github repositories for Android

Ocassionaly, I like to watch github repositories to see if there are interesting projects for future use, or just to learn. One day I was watching github’s repositories I found some interesting projects for Android that you can find interesting :)

XML Code For @JsonSerialize(include=JsonSerialize.Inclusion.NON_NULL)

<mvc:annotation-driven>
  <mvc:message-converters register-defaults="true">
<bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
<property name="objectMapper">
 <bean class="org.codehaus.jackson.map.ObjectMapper">
<property name="serializationInclusion">
<value type="org.codehaus.jackson.map.annotate.JsonSerialize.Inclusion">NON_NULL</value>
</property>
</bean>
</property>
</bean>
</mvc:message-converters>
</mvc:annotation-driven>