Spring Boot with Mongo DB ( Delete Data)

pom.xml

<project xmlns=”http://maven.apache.org/POM/4.0.0&#8243; xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance&#8221;
xsi:schemaLocation=”http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd”&gt;
<modelVersion>4.0.0</modelVersion>
<groupId>com.subu</groupId>
<artifactId>SpringWithMongoDB</artifactId>
<packaging>war</packaging>
<parent>
<groupId>io.spring.platform</groupId>
<artifactId>platform-bom</artifactId>
<version>2.0.1.RELEASE</version>
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.7</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb</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>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
</dependency>
<dependency>
<groupId>commons-beanutils</groupId>
<artifactId>commons-beanutils</artifactId>
</dependency>
<dependency>
<groupId>commons-collections</groupId>
<artifactId>commons-collections</artifactId>
</dependency>
<dependency>
<groupId>org.freemarker</groupId>
<artifactId>freemarker</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-xml</artifactId>
</dependency>
<dependency>
<groupId>org.codehaus.woodstox</groupId>
<artifactId>woodstox-core-asl</artifactId>
<version>4.4.1</version>
</dependency>
<!– <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-security</artifactId>
</dependency> –>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
</plugins>
<finalName>SpringWithMongoDB</finalName>
</build>
<repositories>
<repository>
<id>spring-releases</id>
<name>Spring Releases</name>
<url>https://repo.spring.io/libs-release</url&gt;
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>spring-releases</id>
<name>Spring Releases</name>
<url>https://repo.spring.io/libs-release</url&gt;
</pluginRepository>
</pluginRepositories>
</project>

 

Application.java

package com.subu;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.context.web.SpringBootServletInitializer;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;

@SpringBootApplication
@Configuration
@ComponentScan
@EnableAutoConfiguration
@EnableScheduling
public class Application extends SpringBootServletInitializer {

public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}

@Override
protected SpringApplicationBuilder configure(
SpringApplicationBuilder application) {
return application.sources(Application.class);
}

private static Class<Application> applicationClass = Application.class;

}

 

NameController.java

package com.subu.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoOperations;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import com.subu.entity.MultiListType;
import com.subu.entity.NameType;
import com.subu.repository.DBRepository;

@RestController
public class NameController {

@Autowired
private DBRepository dbRepository;

@Autowired
private MongoOperations mongoOperations;
@RequestMapping(value = “/restservice/delete/name”, method = RequestMethod.POST, produces = { MediaType.APPLICATION_XML_VALUE }, headers = “Accept=application/xml”)
public ResponseEntity<?> deleteNameDetails(@RequestBody MultiListType requestParameter,final HttpServletRequest request) throws Exception {

List<NameType> msg = requestParameter.getNameType();
for (NameType nameType : msg) {
Query query = new Query();
query.addCriteria(Criteria.where(“name”).is(nameType.getName()));
NameType nameTypes = mongoOperations.findOne(query, NameType.class);
if (nameTypes == null)
nameTypes = new NameType();
nameTypes.setName(nameType.getName());
nameTypes.setSurname(nameType.getSurname());
nameType.setAddress(nameType.getAddress());
nameType.setPhone(nameType.getPhone());
dbRepository.delete(nameTypes);
}
Map<String, Object> dataMap = new HashMap<>();
dataMap.put(“message”, “Name Details Deleted successfully”);
return ResponseEntity.ok(dataMap);
}

}

 

MultiListType.java

package com.subu.entity;

import java.util.ArrayList;
import java.util.List;

public class MultiListType {

private List<NameType> nameType;

public List<NameType> getNameType() {
if (nameType == null) {
nameType = new ArrayList<>();
}
return this.nameType;
}

}

 

NameType.java

package com.subu.entity;

import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;

@Document(collection = “name”)
public class NameType {

@Id
private String id;
private String name;
private String surname;
private String address;
private String phone;

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getSurname() {
return surname;
}

public void setSurname(String surname) {
this.surname = surname;
}

public String getAddress() {
return address;
}

public void setAddress(String address) {
this.address = address;
}

public String getPhone() {
return phone;
}

public void setPhone(String phone) {
this.phone = phone;
}

}

 

DBRepository.java

package com.subu.repository;

import java.util.List;

import org.springframework.data.mongodb.repository.MongoRepository;
import com.subu.entity.NameType;

public interface DBRepository extends MongoRepository<NameType, String> {

List<NameType> findByName(String name);
}

 

application.properties

spring.data.mongodb.database=test
spring.data.mongodb.host=localhost
spring.data.mongodb.port=27017

 

Request

deletedetails

 

Data Before Deleting

beforedelete

Data After Deleting

afterdelete

Advertisements

Reverse XML tags using java

Many times we need to reverse the xml tag

</Request>
</name>
</address>
<pincode>123456</pincode>
<area>ABC</area>
<flat>XYZ</flat>
<address>
<name>
<Request>

as below :

<Request>
<name>
<address>
<flat>XYZ</flat>
<area>ABC</area>
<pincode>123456</pincode>
</address>
</name>
</Request>

 

Steps:

 

reverserequest

  1. Create a xml with reverse request
  2. Click on ReverseXMLrequest.bat

 

      3. Enter the source file and destination file path

reverserequest_batch

4) The File with correct xml will be generated in destination path.

 

Code:

==================================

  1. Create a Mavenized project.
  2. Add dependencies jars like commons-io
  3. pom.xml

<project xmlns=”http://maven.apache.org/POM/4.0.0&#8243; xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance&#8221; xsi:schemaLocation=”http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd”&gt;
<modelVersion>4.0.0</modelVersion>
<groupId>com.subhasish</groupId>
<artifactId>ReverseXMLRequest</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.4</version>
</dependency>
</dependencies>

<repositories>
<repository>
<id>maven2-repository.dev.java.net</id>
<name>Java.net repository</name>
<url>http://download.java.net/maven/2</url&gt;
</repository>
</repositories>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>attached</goal>
</goals>
<phase>package</phase>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<appendAssemblyId>false</appendAssemblyId>
<archive>
<manifest>
<mainClass>com.subhasish.ReverseXMLRequest</mainClass>
</manifest>
</archive>
</configuration>
</execution>
</executions>
</plugin>

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
</plugins>
<finalName>ReverseXMLRequest</finalName>
</build>
</project>

 

4) ReverseXMLRequest.java

package com.subhasish;

import org.apache.commons.io.input.*;
import org.w3c.dom.Node;
import org.w3c.dom.bootstrap.DOMImplementationRegistry;
import org.w3c.dom.ls.DOMImplementationLS;
import org.w3c.dom.ls.LSOutput;
import org.w3c.dom.ls.LSSerializer;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import java.io.*;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import javax.xml.transform.*;
import javax.xml.parsers.*;
public class ReverseXMLRequest {

public static void main(String[] args) throws ParserConfigurationException,IOException,SAXException,TransformerException{
String ch;
String destinationPath=args[1];
String tempPath=”C://temp.xml”;
try(ReversedLinesFileReader fr = new ReversedLinesFileReader(new File(args[0]));)
{
Path path = Paths.get(tempPath);
Files.deleteIfExists(path);
Files.createFile(path);
do {
ch = fr.readLine();
Files.write(path, ch.getBytes(),StandardOpenOption.APPEND);

} while (ch != null);
}
catch(Exception e)
{
formatXML(tempPath,destinationPath);
}}

public static void formatXML(String path,String destinationPath) throws IOException
{
Path deletePath = Paths.get(path);
try
{
final InputSource src = new InputSource(new FileInputStream(new File(path)));
final Node document = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(src).getDocumentElement();
final DOMImplementationRegistry registry = DOMImplementationRegistry.newInstance();
final DOMImplementationLS impl = (DOMImplementationLS) registry.getDOMImplementation(“LS”);
final LSSerializer writer = impl.createLSSerializer();
writer.getDomConfig().setParameter(“format-pretty-print”,Boolean.TRUE);
LSOutput lsOutput = impl.createLSOutput();
lsOutput.setEncoding(“UTF-8”);
Writer stringWriter = new StringWriter();
lsOutput.setCharacterStream(stringWriter);
writer.write(document, lsOutput);
String result = stringWriter.toString();
Path formattedpath = Paths.get(destinationPath);
Files.createFile(formattedpath);
Files.deleteIfExists(deletePath);
Files.write(formattedpath, result.getBytes(),StandardOpenOption.APPEND);
System.out.println(“DONE”);
} catch (Exception e)
{
Files.deleteIfExists(deletePath);
}
}}

 

5)  ReverseXMLrequest.bat

@echo off
set /p sourceinput=”Enter Source Path::”
set /p destinput=”Enter Destination Path::”
java -jar ReverseXMLRequest.jar %sourceinput% %destinput%
pause