Total Pageviews

2017/11/16

[Windows 10] 點陣式印表機 (EPSON LQ-310) 無法使用

Problem
一早發現家中的點陣式印表機 (EPSON LQ-310) 無法使用,系統送出列印指令,印表機完全沒有反應。

How-To
經查,由於 11/15 晚上安裝了一個 windows update (KB4048954),導致 11/16 早上開始,點陣式印表機無法列印,處理步驟如下:





2017/11/11

[Fortify] Fix Locale Dependent Comparison

Problem


Before
The original code snippet is the following:
1
2
3
    if (value.toUpperCase().equals("TRUE")) {
        return true;
    }


After
Add dependency in your pom.xml
1
2
3
4
5
6
    <!-- https://mvnrepository.com/artifact/org.apache.commons/commons-lang3 -->
    <dependency>
        <groupId>org.apache.commons</groupId>
        <artifactId>commons-lang3</artifactId>
        <version>3.0</version>
    </dependency>


The updated code snippet are as bellows:
1
2
3
4
    String valueUpperCase = StringUtils.upperCase(value, Locale.ENGLISH);
    if(valueUpperCase.equals("TRUE")) {
        return true;
    }



2017/11/10

[Bootbox] How to change default button and button color in confirmation dialog?

Problem
The confirmation dialog looks like:

I would like to change the default button in confirmation dialog from "確認"(confirm) to "取消"(cancel). 
Owing to confirm to do delete is a dangerous action, so I would like to change it color to red.

How to meet the two requirements?


The original code snippet is as following:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
    $scope.remove = function() {
        bootbox.confirm({
            message:'是否確定要刪除', 
            buttons: {
                confirm: { label: '確認' },
                cancel:  { label: '取消' }
            },
            callback:function(isConfirmed) {
                if(isConfirmed) {
                    // ....
                }
            }
        });
    };



How-to
Add btn-danger class name to confirm button, then the button color will change to red.
Add btn-primary class to cancel button, then the default button will be cancel button.

The updated code snippet is as bellows:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
    $scope.remove = function() {
        bootbox.confirm({
            message:'是否確定要刪除', 
            buttons: {
                confirm: { label: '確認', className: 'btn-danger' },
                cancel:  { label: '取消', className: 'btn-primary' }
            },
            callback:function(isConfirmed) {
                if(isConfirmed) {
                    // ....
                }
            }
        });
    };

Updated screenshot:


2017/11/09

[Bootbox] How to change button name in Booxbox confirm dialog

Problem
I am using Bootbox to implement confirm dialog, the screenshot looks like:

The code snippet is as bellows:
1
2
3
4
5
6
7
    $scope.remove = function() {
        bootbox.confirm('是否確定要刪除', function(isConfirmed) {
            if(isConfirmed) {
                // ....
            }
        });
    };

How can I change OK label to "確定"? And how do I change Cancel label to "取消"?

How-to

Here has sample code to fulfill this requirement:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
    $scope.remove = function() {
        bootbox.confirm({
            message:'是否確定要刪除', 
            buttons: {
                confirm: { label: '確認' },
                cancel:  { label: '取消' }
            },
            callback:function(isConfirmed) {
                if(isConfirmed) {
                    // ....
                }
            }
        });
    };


The confirmation dialog looks like:





2017/11/08

[MapStruct] How to use Decorator in MapStruct?

Problem
Assume I have a DeliveryAddressDto Java bean, it has three attributes:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
package albert.practice.mapStruct;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;

@AllArgsConstructor
@NoArgsConstructor
@Data
@ToString
public class DeliveryAddressDto {
    private String receiver;
    private String addressString;
    private String totalString;
}

I have three Java beans, they provide information which DeliveryAddressDto needed.
1. Person bean
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
package albert.practice.mapStruct;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;

@AllArgsConstructor
@NoArgsConstructor
@Data
@ToString
public class Person {
    private String firstName;
    private String lastName;
}


2. Address bean
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
package albert.practice.mapStruct;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;

@AllArgsConstructor
@NoArgsConstructor
@Data
@ToString
public class Address {
    private String addressString;
}


3. ShoppingItems bean
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
package albert.practice.mapStruct;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;

@AllArgsConstructor
@NoArgsConstructor
@Data
@ToString
public class ShoppingItems {
    private Long total;
}


The three attributes values in DeliveryAddressDto Java bean will copy from Person, Address, and ShoppingItems bean
(1) DeliveryAddressDto.receiver = Person.firstName + " " + Person.lastName
(2) DeliveryAddressDto.addressString = Address.addressString
(3) DeliveryAddressDto.totalString = ShoppingItems.total (Apply format: NT$#,###,###,##0)




How to use decorator to fulfill this requirement?
 

How-To
1. create a mapper class
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
package albert.practice.mapStruct.mapper;

import org.mapstruct.DecoratedWith;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.factory.Mappers;

import albert.practice.mapStruct.Address;
import albert.practice.mapStruct.DeliveryAddressDto;
import albert.practice.mapStruct.Person;
import albert.practice.mapStruct.ShoppingItems;
import albert.practice.mapStruct.decorator.AddressDecorator;

@Mapper
@DecoratedWith(AddressDecorator.class)
public interface AddressMapper {
    // By convention, a mapper interface should define a member called INSTANCE
    // which holds a single instance of the mapper type:
    AddressMapper INSTANCE = Mappers.getMapper(AddressMapper.class);

    @Mapping(target = "totalString", source = "shoppingItems.total")
    @Mapping(target = "receiver", source = "person.lastName")
    @Mapping(target = "addressString", source = "address.addressString")
    DeliveryAddressDto covertPersonAndAddressToDeliveryAddressDto(Person person, Address address,
      ShoppingItems shoppingItems);

}

2. create a decorator class
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
package albert.practice.mapStruct.decorator;

import java.text.DecimalFormat;

import albert.practice.mapStruct.Address;
import albert.practice.mapStruct.DeliveryAddressDto;
import albert.practice.mapStruct.Person;
import albert.practice.mapStruct.ShoppingItems;
import albert.practice.mapStruct.mapper.AddressMapper;

public class AddressDecorator implements AddressMapper {

    private final AddressMapper delegate;
    private DecimalFormat decimalFormat = new DecimalFormat("NT$#,###,###,##0");

    public AddressDecorator(AddressMapper delegate) {
        this.delegate = delegate;
    }

    @Override
    public DeliveryAddressDto covertPersonAndAddressToDeliveryAddressDto(Person person, Address address,
      ShoppingItems shoppingItems) {
        DeliveryAddressDto dto = delegate.covertPersonAndAddressToDeliveryAddressDto(person, address, shoppingItems);
        dto.setReceiver(person.getFirstName() + " " + person.getLastName());
        dto.setTotalString(decimalFormat.format(shoppingItems.getTotal()));
        return dto;
    }

}