Tuesday, November 26, 2019

Velocity in Spring boot - focus on velocity template vm file path


There are 2 cases: depend on where your velocity template .vm file is placed.

1. If the .vm file is not placed in the resources directory:

> project
       > src/main/java
       > src/main/resources
       > prjconfig
               > emailTemplate
             > order.vm

In @Configuration class:

@Bean
public VelocityEngine velocityEngine() throws Exception {
    Properties properties = new Properties();
    properties.setProperty("input.encoding", "UTF-8");
    properties.setProperty("output.encoding", "UTF-8");
    properties.setProperty(RuntimeConstants.RESOURCE_LOADER, "file");
    properties.setProperty(RuntimeConstants.FILE_RESOURCE_LOADER_PATH,                                                                                                                                "./prjconfig/emailTemplate");
    properties.setProperty("class.resource.loader.class", "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");
    VelocityEngine velocityEngine = new VelocityEngine(properties);
    return velocityEngine;
}

In service class:

        @Autowired
private VelocityEngine velocityEngine;

        private String prepareRegistrationEmailText(List<OrderData> list) {
VelocityContext context = new VelocityContext();
context.put("alertList", list);
Writer stringWriter = new StringWriter();
                // only file name
velocityEngine.mergeTemplate("order.vm", "UTF-8", context, stringWriter);
String text = stringWriter.toString();
return text;
}

I have tried with "classpath" but didn't work :)

2. If the .vm file is placed in the resources directory:

> project
       > src/main/java
       > src/main/resources    
                > emailTemplate
                 > order.vm 

In @Configuration class:

       @Bean
public VelocityEngine velocityEngineClasspath() throws Exception {
    Properties properties = new Properties();
    properties.setProperty("input.encoding", "UTF-8");
    properties.setProperty("output.encoding", "UTF-8");
    properties.setProperty(RuntimeConstants.RESOURCE_LOADER, "class");
    properties.setProperty("class.resource.loader.class", "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");
    VelocityEngine velocityEngine = new VelocityEngine(properties);
    return velocityEngine;
}

In service class:

        @Autowired
private VelocityEngine velocityEngine;

        private String prepareRegistrationEmailText(List<OrderData> list) {
VelocityContext context = new VelocityContext();
context.put("alertList", list);
Writer stringWriter = new StringWriter();
                // full path of file
velocityEngine.mergeTemplate("/emailTemplate/order.vm", "UTF-8", context, stringWriter);
return stringWriter.toString();
}



Maven dependency:
<dependency>
<groupId>org.apache.velocity</groupId>
<artifactId>velocity</artifactId>
<version>1.7</version>
</dependency>
There is no need for spring-support dependency.

Cheer (^.^)
theroad


Tuesday, November 12, 2019

Resource to prepare for Java job interview in US



I'm a normal one :) I was looking for a Java developer position in the US to start my internship.

To revise and connect the knowledge for interviews, I need to prepare what to talk about and how to express what I am thinking. 

Somehow, I can not express what I've done in English. I need words to show it up.

I recommend 3 resources which you should take a look:

1. Java basic & more than that: 

Java Interview Questions and Answers


2. Spring: Spring Questions And Answers

3. Algorithm: https://www.algoexpert.io/questions

Common Programming questions 


Certainly, you don't have to practice all of the questions once. The process is to study and take interviews, study, interview, keep it going and going. 
In general, most of us are worried about the algorithm. However, after taking interviews, I realized that we just need to prepare simple problems that are in the easy category. I'm going to list below for reference



- revert string

- revert number

- palindrome

- 2 numbers sum

- traverse binary tree

- check 2 binary trees are unique 

- different characters of 2 strings ('abc', 'cdabf' -> 'df')


Good luck, folks