forked from sermakov/JavaPatternMirea
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtsk18.java
More file actions
186 lines (152 loc) · 6.2 KB
/
tsk18.java
File metadata and controls
186 lines (152 loc) · 6.2 KB
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
package a;
import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;
import lombok.*;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.annotations.TestOnly;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.actuate.endpoint.annotation.Endpoint;
import org.springframework.boot.actuate.endpoint.annotation.ReadOperation;
import org.springframework.boot.actuate.endpoint.annotation.WriteOperation;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.data.repository.NoRepositoryBean;
import org.springframework.orm.hibernate5.HibernateTransactionManager;
import org.springframework.orm.hibernate5.LocalSessionFactoryBean;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;
import org.springframework.transaction.PlatformTransactionManager;
import javax.persistence.*;
import javax.sql.DataSource;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.Properties;
/*
sudo mkdir /run/postgresql
sudo chgrp postgres /run/postgresql
sudo chown postgres /run/postgresql
sudo su - postgres
pg_ctl -D /var/lib/postgres/data start
psql -d db
select * from urs;
select * from dgs;
pg_ctl -D /var/lib/postgres/data stop
management.endpoints.web.exposure.include=usr,dg
*/
@SpringBootApplication
public class tsk18 {
private static final String db = "db";
private static final String tb1 = "urs";
private static final String tb2 = "dgs";
public static void main(String[] args)
{ SpringApplication.run(tsk18.class, args); }
////////////////////////////////////////////////////////////////////// configuration
@Configuration @EnableJpaRepositories(considerNestedRepositories = true)
public static class A {
@Bean public DataSource ds() {
val a = new HikariConfig();
a.setJdbcUrl("jdbc:postgresql://localhost:5432/" + db);
a.setDriverClassName("org.postgresql.Driver");
a.setUsername("postgres");
a.setPassword("postgres");
return new HikariDataSource(a);
}
@Bean(name = "entityManagerFactory")
public LocalSessionFactoryBean fc(DataSource b) {
val a = new LocalSessionFactoryBean();
a.setDataSource(b);
a.setPackagesToScan(this.getClass().getPackageName());
val c = new Properties();
c.setProperty("hibernate.dialect",
"org.hibernate.dialect.PostgreSQL92Dialect");
c.setProperty("hibernate.show_sql", "true");
c.setProperty("hibernate.hbm2ddl.auto", "update");
a.setHibernateProperties(c);
return a;
}
@Bean(name = "transactionManager")
public PlatformTransactionManager
pt(LocalSessionFactoryBean a) {
return new HibernateTransactionManager(
Objects.requireNonNull(a.getObject())); }
}
////////////////////////////////////////////////////////////////////// service
private interface $$<T extends _$, R extends $$$$<T>>
{ List<T> gal(); void dd(T a); void dl(int a);
@TestOnly void nw(); @TestOnly String gt(); }
public interface uss extends $$<usr, usp> {}
public interface dgs extends $$<dg, dgp> {}
@RequiredArgsConstructor
private abstract static class
$$$<T extends _$, R extends $$$$<T>> implements $$<T, R> {
private final R rp;
@Override public List<T> gal() { return rp.findAll(); }
@Override public void dd(T a) { rp.save(a); }
@Override public void dl(int a) { rp.deleteById(a); }
@SuppressWarnings("unchecked")
@TestOnly @Override
public void nw() { dd((T) (
rp instanceof usp ?
new usr("fn", "ln") :
new dg("nm", "bd"))); }
@TestOnly @Override
public String gt() { return gal().toString(); }
}
// GET curl http://localhost:8080/actuator/usr
@Component @Endpoint(id = "usr") @Service
public static class ussi extends $$$<usr, usp> implements uss {
public ussi(usp a) { super(a); }
@SuppressWarnings("unused") @ReadOperation
public String a() { return gt(); }
}
// GET curl http://localhost:8080/actuator/dg
@Component @Endpoint(id = "dg") @Service
public static class dgsi extends $$$<dg, dgp> implements dgs {
public dgsi(dgp a) { super(a); }
@SuppressWarnings("unused") @ReadOperation
public String a() { return gt(); }
}
////////////////////////////////////////////////////////////////////// repository
@NoRepositoryBean public interface
$$$$<T extends _$> extends JpaRepository<T, Integer> {}
@Repository public interface usp extends $$$$<usr> {}
@Repository public interface dgp extends $$$$<dg> {}
////////////////////////////////////////////////////////////////////// data
@MappedSuperclass
public static abstract class _$ implements Serializable
{ @GeneratedValue(strategy = GenerationType.IDENTITY)
@Nullable @Id Integer id; }
@Table(name = tb1) @Entity
@RequiredArgsConstructor
public static class usr extends _$ {
@NonNull String fn;
@NonNull String ln;
@OneToMany(
mappedBy = "rs",
fetch = FetchType.EAGER)
List<dg> dgs = new ArrayList<>();
public usr() {}
@Override public String toString()
{ return "(%d %s %s %s)"
.formatted(id, fn, ln, dgs
.stream()
.map(a -> "(%d %s %s)"
.formatted(a.id, a.nm, a.bd))
.toList()); }
}
@Table(name = tb2) @Entity @ToString
@RequiredArgsConstructor
public static class dg extends _$ {
@NonNull String nm;
@NonNull String bd;
@ManyToOne
@Nullable usr rs;
public dg() {}
}
}