Skip to content

Instantly share code, notes, and snippets.

View Ikhiloya's full-sized avatar

Ikhiloya Ikhiloya

View GitHub Profile
@Ikhiloya
Ikhiloya / controller.java
Created July 24, 2018 02:46
controller for Author-Book One-To-Many Relationship
package com.loya.onetomanybidirectionaldemo.controller;
import com.loya.onetomanybidirectionaldemo.entity.Author;
import com.loya.onetomanybidirectionaldemo.entity.Book;
import com.loya.onetomanybidirectionaldemo.service.AuthorService;
import com.loya.onetomanybidirectionaldemo.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
@Ikhiloya
Ikhiloya / AuthorBookController.java
Created July 24, 2018 02:46
controller for Author-Book One-To-Many Relationship
package com.loya.onetomanybidirectionaldemo.controller;
import com.loya.onetomanybidirectionaldemo.entity.Author;
import com.loya.onetomanybidirectionaldemo.entity.Book;
import com.loya.onetomanybidirectionaldemo.service.AuthorService;
import com.loya.onetomanybidirectionaldemo.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
@Ikhiloya
Ikhiloya / ExcelRead.java
Created July 24, 2018 11:50
Code Snippet to read from an excel file using Apache POI library
private ArrayList<String[]> readXcelDocuments(InputStream file) throws Exception {
ArrayList<String[]> trans = new ArrayList<String[]>();
Workbook workbook = WorkbookFactory.create(file);
FormulaEvaluator evaluator = workbook.getCreationHelper().createFormulaEvaluator();
//Get first sheet from the workbook
Sheet sheet = workbook.getSheetAt(0);
@Ikhiloya
Ikhiloya / ReadFile.java
Created July 24, 2018 11:52
A code snippe t
public static List<String> getFile(String filePath) throws IOException {
BufferedReader br =
new BufferedReader(new FileReader
(filePath));
List<String> lines = new ArrayList<String>();
// Read file line by line
String line = "";
@Ikhiloya
Ikhiloya / ReadFile.java
Last active July 24, 2018 11:52
A code snippet to read from a file
public static List<String> getFile(String filePath) throws IOException {
BufferedReader br =
new BufferedReader(new FileReader
(filePath));
List<String> lines = new ArrayList<String>();
// Read file line by line
String line = "";
@Ikhiloya
Ikhiloya / apachePoiDependency.xml
Created July 24, 2018 11:54
Apache POI dependency to read/write from an Excel file
<dependencies>
<!--Apache POI dependency fo reading and writing microsoft docs -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.17</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.poi/poi-ooxml -->
@Ikhiloya
Ikhiloya / convertMultipartFile.java
Created July 24, 2018 11:56
A helper method to convert a Multipart File to a java.io file
/**
* A helper method to convert a Multipart File to a java.io file
*
* @param file a Multipart file to be converted
* @return a java.io File
* @throws IOException
*/
public static File convertMultipartFile(MultipartFile file) throws IOException {
File convertedFile = new File(file.getOriginalFilename());
convertedFile.createNewFile();
@Ikhiloya
Ikhiloya / SplitWordWithSpecialCharacter.java
Created July 24, 2018 12:00
A simple class that splits a word separated by a special character such as "=" or ",", etc
public class SplitWordWithSpecialCharacter {
public String keyword;
public String word;
public SplitWordWithSpecialCharacter(String line) {
String[] split = line.split("=");
this.keyword = split[0];
this.word = split[1];
}
@Ikhiloya
Ikhiloya / Author.java
Created July 28, 2018 15:08
Author entity fof a one to many bidirectional relationship
@Entity
@Table(name = "Author")
public class Author implements Serializable {
@Column(name = "ID", nullable = false, length = 10)
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@Column(name = "FirstName")
@Ikhiloya
Ikhiloya / MultipartFileToInputStream.java
Created August 7, 2018 09:55
A simple code to convert a multi[part file to an inputStream and extract contents from a multipart file
public void extractMultipartFile(MultipartFile file){
InputStream inputStream =file.getInputStream();
String originalFileName = file.getOriginalFilename();
String fileName = file.getName();
byte[] bytes = file.getBytes();
String contentType = file.getContentType();
}