Skip to content

Commit 2fe7852

Browse files
Fix select procedures without id
add override id option
1 parent ff3c2b5 commit 2fe7852

File tree

6 files changed

+131
-10
lines changed

6 files changed

+131
-10
lines changed
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package main.annotations;
2+
3+
4+
import java.lang.annotation.ElementType;
5+
import java.lang.annotation.Retention;
6+
import java.lang.annotation.RetentionPolicy;
7+
import java.lang.annotation.Target;
8+
9+
@Target(value= ElementType.FIELD)
10+
@Retention(value= RetentionPolicy.RUNTIME)
11+
public @interface OverrideIDName {
12+
}

src/main/java/main/model/db/dao/DAO.java

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -104,16 +104,36 @@ public T getEntityById(Integer id) throws AqualityException {
104104
}
105105
}
106106

107+
/**
108+
* Get single entity by id and project_id
109+
* @param entity entity with id and project_id
110+
* @return entity
111+
*/
112+
public T getEntityById(T entity) throws AqualityException {
113+
List<Pair<String, String>> parameters = entity.getIdAndProjectIdSearchParameters();
114+
List<T> all = dtoMapper.mapObjects(CallStoredProcedure(select, parameters).toString());
115+
if(!all.isEmpty()) {
116+
return all.get(0);
117+
}
118+
else{
119+
throw new AqualityException("No Entities was found by id");
120+
}
121+
}
122+
107123
/**
108124
* Update entity
109125
* @param entity with fields that should be updated (id is required)
110126
* @return Updated entity
111127
*/
112128
public T update(T entity) throws AqualityException {
113129
try {
114-
getEntityById(entity.getId());
130+
if(entity.hasProjectId()){
131+
getEntityById(entity);
132+
} else {
133+
getEntityById(entity.getIdOrOverrideId());
134+
}
115135
} catch (AqualityException e) {
116-
throw new AqualityParametersException("Entity with specified '%s' id does not exist!", entity.getId());
136+
throw new AqualityParametersException("Entity with specified '%s' id does not exist!", entity.getIdOrOverrideId());
117137
}
118138

119139
List<Pair<String, String>> parameters = entity.getParameters();
@@ -145,7 +165,7 @@ public boolean delete(T entity) throws AqualityException {
145165
public T create(T entity) throws AqualityException {
146166
Integer id = null;
147167
try {
148-
id = entity.getId();
168+
id = entity.getIdOrOverrideId();
149169
} catch (AqualityException e) {
150170
// entity has no id
151171
}

src/main/java/main/model/db/dao/audit/AuditorsDao.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
public class AuditorsDao extends DAO<AuditorDto> {
1212
public AuditorsDao() {
1313
super(AuditorDto.class);
14-
select = "{call SELECT_AUDITOR(?,?)}";
14+
select = "{call SELECT_AUDITOR(?,?,?)}";
1515
insert = "{call INSERT_AUDITOR(?,?)}";
1616
remove = "{call REMOVE_AUDITOR(?)}";
1717
}

src/main/java/main/model/dto/AuditorDto.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ public class AuditorDto extends UserDto {
1515
@DataBaseInsert
1616
@DataBaseSearchable
1717
private Integer id;
18+
@DataBaseSearchable
19+
@OverrideIDName
1820
@DataBaseName(name="request_auditor_id")
1921
@DataBaseID
2022
private Integer auditor_id;

src/main/java/main/model/dto/BaseDto.java

Lines changed: 69 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -80,16 +80,50 @@ public List<Pair<String, String>> getDataBaseIDParameters() throws AqualityExcep
8080
return list;
8181
}
8282

83+
public List<Pair<String, String>> getIdAndProjectIdSearchParameters() throws AqualityException {
84+
List<Pair<String, String>> list = new ArrayList<>();
85+
List<Field> classFields = this.getClassFields();
86+
for (Field field : classFields) {
87+
DataBaseName nameAnnotation = field.getAnnotation(DataBaseName.class);
88+
DataBaseSearchable searchAnnotation = field.getAnnotation(DataBaseSearchable.class);
89+
if (nameAnnotation != null && searchAnnotation != null) {
90+
try {
91+
field.setAccessible(true);
92+
String value = "";
93+
if (Objects.equals(field.getName(), "id") || Objects.equals(field.getName(), "project_id")) {
94+
value = getStringValue(field.get(this));
95+
}
96+
if (nameAnnotation.name().equals("request_limit") && (value.equals("0") || value.equals(""))) {
97+
value = "100000";
98+
}
99+
Pair<String, String> pair = new Pair<>(nameAnnotation.name(), value);
100+
list.add(pair);
101+
102+
} catch (IllegalAccessException e) {
103+
throw new AqualityException(String.format("Cannot read Field: %s", field.getName()));
104+
}
105+
}
106+
}
107+
108+
if (list.isEmpty()) {
109+
throw new AqualityException("Entity has no id parameter");
110+
}
111+
112+
return list;
113+
}
114+
83115
public List<Pair<String, String>> getIdSearchParameters(Integer id) throws AqualityException {
84116
List<Pair<String, String>> list = new ArrayList<>();
85117
List<Field> classFields = this.getClassFields();
118+
boolean hasOverrideIdAnnotation = hasOverrideIdAnnotation(OverrideIDName.class);
86119
for (Field field: classFields) {
87120
DataBaseName nameAnnotation = field.getAnnotation(DataBaseName.class);
88121
DataBaseSearchable searchAnnotation = field.getAnnotation(DataBaseSearchable.class);
122+
OverrideIDName override = field.getAnnotation(OverrideIDName.class);
89123
if(nameAnnotation != null && searchAnnotation != null){
90124
field.setAccessible(true);
91125
String value = "";
92-
if(Objects.equals(field.getName(), "id")) {
126+
if((Objects.equals(field.getName(), "id") && !hasOverrideIdAnnotation) || override != null) {
93127
value = id.toString();
94128
}
95129
if(nameAnnotation.name().equals("request_limit") && (value.equals("0") || value.equals(""))){
@@ -107,24 +141,43 @@ public List<Pair<String, String>> getIdSearchParameters(Integer id) throws Aqual
107141
return list;
108142
}
109143

110-
public Integer getId() throws AqualityException {
144+
public Integer getIdOrOverrideId() throws AqualityException {
111145
List<Field> classFields = this.getClassFields();
146+
boolean hasOverrideIdAnnotation = hasOverrideIdAnnotation(OverrideIDName.class);
112147
for (Field field: classFields) {
113148
field.setAccessible(true);
114-
if(Objects.equals(field.getName(), "id")) {
115-
Object value;
149+
OverrideIDName override = field.getAnnotation(OverrideIDName.class);
150+
if((Objects.equals(field.getName(), "id") && !hasOverrideIdAnnotation) || override != null) {
151+
String value;
116152
try {
117-
value = field.get(this);
153+
value = getStringValue(field.get(this));
118154
} catch (IllegalAccessException e) {
119155
throw new AqualityException(String.format("Cannot read Field: %s", field.getName()));
120156
}
121-
return Integer.valueOf(getStringValue(value));
157+
return value.isEmpty() ? null : Integer.valueOf(value);
122158
}
123159
}
124160

125161
throw new AqualityException("Entity has no id parameter");
126162
}
127163

164+
public boolean hasProjectId() throws AqualityException {
165+
List<Field> classFields = this.getClassFields();
166+
for (Field field: classFields) {
167+
field.setAccessible(true);
168+
if(Objects.equals(field.getName(), "project_id")) {
169+
Object value;
170+
try {
171+
value = field.get(this);
172+
} catch (IllegalAccessException e) {
173+
throw new AqualityException(String.format("Cannot read Field: %s", field.getName()));
174+
}
175+
return !getStringValue(value).isEmpty();
176+
}
177+
}
178+
return false;
179+
}
180+
128181
public void getSearchTemplateFromRequestParameters(@NotNull HttpServletRequest req) throws AqualityException {
129182
getTemplate(req, DataBaseSearchable.class);
130183
}
@@ -160,6 +213,16 @@ private boolean hasIdAnnotation(Class<DataBaseID> dataBaseIDClass){
160213
return false;
161214
}
162215

216+
private boolean hasOverrideIdAnnotation(Class<OverrideIDName> dataBaseIDClass){
217+
List<Field> classFields = this.getClassFields();
218+
for (Field field: classFields) {
219+
if(field.getAnnotation(dataBaseIDClass) != null){
220+
return true;
221+
}
222+
}
223+
return false;
224+
}
225+
163226
private List<Field> getClassFields(){
164227
List<Field> declaredFields = new ArrayList<>();
165228
Class<?> superclass = this.getClass();

src/main/resources/db_changelog/db.changelog-0.3.8.xml

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,4 +160,28 @@
160160
</rollback>
161161
</changeSet>
162162

163+
<changeSet id="Update SELECT_AUDITOR to use id" author="v.kostyukevich">
164+
<sql endDelimiter="#">
165+
166+
DROP procedure IF EXISTS `SELECT_AUDITOR`;
167+
168+
#
169+
CREATE PROCEDURE `SELECT_AUDITOR`(
170+
In request_audit_id varchar(11),
171+
In request_user_id varchar(11),
172+
In request_auditor_id varchar(11)
173+
)
174+
BEGIN
175+
SELECT * FROM auditor_assignments
176+
LEFT JOIN users ON auditor_assignments.user_id=users.id
177+
WHERE empty_or_equal(request_audit_id, auditor_assignments.audit_id)
178+
AND empty_or_equal(request_user_id, auditor_assignments.user_id)
179+
AND empty_or_equal(request_auditor_id, auditor_assignments.auditor_id)
180+
;
181+
END
182+
</sql>
183+
<rollback>
184+
</rollback>
185+
</changeSet>
186+
163187
</databaseChangeLog>

0 commit comments

Comments
 (0)