Skip to content

Commit 363e11b

Browse files
committed
Upload of ZAD 5
1 parent 76cf45a commit 363e11b

File tree

9 files changed

+127
-0
lines changed

9 files changed

+127
-0
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.jaz2.figurines.Dictionary;
2+
3+
import jakarta.validation.Constraint;
4+
import jakarta.validation.Payload;
5+
6+
import java.lang.annotation.*;
7+
8+
@Constraint(validatedBy = DictionaryValidator.class)
9+
@Target({ ElementType.METHOD, ElementType.FIELD })
10+
@Retention(RetentionPolicy.RUNTIME)
11+
public @interface Dictionary {
12+
String value();
13+
14+
String message() default "Invalid value for dictionary {value}";
15+
16+
Class<?>[] groups() default {};
17+
18+
Class<? extends Payload>[] payload() default {};
19+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.jaz2.figurines.Dictionary;
2+
3+
import jakarta.persistence.Entity;
4+
import jakarta.persistence.Id;
5+
import lombok.Data;
6+
import org.hibernate.annotations.UuidGenerator;
7+
8+
import java.util.UUID;
9+
10+
@Data
11+
@Entity
12+
public class DictionaryEntity {
13+
@Id
14+
@UuidGenerator
15+
private UUID id;
16+
private String valueName;
17+
private String dictionaryName;
18+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.jaz2.figurines.Dictionary;
2+
3+
import org.springframework.data.jpa.repository.JpaRepository;
4+
import org.springframework.stereotype.Repository;
5+
6+
import java.util.UUID;
7+
8+
@Repository
9+
public interface DictionaryRepository extends JpaRepository<DictionaryEntity, UUID> {
10+
boolean existsByDictionaryNameAndValueName(String valueName, String dictionaryName);
11+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package com.jaz2.figurines.Dictionary;
2+
3+
import java.sql.Connection;
4+
import java.sql.SQLException;
5+
import javax.sql.DataSource;
6+
7+
import jakarta.validation.ConstraintValidator;
8+
import jakarta.validation.ConstraintValidatorContext;
9+
import org.springframework.context.ApplicationContext;
10+
import org.springframework.context.ApplicationContextAware;
11+
import org.springframework.stereotype.Component;
12+
import org.springframework.beans.factory.annotation.Autowired;
13+
14+
@Component
15+
public class DictionaryValidator implements ConstraintValidator<Dictionary, String>, ApplicationContextAware {
16+
17+
private static ApplicationContext applicationContext;
18+
private String dictionaryName;
19+
Connection sqlConnection;
20+
21+
@SuppressWarnings("null")
22+
@Override
23+
public void setApplicationContext(ApplicationContext applicationContext) {
24+
DictionaryValidator.applicationContext = applicationContext;
25+
}
26+
27+
@Override
28+
public void initialize(Dictionary constraintAnnotation) {
29+
this.dictionaryName = constraintAnnotation.value();
30+
}
31+
32+
@Override
33+
public boolean isValid(String value, ConstraintValidatorContext context) {
34+
if (value == null) {
35+
return true;
36+
}
37+
try {
38+
DataSource dataSource = applicationContext.getBean(DataSource.class);
39+
sqlConnection = dataSource.getConnection();
40+
var sqlQuery = sqlConnection.prepareStatement("SELECT VALUE_NAME FROM DICTIONARY_ENTITY WHERE DICTIONARY_NAME = ?");
41+
sqlQuery.setString(1, dictionaryName);
42+
var resultSet = sqlQuery.executeQuery();
43+
while (resultSet.next()) {
44+
System.out.println(resultSet.getString("VALUE_NAME"));
45+
if (resultSet.getString("VALUE_NAME").equals(value)) {
46+
return true;
47+
}
48+
}
49+
return false;
50+
} catch (Exception e) {
51+
throw new RuntimeException(e);
52+
}
53+
}
54+
}

Project/src/main/java/com/jaz2/figurines/Owner/Owner.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.jaz2.figurines.Owner;
22

3+
import com.jaz2.figurines.Dictionary.Dictionary;
34
import jakarta.persistence.Entity;
45
import jakarta.persistence.Id;
56
import lombok.Data;
@@ -13,6 +14,7 @@ public class Owner {
1314
@Id
1415
@UuidGenerator
1516
private UUID id;
17+
@Dictionary("Name")
1618
private String name;
1719
private String surname;
1820
private String email;

Project/src/main/java/com/jaz2/figurines/Owner/OwnerService.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@
33
import com.jaz2.figurines.Exceptions.OwnerNotFoundException;
44
import com.jaz2.figurines.Exceptions.BadOwnerFieldsException;
55
import jakarta.transaction.Transactional;
6+
import jakarta.validation.Validator;
67
import lombok.RequiredArgsConstructor;
78

9+
import org.springframework.beans.factory.annotation.Autowired;
810
import org.springframework.stereotype.Service;
911

1012
import java.util.List;
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.jaz2.figurines;
2+
3+
import org.springframework.context.annotation.Bean;
4+
import org.springframework.context.annotation.Configuration;
5+
import org.springframework.validation.beanvalidation.LocalValidatorFactoryBean;
6+
7+
@Configuration
8+
public class ValidationConfig {
9+
10+
@Bean
11+
public LocalValidatorFactoryBean validatorFactoryBean() {
12+
return new LocalValidatorFactoryBean();
13+
}
14+
}

Project/src/main/resources/application.properties

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,5 @@ spring.datasource.driver-class-name= org.h2.Driver
55
spring.datasource.username=sa
66
spring.datasource.password=
77
spring.jpa.hibernate.ddl-auto=create
8+
spring.jpa.properties.jakarta.persistence.validation.mode=none
9+
spring.jpa.properties.javax.persistence.validation.mode=none

Project/src/main/resources/import.sql

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
-- Insert Dictionary
2+
INSERT INTO Dictionary_Entity (id, VALUE_NAME, DICTIONARY_NAME) VALUES (UUID(), 'John', 'Name');
3+
INSERT INTO Dictionary_Entity (id, VALUE_NAME, DICTIONARY_NAME) VALUES (UUID(), 'Jane', 'Name');
4+
INSERT INTO Dictionary_Entity (id, VALUE_NAME, DICTIONARY_NAME) VALUES (UUID(), 'Alice', 'Name');
5+
16
-- Insert owner
27
INSERT INTO Owner (id, name, surname, email, address, phone) VALUES (UUID(), 'John', 'Doe', '[email protected]', '123 Main St', '555-1234');
38
INSERT INTO Owner (id, name, surname, email, address, phone) VALUES (UUID(), 'Jane', 'Doe', '[email protected]', '456 Main St', '555-5678');

0 commit comments

Comments
 (0)