Skip to content

Commit 3970a98

Browse files
committed
Extracted entities meta information to MetaInfo class.
Fixed CPD warning about code duplication. No functional changes.
1 parent 33462ec commit 3970a98

File tree

9 files changed

+109
-61
lines changed

9 files changed

+109
-61
lines changed

src/env/dev/WEB-INF/classes/mysql-scheme.sql

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
create table countries (
66
id integer not null auto_increment,
77
created_at datetime not null,
8-
name varchar(50) not null unique,
98
updated_at datetime not null,
9+
name varchar(50) not null unique,
1010
version bigint not null,
1111
created_by integer not null,
1212
updated_by integer not null,
@@ -41,12 +41,12 @@
4141
create table series (
4242
id integer not null auto_increment,
4343
comment varchar(255),
44-
created_at datetime not null,
4544
image_url varchar(255),
45+
created_at datetime not null,
46+
updated_at datetime not null,
4647
perforated bit not null,
4748
quantity integer not null,
4849
released_at date,
49-
updated_at datetime not null,
5050
version bigint not null,
5151
country_id integer,
5252
created_by integer not null,

src/env/test/WEB-INF/classes/h2-scheme.sql

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
create table countries (
66
id integer generated by default as identity,
77
created_at timestamp not null,
8-
name varchar(50) not null unique,
98
updated_at timestamp not null,
9+
name varchar(50) not null unique,
1010
version bigint not null,
1111
created_by integer not null,
1212
updated_by integer not null,
@@ -41,12 +41,12 @@
4141
create table series (
4242
id integer generated by default as identity,
4343
comment varchar(255),
44-
created_at timestamp not null,
4544
image_url varchar(255),
45+
created_at timestamp not null,
46+
updated_at timestamp not null,
4647
perforated boolean not null,
4748
quantity integer not null,
4849
released_at date,
49-
updated_at timestamp not null,
5050
version bigint not null,
5151
country_id integer,
5252
created_by integer not null,

src/main/java/ru/mystamps/web/entity/Country.java

Lines changed: 7 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,12 @@
1717
*/
1818
package ru.mystamps.web.entity;
1919

20-
import java.util.Date;
21-
2220
import javax.persistence.Column;
21+
import javax.persistence.Embedded;
2322
import javax.persistence.Entity;
2423
import javax.persistence.GeneratedValue;
2524
import javax.persistence.Id;
26-
import javax.persistence.JoinColumn;
27-
import javax.persistence.ManyToOne;
2825
import javax.persistence.Table;
29-
import javax.persistence.Temporal;
30-
import javax.persistence.TemporalType;
3126
import javax.persistence.Version;
3227

3328
import lombok.AccessLevel;
@@ -49,25 +44,16 @@ public class Country {
4944
@Column(length = NAME_LENGTH, unique = true, nullable = false)
5045
private String name;
5146

52-
@Temporal(TemporalType.TIMESTAMP)
53-
@Column(name = "created_at", nullable = false)
54-
private Date createdAt;
55-
56-
@ManyToOne(optional = false)
57-
@JoinColumn(name = "created_by", nullable = false)
58-
private User createdBy;
59-
60-
@Temporal(TemporalType.TIMESTAMP)
61-
@Column(name = "updated_at", nullable = false)
62-
private Date updatedAt;
63-
64-
@ManyToOne(optional = false)
65-
@JoinColumn(name = "updated_by", nullable = false)
66-
private User updatedBy;
47+
@Embedded
48+
private MetaInfo metaInfo; // NOPMD
6749

6850
@Setter(AccessLevel.PROTECTED)
6951
@Version
7052
@Column(nullable = false)
7153
private Long version;
7254

55+
public Country() {
56+
metaInfo = new MetaInfo();
57+
}
58+
7359
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
* Copyright (C) 2009-2012 Slava Semushin <slava.semushin@gmail.com>
3+
*
4+
* This program is free software; you can redistribute it and/or modify
5+
* it under the terms of the GNU General Public License as published by
6+
* the Free Software Foundation; either version 2 of the License, or
7+
* (at your option) any later version.
8+
*
9+
* This program is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
* GNU General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU General Public License
15+
* along with this program; if not, write to the Free Software
16+
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17+
*/
18+
package ru.mystamps.web.entity;
19+
20+
import java.util.Date;
21+
22+
import javax.persistence.Column;
23+
import javax.persistence.Embeddable;
24+
import javax.persistence.JoinColumn;
25+
import javax.persistence.ManyToOne;
26+
import javax.persistence.Temporal;
27+
import javax.persistence.TemporalType;
28+
29+
import lombok.Getter;
30+
import lombok.Setter;
31+
32+
@Embeddable
33+
@Getter
34+
@Setter
35+
public class MetaInfo {
36+
37+
@Temporal(TemporalType.TIMESTAMP)
38+
@Column(name = "created_at", nullable = false)
39+
private Date createdAt;
40+
41+
@ManyToOne(optional = false)
42+
@JoinColumn(name = "created_by", nullable = false)
43+
private User createdBy;
44+
45+
@Temporal(TemporalType.TIMESTAMP)
46+
@Column(name = "updated_at", nullable = false)
47+
private Date updatedAt;
48+
49+
@ManyToOne(optional = false)
50+
@JoinColumn(name = "updated_by", nullable = false)
51+
private User updatedBy;
52+
53+
}

src/main/java/ru/mystamps/web/entity/Series.java

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,11 @@
2121
import java.util.Set;
2222

2323
import javax.persistence.Column;
24+
import javax.persistence.Embedded;
2425
import javax.persistence.Entity;
2526
import javax.persistence.FetchType;
2627
import javax.persistence.GeneratedValue;
2728
import javax.persistence.Id;
28-
import javax.persistence.JoinColumn;
2929
import javax.persistence.ManyToMany;
3030
import javax.persistence.ManyToOne;
3131
import javax.persistence.OrderBy;
@@ -87,25 +87,16 @@ public class Series {
8787
@Column(length = COMMENT_LENGTH)
8888
private String comment;
8989

90-
@Temporal(TemporalType.TIMESTAMP)
91-
@Column(name = "created_at", nullable = false)
92-
private Date createdAt;
93-
94-
@ManyToOne(optional = false)
95-
@JoinColumn(name = "created_by", nullable = false)
96-
private User createdBy;
97-
98-
@Temporal(TemporalType.TIMESTAMP)
99-
@Column(name = "updated_at", nullable = false)
100-
private Date updatedAt;
101-
102-
@ManyToOne(optional = false)
103-
@JoinColumn(name = "updated_by", nullable = false)
104-
private User updatedBy;
90+
@Embedded
91+
private MetaInfo metaInfo; // NOPMD
10592

10693
@Setter(AccessLevel.PROTECTED)
10794
@Version
10895
@Column(nullable = false)
10996
private Long version;
11097

98+
public Series() {
99+
metaInfo = new MetaInfo();
100+
}
101+
111102
}

src/main/java/ru/mystamps/web/service/CountryService.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,13 +51,13 @@ public Country add(AddCountryDto dto) {
5151
country.setName(dto.getName());
5252

5353
Date now = new Date();
54-
country.setCreatedAt(now);
55-
country.setUpdatedAt(now);
54+
country.getMetaInfo().setCreatedAt(now);
55+
country.getMetaInfo().setUpdatedAt(now);
5656

5757
User currentUser = authService.getCurrentUser();
5858
Validate.validState(currentUser != null, "Current user must be non null");
59-
country.setCreatedBy(currentUser);
60-
country.setUpdatedBy(currentUser);
59+
country.getMetaInfo().setCreatedBy(currentUser);
60+
country.getMetaInfo().setUpdatedBy(currentUser);
6161

6262
return countryDao.save(country);
6363
}

src/main/java/ru/mystamps/web/service/SeriesService.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -123,13 +123,13 @@ public Series add(AddSeriesDto dto) {
123123
}
124124

125125
Date now = new Date();
126-
series.setCreatedAt(now);
127-
series.setUpdatedAt(now);
126+
series.getMetaInfo().setCreatedAt(now);
127+
series.getMetaInfo().setUpdatedAt(now);
128128

129129
User currentUser = authService.getCurrentUser();
130130
Validate.validState(currentUser != null, "Current user must be non null");
131-
series.setCreatedBy(currentUser);
132-
series.setUpdatedBy(currentUser);
131+
series.getMetaInfo().setCreatedBy(currentUser);
132+
series.getMetaInfo().setUpdatedBy(currentUser);
133133

134134
return seriesDao.save(series);
135135
}

src/test/java/ru/mystamps/web/service/CountryServiceTest.java

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242

4343
import ru.mystamps.web.dao.CountryDao;
4444
import ru.mystamps.web.entity.Country;
45+
import ru.mystamps.web.entity.MetaInfo;
4546
import ru.mystamps.web.entity.User;
4647
import ru.mystamps.web.model.AddCountryForm;
4748
import ru.mystamps.web.tests.fest.DateAssert;
@@ -118,7 +119,9 @@ public void addShouldAssignCreatedAtToCurrentDate() {
118119

119120
verify(countryDao).save(countryCaptor.capture());
120121

121-
DateAssert.assertThat(countryCaptor.getValue().getCreatedAt()).isCurrentDate();
122+
MetaInfo metaInfo = countryCaptor.getValue().getMetaInfo();
123+
assertThat(metaInfo).isNotNull();
124+
DateAssert.assertThat(metaInfo.getCreatedAt()).isCurrentDate();
122125
}
123126

124127
@Test
@@ -127,7 +130,9 @@ public void addShouldAssignUpdatedAtToCurrentDate() {
127130

128131
verify(countryDao).save(countryCaptor.capture());
129132

130-
DateAssert.assertThat(countryCaptor.getValue().getUpdatedAt()).isCurrentDate();
133+
MetaInfo metaInfo = countryCaptor.getValue().getMetaInfo();
134+
assertThat(metaInfo).isNotNull();
135+
DateAssert.assertThat(metaInfo.getUpdatedAt()).isCurrentDate();
131136
}
132137

133138
@Test(expected = IllegalStateException.class)
@@ -145,7 +150,9 @@ public void addShouldAssignCreatedAtToCurrentUser() {
145150
service.add(form);
146151

147152
verify(countryDao).save(countryCaptor.capture());
148-
assertThat(countryCaptor.getValue().getCreatedBy()).isEqualTo(expectedUser);
153+
final MetaInfo metaInfo = countryCaptor.getValue().getMetaInfo();
154+
assertThat(metaInfo).isNotNull();
155+
assertThat(metaInfo.getCreatedBy()).isEqualTo(expectedUser);
149156
}
150157

151158
@Test
@@ -156,7 +163,9 @@ public void addShouldAssignUpdatedAtToCurrentUser() {
156163
service.add(form);
157164

158165
verify(countryDao).save(countryCaptor.capture());
159-
assertThat(countryCaptor.getValue().getUpdatedBy()).isEqualTo(expectedUser);
166+
final MetaInfo metaInfo = countryCaptor.getValue().getMetaInfo();
167+
assertThat(metaInfo).isNotNull();
168+
assertThat(metaInfo.getUpdatedBy()).isEqualTo(expectedUser);
160169
}
161170

162171
//
@@ -239,8 +248,8 @@ static Country getCountry() {
239248
country.setId(TEST_COUNTRY_ID);
240249
country.setName(TEST_COUNTRY_NAME);
241250
Date now = new Date();
242-
country.setCreatedAt(now);
243-
country.setUpdatedAt(now);
251+
country.getMetaInfo().setCreatedAt(now);
252+
country.getMetaInfo().setUpdatedAt(now);
244253
return country;
245254
}
246255

src/test/java/ru/mystamps/web/service/SeriesServiceTest.java

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
import ru.mystamps.web.dao.YvertCatalogDao;
5353
import ru.mystamps.web.entity.Country;
5454
import ru.mystamps.web.entity.GibbonsCatalog;
55+
import ru.mystamps.web.entity.MetaInfo;
5556
import ru.mystamps.web.entity.MichelCatalog;
5657
import ru.mystamps.web.entity.ScottCatalog;
5758
import ru.mystamps.web.entity.Series;
@@ -430,7 +431,9 @@ public void addShouldAssignCreatedAtToCurrentDate() {
430431

431432
verify(seriesDao).save(seriesCaptor.capture());
432433

433-
DateAssert.assertThat(seriesCaptor.getValue().getCreatedAt()).isCurrentDate();
434+
MetaInfo metaInfo = seriesCaptor.getValue().getMetaInfo();
435+
assertThat(metaInfo).isNotNull();
436+
DateAssert.assertThat(metaInfo.getCreatedAt()).isCurrentDate();
434437
}
435438

436439
@Test
@@ -439,7 +442,9 @@ public void addShouldAssignUpdatedAtToCurrentDate() {
439442

440443
verify(seriesDao).save(seriesCaptor.capture());
441444

442-
DateAssert.assertThat(seriesCaptor.getValue().getUpdatedAt()).isCurrentDate();
445+
MetaInfo metaInfo = seriesCaptor.getValue().getMetaInfo();
446+
assertThat(metaInfo).isNotNull();
447+
DateAssert.assertThat(metaInfo.getUpdatedAt()).isCurrentDate();
443448
}
444449

445450
@Test(expected = IllegalStateException.class)
@@ -457,7 +462,9 @@ public void addShouldAssignCreatedAtToCurrentUser() {
457462
service.add(form);
458463

459464
verify(seriesDao).save(seriesCaptor.capture());
460-
assertThat(seriesCaptor.getValue().getCreatedBy()).isEqualTo(expectedUser);
465+
MetaInfo metaInfo = seriesCaptor.getValue().getMetaInfo();
466+
assertThat(metaInfo).isNotNull();
467+
assertThat(metaInfo.getCreatedBy()).isEqualTo(expectedUser);
461468
}
462469

463470
@Test
@@ -468,7 +475,9 @@ public void addShouldAssignUpdatedAtToCurrentUser() {
468475
service.add(form);
469476

470477
verify(seriesDao).save(seriesCaptor.capture());
471-
assertThat(seriesCaptor.getValue().getUpdatedBy()).isEqualTo(expectedUser);
478+
MetaInfo metaInfo = seriesCaptor.getValue().getMetaInfo();
479+
assertThat(metaInfo).isNotNull();
480+
assertThat(metaInfo.getUpdatedBy()).isEqualTo(expectedUser);
472481
}
473482

474483
//

0 commit comments

Comments
 (0)