Summarize the TERASOLUNA Server Framework for Java (5.x) Development Guideline for yourself: 4.8 Code List

Introduction

Hello. I started studying TERASOLUNA.

――As a study method, I will read Guidelines for a while, but since the amount of information is heavy, I will proceed while summarizing it in my own way. I will go. -(I have exams every few months, but I can read these guidelines during the exam, so I'd rather remember where the topics are to answer the question, rather than remembering the whole thing perfectly. ) ――_ As a policy, __ "Note" and "Tip" will appear in the exam as if they are off the main line, so I will not overlook those points (posted here in the form of quotations). Do you plan to do it).

4.8 Code list

infomation

version

5.3.1.RELEASE (2017-11-10)

History

Main story

1.OverView

--A code list is a set of "code values (values) and their display names (labels)". It is used as a mapping table for labels when displaying code values on the screen, such as the screen select box. --The common library provides the following. (As an application, it supports internationalization and supports reloading of cached code lists) --A function to read and cache the code list defined in the xml file or DB when the application starts. --Ability to refer to code list from JSP or Java class --Ability to check input using a code list

Reloading is possible by default only when using the code list defined in the DB.

--Code list implementation provided by common library --SimpleMapCodeList Use the contents described directly in the xml file. --NumberRangeCodeList Used when creating a list of numerical ranges. --JdbcCodeList Get the target code from DB with SQL and use it. --ʻEnumCodeList ʻEnum Used when creating a code list from constants defined in the class. --SinpleI18nCodeList Use the code list according to java.util.Locale.

Class diagram structure (from Guidelines)

From Guidelines

  1. How to use

2.1. How to use SimpleMapCodeList

Read the code value defined in the xml file when starting the application and use it as it is.

Code list settings

--It is recommended to create the bean definition file (xxx-codelist.xml) for the code list. --After creating the bean definition file for the code list, it is necessary to import to the existing bean definition file (xxx-domain.xml).

Using code list in JSP

By using the interceptor provided from the common library, it is automatically set in the request scope and the code list can be easily referenced from the JSP.

--Set in the bean definition file (spring-mvc.xml). --Set the applicable path with <mvc: mapping path =" / ** "> (in this case, set the whole path). --Define the CodeListInterceptor class as a bean. By setting <propery name =" CodeListIdPattern "value =" CL_. + "/>, The beanID pattern of the code list automatically set in the request scope is the data whose id is defined in "CL_XXX" format. Can only be targeted. The bean ID that applies here will be available in JSP. --If you omit codeListPattern, all codelists will be available in JSP.

--Implement in JSP (in case of select box). You can set a dummy value at the beginning by setting an empty string for --value. --By setting <form: options items =" $ {CL_XXX} "/>, the options that apply to the bean ID are output.

Using codelist in Java class

Inject the code list using @Inject and @Named. For @Named, specify the code list ID.

--Get the code list in Map format with CodeList # asMap method.

2.2. How to use NumberRangeCodeList

Code that lists the specified range of numbers when the application starts. It is supposed to be used mainly for a number of select boxes and select boxes for months and dates.

NumberRangeCodeList only supports Arabic numerals, not Chinese or Roman numerals. If you want to display Chinese numerals and Roman numerals, you can define them in JdbcCodeList and SimpleMapCodeList.

Code list setting example (Implementation example when the value of From is made smaller than To)

--<property name = "from" value = "" /> Range start value (default is 0) --<property name =" to "value =" "/> Range end value (required) --<property name =" valueFormat "value =" "/> Set the format of the code value (the format of java.lang.String.format is used) (default is% s) --<property name =" labelFormat "value =" "/> Set the format of the code name (the format of java.lang.String.format is used) (default is% s) --<property name = "interval" value = "" /> Set the value to increase (default is 1)

Using code list in JSP

Same as how to use SimpleMapCodeList.

Using codelist in Java class

Same as how to use SimpleMapCodeList.

2.3. How to use JdbcCodeList

A class that gets a value from the DB when the application starts and creates a code list. Since the cache is created when the application is started, there is no delay due to DB access when displaying the list. You can set an upper limit on the number of acquisitions in order to reduce the loading time at startup. It can be changed by setting an upper limit to fetchSize of ʻorg.springframework.jdbc.core.JdbcTemplate`.

Code list setting example

--Define in the bean definition file (xxx-codelist.xml). --ʻOrg.springframework.jdbc.core.JdbcTemplateDefine a class bean. --SetfetchSize. Note that if the default value is to get all records, it will affect the processing performance. --Set the following in each JdbcCodeList`. --When writing the SQL to be acquired in the querySql property, be sure to specify "ORDER BY" and confirm the order. --Set the value corresponding to the Key of Map in the valueColumn property. --Set the value corresponding to the Value of Map in the labelColumn property.

Using code list in JSP

Same as how to use SimpleMapCodeList.

Using codelist in Java class

Same as how to use SimpleMapCodeList.

2.4. How to use EnumCodeList

A class that creates a code list from the constants defined in the Enum class.

If you want to work with codelists in an application that meets the following conditions, consider using EnumCodeList to manage the labels of the codelist in the Enum class. By managing the label of the code list in the Enum class, the information and operations associated with the code value can be aggregated in the Enum class. --Code values need to be managed by Enum class (that is, Java logic needs to be aware of code values) --No need for UI internationalization (multilingualization)


EnumCodeList provides the org.terasoluna.gfw.common.codelist.EnumCodeList.CodeListItem interface as an interface to get the information (code values and labels) needed to create a codelist from the Enum class. .. When using EnumCodeList, it is necessary to implement the EnumCodeList.CodeListItem interface in the created Enum class.

Code list setting example

--Create an Enum class that implements ʻEnumCodeList.CodeListItemprovided by the common library. In theCodeListIteminterface, the following is defined as a method for acquiring the information (code value and label) required to create a code list. -getCodeValue() -getCodeList()` --Define a constant. At this time, specify the necessary information (code value, label).

The order of the code list when using EnumCodeList is the definition order of constants.

--Prepare a property that holds the information necessary to create a code list, a constructor that receives it, a code value that a constant holds, and a method that returns a label.

Using code list in JSP

Same as how to use SimpleMapCodeList.

Using codelist in Java class

Same as how to use SimpleMapCodeList.

2.5. How to use SimpleI18nCodeList

A code list that supports internationalization. You can set a code list for each locale and return the code list corresponding to the locale.

Code list setting example

It is easy to understand if you imagine a two-dimensional table where the rows are Locale, the columns are code values, and the cell contents are labels. There are three setting methods, but basically the method of "setting the CodeList for each Locale on a line-by-line basis" is recommended.

xxx-codelist.xml


<bean id="CL_I18N_PRICE"
    class="org.terasoluna.gfw.common.codelist.i18n.SimpleI18nCodeList">
    <property name="rowsByCodeList"> <!-- (1) -->
        <util:map>
            <entry key="en" value-ref="CL_PRICE_EN" />
            <entry key="ja" value-ref="CL_PRICE_JA" />
        </util:map>
    </property>
</bean>

-(1) Set Map with key java.lang.Locale in rowsByCodeList property. In Map, specify the locale in key and the reference destination of the code list class corresponding to the locale in value-ref. Map value refers to the code list class corresponding to each locale.

For the code list for each Locale, the above-mentioned SimpleMapCodeList and JdbcCodeList can be prepared. At this time, SimpleI18nCodeList does not support reloadable, and it is necessary to support it by its own implementation.

2.6. Display the code name from a specific code value

When referencing the code list from JSP, it can be referenced in the same way as the Map interface. Example: ʻOrder Status: $ {f: h (CL_ORDERSTATUS [orderForm.orderStatus])} ` (By specifying the value stored in the code value (orderStatus in this example) as the acquired Map interface key, the corresponding code name can be displayed.)

2.7. Checking the input of code values using the code list

When checking whether the input value is a code value defined in the code list, the common library provides an annotation for Bean Validation, ʻorg.terasoluna.gfw.common.codelist.ExistInCodeList`. (At this point, you need to change the default error message to suit your application requirements)

Setting example of @ExistInCodeList

xxx-codelist.xml


<bean id="CL_GENDER" class="org.terasoluna.gfw.common.codelist.SimpleMapCodeList">
    <property name="map">
        <map>
            <entry key="M" value="Male" />
            <entry key="F" value="Female" />
        </map>
    </property>
</bean>

Person.java


public class Person {
    @ExistInCodeList(codeListId = "CL_GENDER")  // (1)
    private String gender;

    // getter and setter omitted
}

-(1) Set @ExistInCodeList for the field you want to check the input, and specify the code list to be checked in cordListId.

The only type supported by the input check of @ExistInCodeList is the implementation class of the CharSequence interface (such as String) or Character. Therefore, even if the field with @ExistInCodeList is semantically an integer type, it must be defined as a String. (Year / Month / Day, etc.) Also, since @ExistInCodeList does not support values in the collection, it is necessary to devise an implementation to make the @ExistInCodeList annotation correspond to multiple selectable screen items (checkbox, multiple selection dropdown, etc.). is there.

  1. How to extend

3.1. When reloading the code list

When you update the master data of the code list, you may want to update the code list as well. The common library provides the ʻorg.terasoluna.gfw.common.codelist.ReloadableCodeList` interface. By implementing this and calling the refresh method, the code list can be updated. There are two ways to update the code list.

--Achieved by Task Scheduler --Call the refresh method in the Contoller (Service) class

** The guideline recommends using Task Scheduler provided by Spring to reload the code list on a regular basis. ** ** (If you need to refresh at any time, you can use Controller)

Refresh with Task Scheduler

xxx-codelist.xml


<task:scheduler id="taskScheduler" pool-size="10"/>  <!-- (1) -->

<task:scheduled-tasks scheduler="taskScheduler">  <!-- (2) -->
    <task:scheduled ref="CL_AUTHORITIES" method="refresh" cron="${cron.codelist.refreshTime}"/>  <!-- (3) -->
</task:scheduled-tasks>

<bean id="CL_AUTHORITIES" parent="AbstractJdbcCodeList">
    <property name="querySql"
        value="SELECT authority_id, authority_name FROM authority ORDER BY authority_id" />
    <property name="valueColumn" value="authority_id" />
    <property name="labelColumn" value="authority_name" />
</bean>
  1. Specify the thread pool size in the pool-size attribute of <task: scheduler>. (1 by default)
  2. Set the ID of <task: scheduler> in the scheduler attribute of <task: scheduler-tasks>.
  3. Specify the refresh method in the method attribute of <task: scheduled>. Describe in the cron attribute in the format supported by ʻorg.springframework.scheduling.support.CronSequenceGenerator`. Since it is expected that the reload timing of the cron attribute will change depending on the environment, it is recommended to obtain it from the property file or environment variables.

Call the refresh method in the Contoller (Service) class

--In Controller, you don't have to do anything, just call the Service class for refresh. --In the Service class, the code list is injected in the same way as "Using the code list in the Java class" described above. At this time, the ReloadableCodeList interface is defined for the field type. Therefore, it is necessary to implement ReloadableCodeList at the stage of Bean definition. --Define the refresh method in the Service class and execute the refresh method of the ReloadableCodeList interface in it.

3.2. How to customize your own code list

If it cannot be realized with the four types of code lists provided by the common library, customize it yourself. There are two types of code lists that can be created. --ʻAbstractCodeList Reloadable Used when unnecessary. Override ʻasMap. --ʻAbstractReloadableCodeListUsed when Reloadable is required. OverrideretrieveMap`.

Code list class

As an example, let's take a code list that creates a list for this year's next year.

DepYearCodeList.java


@Component("CL_YEAR") // (1)
public class DepYearCodeList extends AbstractCodeList { // (2)

    @Inject
    JodaTimeDateFactory dateFactory; // (3)

    @Override
    public Map<String, String> asMap() {  // (4)
        DateTime dateTime = dateFactory.newDateTime();
        DateTime nextYearDateTime = dateTime.plusYears(1);

        Map<String, String> depYearMap = new LinkedHashMap<String, String>();

        String thisYear = dateTime.toString("Y");
        String nextYear = nextYearDateTime.toString("Y");
        depYearMap.put(thisYear, thisYear);
        depYearMap.put(nextYear, nextYear);

        return Collections.unmodifiableMap(depYearMap);
    }
}
  1. Register the code list as a component with @Component. In value, the code list is registered as a component by the code list intercept set in the bean definition.
  2. Inherit ʻAbstractCodeList`.
  3. Inject JodaTimeDateFactory. You can create a Date class for system dates.
  4. Override the ʻasMap ()` method to create a list of this year and next year.

If you customize the reloadable CodeList, implement it so that it is thread-safe.

  1. Appendix

4.1. How to set the code list of SimpleI18nCodeList

For methods other than those described in item 2.5.

Set Map for each Locale line by line

Set "Map of Map" for the rows property. Set the locale first on the outer Map, and define the value for each key on the inner Map.

Set Map for each code value for each column

Determine the key first on the outer Map, and set the locale and the corresponding value on the inner Map.

References: TERASOLUNA Server Framework for Java (5.x) Development Guideline

Recommended Posts

Summarize the TERASOLUNA Server Framework for Java (5.x) Development Guideline for yourself: 4.8 Code List
Sample code collection for Azure Java development
[Development] Java framework comparison
Eclipse installation and code completion enhancements (Mac for Java development)
Introducing Spring Boot2, a Java framework for web development (for beginners)