Skip to content

Commit

Permalink
/series/add: add fields with day and month of release.
Browse files Browse the repository at this point in the history
Fixed #148 (GH #4)
  • Loading branch information
php-coder committed May 28, 2014
1 parent a79f4db commit a8573e7
Show file tree
Hide file tree
Showing 24 changed files with 224 additions and 57 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -602,7 +602,7 @@
</propertyFile>
<migrationSqlOutputFile>/dev/stdout</migrationSqlOutputFile>
<!-- See also src/main/java/ru/mystamps/web/config/LiquibaseConfig.java -->
<contexts>scheme,init-data</contexts>
<contexts>scheme,init-data,prod-data</contexts>
</configuration>
</plugin>

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/ru/mystamps/web/config/LiquibaseConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ private static String getActiveContexts(Environment env) {
return "scheme, init-data, test-data";
} else {
// see also duplicate definition at pom.xml
return "scheme, init-data";
return "scheme, init-data, prod-data";
}
}

Expand Down
25 changes: 25 additions & 0 deletions src/main/java/ru/mystamps/web/controller/SeriesController.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
import ru.mystamps.web.service.dto.EntityInfoDto;
import ru.mystamps.web.support.spring.security.SecurityContextUtils;
import ru.mystamps.web.util.CatalogUtils;
import ru.mystamps.web.validation.ValidationRules;

@Controller
@RequiredArgsConstructor
Expand All @@ -63,13 +64,27 @@ public class SeriesController {
private static final Integer SINCE_YEAR = 1840;
private static final Integer CURRENT_YEAR = new GregorianCalendar().get(Calendar.YEAR);

private static final Map<Integer, Integer> DAYS;
private static final Map<Integer, Integer> MONTHS;
private static final Map<Integer, Integer> YEARS;

private final CategoryService categoryService;
private final CountryService countryService;
private final SeriesService seriesService;

static {
DAYS = new LinkedHashMap<>(ValidationRules.MAX_DAYS_IN_MONTH);
for (int i = 1; i <= ValidationRules.MAX_DAYS_IN_MONTH; i++) {
Integer day = Integer.valueOf(i);
DAYS.put(day, day);
}

MONTHS = new LinkedHashMap<>(ValidationRules.MAX_MONTHS_IN_YEAR);
for (int i = 1; i <= ValidationRules.MAX_MONTHS_IN_YEAR; i++) {
Integer month = Integer.valueOf(i);
MONTHS.put(month, month);
}

YEARS = new LinkedHashMap<>();
for (Integer i = CURRENT_YEAR; i >= SINCE_YEAR; i--) {
YEARS.put(i, i);
Expand All @@ -86,6 +101,16 @@ protected void initBinder(WebDataBinder binder) {
binder.registerCustomEditor(String.class, "comment", new StringTrimmerEditor(true));
}

@ModelAttribute("days")
public Map<Integer, Integer> getDays() {
return DAYS;
}

@ModelAttribute("months")
public Map<Integer, Integer> getMonths() {
return MONTHS;
}

@ModelAttribute("years")
public Map<Integer, Integer> getYears() {
return YEARS;
Expand Down
8 changes: 6 additions & 2 deletions src/main/java/ru/mystamps/web/dao/SeriesDao.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,9 @@ public interface SeriesDao extends CrudRepository<Series, Integer> {
+ "s.id, "
+ "cat.id, cat.name, "
+ "c.id, c.name, "
+ "s.releasedAt, "
+ "s.releaseDay, "
+ "s.releaseMonth, "
+ "s.releaseYear, "
+ "s.quantity, "
+ "s.perforated"
+ ") "
Expand All @@ -64,7 +66,9 @@ public interface SeriesDao extends CrudRepository<Series, Integer> {
+ "s.id, "
+ "cat.id, cat.name, "
+ "c.id, c.name, "
+ "s.releasedAt, "
+ "s.releaseDay, "
+ "s.releaseMonth, "
+ "s.releaseYear, "
+ "s.quantity, "
+ "s.perforated"
+ ") "
Expand Down
14 changes: 8 additions & 6 deletions src/main/java/ru/mystamps/web/entity/Series.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
*/
package ru.mystamps.web.entity;

import java.util.Date;
import java.util.Set;

import javax.persistence.AttributeOverride;
Expand All @@ -33,8 +32,6 @@
import javax.persistence.ManyToOne;
import javax.persistence.OrderBy;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

import lombok.Getter;
import lombok.Setter;
Expand All @@ -61,9 +58,14 @@ public class Series {
@ManyToOne
private Country country;

@Temporal(TemporalType.DATE)
@Column(name = "released_at")
private Date releasedAt;
@Column(name = "release_day")
private Integer releaseDay;

@Column(name = "release_month")
private Integer releaseMonth;

@Column(name = "release_year")
private Integer releaseYear;

@Column(nullable = false)
private Integer quantity;
Expand Down
17 changes: 17 additions & 0 deletions src/main/java/ru/mystamps/web/model/AddSeriesForm.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
import javax.validation.constraints.Size;
import javax.validation.GroupSequence;

import org.hibernate.validator.constraints.Range;

import org.springframework.web.multipart.MultipartFile;

import ru.mystamps.web.entity.Category;
Expand All @@ -42,6 +44,8 @@
import static ru.mystamps.web.validation.ValidationRules.MAX_SERIES_COMMENT_LENGTH;
import static ru.mystamps.web.validation.ValidationRules.MAX_STAMPS_IN_SERIES;
import static ru.mystamps.web.validation.ValidationRules.MIN_STAMPS_IN_SERIES;
import static ru.mystamps.web.validation.ValidationRules.MAX_DAYS_IN_MONTH;
import static ru.mystamps.web.validation.ValidationRules.MAX_MONTHS_IN_YEAR;

import lombok.Getter;
import lombok.Setter;
Expand All @@ -51,6 +55,12 @@
// TODO: combine price with currency to separate class
@SuppressWarnings({"PMD.TooManyFields", "PMD.AvoidDuplicateLiterals"})
@NotNullIfFirstField.List({
@NotNullIfFirstField(
first = "month", second = "year", message = "{month.requires.year}"
),
@NotNullIfFirstField(
first = "day", second = "month", message = "{day.requires.month}"
),
@NotNullIfFirstField(
first = "michelPrice", second = "michelCurrency", message = "{currency.required}"
),
Expand All @@ -70,6 +80,13 @@ public class AddSeriesForm implements AddSeriesDto {
private Category category;

private Country country;

@Range(min = 1, max = MAX_DAYS_IN_MONTH, message = "{day.invalid}")
private Integer day;

@Range(min = 1, max = MAX_MONTHS_IN_YEAR, message = "{month.invalid}")
private Integer month;

private Integer year;

@NotNull
Expand Down
18 changes: 9 additions & 9 deletions src/main/java/ru/mystamps/web/service/SeriesServiceImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,12 @@
*/
package ru.mystamps.web.service;

import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Set;

import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.transaction.annotation.Transactional;

import static java.util.Calendar.JANUARY;

import org.apache.commons.lang3.Validate;

import org.slf4j.Logger;
Expand Down Expand Up @@ -75,11 +71,15 @@ public Series add(AddSeriesDto dto, User user, boolean userCanAddComments) {
}

if (dto.getYear() != null) {
Calendar releaseDate = GregorianCalendar.getInstance();
releaseDate.clear();
releaseDate.set(dto.getYear(), JANUARY, 1);

series.setReleasedAt(releaseDate.getTime());
series.setReleaseYear(dto.getYear());

if (dto.getMonth() != null) {
series.setReleaseMonth(dto.getMonth());

if (dto.getDay() != null) {
series.setReleaseDay(dto.getDay());
}
}
}

series.setCategory(dto.getCategory());
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/ru/mystamps/web/service/dto/AddSeriesDto.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
public interface AddSeriesDto {
Category getCategory();
Country getCountry();
Integer getDay();
Integer getMonth();
Integer getYear();
Integer getQuantity();
Boolean getPerforated();
Expand Down
12 changes: 7 additions & 5 deletions src/main/java/ru/mystamps/web/service/dto/SeriesInfoDto.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,16 @@
*/
package ru.mystamps.web.service.dto;

import java.util.Date;

import lombok.Getter;

@Getter
public class SeriesInfoDto {
private final Integer id;
private final EntityInfoDto category;
private final EntityInfoDto country;
private final Date releasedAt;
private final Integer releaseDay;
private final Integer releaseMonth;
private final Integer releaseYear;
private final Integer quantity;
private final Boolean perforated;

Expand All @@ -35,13 +35,15 @@ public SeriesInfoDto(
Integer id,
Integer categoryId, String categoryName,
Integer countryId, String countryName,
Date releasedAt,
Integer releaseDay, Integer releaseMonth, Integer releaseYear,
Integer quantity,
Boolean perforated) {
this.id = id;
this.category = new EntityInfoDto(categoryId, categoryName);
this.country = new EntityInfoDto(countryId, countryName);
this.releasedAt = releasedAt;
this.releaseDay = releaseDay;
this.releaseMonth = releaseMonth;
this.releaseYear = releaseYear;
this.quantity = quantity;
this.perforated = perforated;
}
Expand Down
3 changes: 3 additions & 0 deletions src/main/java/ru/mystamps/web/validation/ValidationRules.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ public final class ValidationRules {
public static final int MAX_SERIES_COMMENT_LENGTH = Series.COMMENT_LENGTH;
public static final String CATALOG_NUMBERS_REGEXP = "[1-9][0-9]{0,3}(,[1-9][0-9]{0,3})*";

public static final int MAX_DAYS_IN_MONTH = 31;
public static final int MAX_MONTHS_IN_YEAR = 31;

private ValidationRules() {
}

Expand Down
1 change: 1 addition & 0 deletions src/main/resources/liquibase/version/0.3.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@
http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.0.xsd">

<include file="0.3/2014-02-11--categories.xml" relativeToChangelogFile="true" />
<include file="0.3/2014-05-28--release_month_and_day.xml" relativeToChangelogFile="true" />

</databaseChangeLog>
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?xml version="1.0" encoding="UTF-8"?>
<databaseChangeLog
xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog
http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.0.xsd">

<changeSet id="add-release_day-column-to-series-table" author="php-coder" context="scheme">
<comment>Adds release_day column to series table</comment>

<addColumn tableName="series">
<column name="release_day" type="INTEGER" />
</addColumn>

</changeSet>

<changeSet id="add-release_month-column-to-series-table" author="php-coder" context="scheme">
<comment>Adds release_month column to series table</comment>

<addColumn tableName="series">
<column name="release_month" type="INTEGER" />
</addColumn>

</changeSet>

<changeSet id="add-release_year-column-to-series-table" author="php-coder" context="scheme">
<comment>Adds release_year column to series table</comment>

<addColumn tableName="series">
<column name="release_year" type="INTEGER" />
</addColumn>

</changeSet>

<changeSet id="fill-release_year-column-based-on-values-from-released_at" author="php-coder" context="test-data, prod-data">
<comment>Migrates data from series.released_at to series.release_year</comment>

<sql>
UPDATE series
SET release_year=YEAR(released_at)
WHERE released_at IS NOT NULL
</sql>

</changeSet>

<changeSet id="drop-released_at-column-from-series-table" author="php-coder" context="scheme">
<comment>Drops released_at column from series table</comment>

<dropColumn tableName="series" columnName="released_at" />

</changeSet>

</databaseChangeLog>
2 changes: 1 addition & 1 deletion src/main/resources/ru/mystamps/i18n/Messages.properties
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ t_country = Country
t_add = Add
t_add_country = add country
t_create_category = add category
t_issue_date = Date of release

# site/index.jsp
t_index_title = create your own virtual collection!
Expand Down Expand Up @@ -64,7 +65,6 @@ t_enter = Sign in

# series/add.jsp
t_add_series_ucfirst = Add stamp series
t_issue_year = Year of release
t_year = Year
t_quantity = Quantity
t_perforated = Perforated
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/ru/mystamps/i18n/Messages_ru.properties
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ t_country = Страна
t_add = Добавить
t_add_country = добавить страну
t_create_category = добавить категорию
t_issue_date = Дата выпуска

# site/index.jsp
t_index_title = создай свою виртуальную коллекцию!
Expand Down Expand Up @@ -64,7 +65,6 @@ t_enter = Войти

# series/add.jsp
t_add_series_ucfirst = Добавить серию марок
t_issue_year = Год выпуска
t_year = Год
t_quantity = Количество
t_perforated = Перфорированная
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,7 @@ key.invalid = Key must consist only latin letters in lower case or digits

currency.required = Currency must be chosen

day.requires.month = Month must be specified
month.requires.year = Year must be specified
day.invalid = Invalid day of month
month.invalid = Invalid month of year
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,7 @@ key.invalid = Код активации может состоять только

currency.required = Необходимо выбрать валюту

day.requires.month = Необходимо также указать месяц
month.requires.year = Необходимо также указать год
day.invalid = Некорректный день месяца
month.invalid = Некорректный номер месяца
5 changes: 2 additions & 3 deletions src/main/webapp/WEB-INF/tags/elem/series-info.tag
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>
<%@ taglib uri="http://www.springframework.org/tags" prefix="spring" %>

<%@ tag import="ru.mystamps.web.Url" %>
Expand Down Expand Up @@ -33,8 +32,8 @@
</spring:url>

<a href="${seriesInfoUrl}">
<c:if test="${not empty series.releasedAt}">
<fmt:formatDate value="${series.releasedAt}" pattern="yyyy, " />
<c:if test="${not empty series.releaseYear}">
<c:out value="${series.releaseYear}, " />
</c:if>

<c:out value="${series.quantity}" />&nbsp;<spring:message code="t_items" />
Expand Down
Loading

0 comments on commit a8573e7

Please sign in to comment.