delete documents

This commit is contained in:
Jottyfan 2023-01-29 21:13:12 +01:00
parent 6ae60fad8c
commit 27ee382f4a
6 changed files with 109 additions and 16 deletions

View File

@ -102,4 +102,11 @@ public class AdminController extends CommonController {
service.updateDocument(bean);
return "redirect:/admin/document";
}
@GetMapping("/admin/document/delete/{id}")
public String deleteDocument(@PathVariable Integer id, Model model, HttpServletRequest request) {
super.setupSession(model, request);
service.deleteDocument(id);
return "redirect:/admin/document";
}
}

View File

@ -1,7 +1,9 @@
package de.jottyfan.camporganizer.module.admin;
import static de.jottyfan.camporganizer.db.jooq.Tables.T_CAMP;
import static de.jottyfan.camporganizer.db.jooq.Tables.T_DOCUMENT;
import static de.jottyfan.camporganizer.db.jooq.Tables.T_DOCUMENTROLE;
import static de.jottyfan.camporganizer.db.jooq.Tables.T_LOCATION;
import java.util.ArrayList;
import java.util.Arrays;
@ -19,6 +21,7 @@ import org.jooq.InsertReturningStep;
import org.jooq.Record5;
import org.jooq.SelectSeekStep1;
import org.jooq.UpdateConditionStep;
import org.jooq.UpdateSetMoreStep;
import org.jooq.exception.DataAccessException;
import org.jooq.impl.DSL;
import org.springframework.beans.factory.annotation.Autowired;
@ -28,8 +31,10 @@ import org.springframework.transaction.annotation.Transactional;
import de.jottyfan.camporganizer.db.jooq.enums.EnumCamprole;
import de.jottyfan.camporganizer.db.jooq.enums.EnumDocument;
import de.jottyfan.camporganizer.db.jooq.enums.EnumFiletype;
import de.jottyfan.camporganizer.db.jooq.tables.records.TCampRecord;
import de.jottyfan.camporganizer.db.jooq.tables.records.TDocumentRecord;
import de.jottyfan.camporganizer.db.jooq.tables.records.TDocumentroleRecord;
import de.jottyfan.camporganizer.db.jooq.tables.records.TLocationRecord;
import de.jottyfan.camporganizer.module.camplist.LambdaResultWrapper;
/**
@ -125,12 +130,17 @@ public class AdminRepository {
jooq.transaction(c -> {
Integer pk = bean.getPk();
if (bean.getPk() != null) {
UpdateConditionStep<TDocumentRecord> sql = DSL.using(c)
UpdateSetMoreStep<TDocumentRecord> sqlPart = DSL.using(c)
// @formatter:off
.update(T_DOCUMENT)
.set(T_DOCUMENT.NAME, bean.getName())
.set(T_DOCUMENT.DOCTYPE, bean.getDoctype())
.set(T_DOCUMENT.DOCUMENT, bean.getDocument())
.set(T_DOCUMENT.DOCTYPE, bean.getDoctype());
// @formatter:on
if (bean.getDocument() != null) {
sqlPart = sqlPart.set(T_DOCUMENT.DOCUMENT, bean.getDocument());
}
// @formatter:off
UpdateConditionStep<TDocumentRecord> sql = sqlPart
.set(T_DOCUMENT.FILETYPE, bean.getFiletype())
.where(T_DOCUMENT.PK.eq(bean.getPk()));
// @formatter:on
@ -183,4 +193,53 @@ public class AdminRepository {
});
return lrw.getCounter();
}
/**
* delete entry from t_document where pk = ?
*
* @param pk
* to be used as reference
* @return number of affected database lines
* @throws DataAccessException
*/
public Integer deleteDocument(Integer pk) throws DataAccessException {
LambdaResultWrapper lrw = new LambdaResultWrapper();
jooq.transaction(t -> {
UpdateConditionStep<TCampRecord> sql = DSL.using(t)
// @formatter:off
.update(T_CAMP)
.set(T_CAMP.FK_DOCUMENT, (Integer) null)
.where(T_CAMP.FK_DOCUMENT.eq(pk));
// @formatter:on
LOGGER.debug("{}", sql.toString());
lrw.add(sql.execute());
UpdateConditionStep<TLocationRecord> sql1 = DSL.using(t)
// @formatter:off
.update(T_LOCATION)
.set(T_LOCATION.FK_DOCUMENT, (Integer) null)
.where(T_LOCATION.FK_DOCUMENT.eq(pk));
// @formatter:on
LOGGER.debug("{}", sql1.toString());
lrw.add(sql1.execute());
DeleteConditionStep<TDocumentroleRecord> sql2 = DSL.using(t)
// @formatter:off
.deleteFrom(T_DOCUMENTROLE)
.where(T_DOCUMENTROLE.FK_DOCUMENT.eq(pk));
// @formatter:on
LOGGER.debug("{}", sql2.toString());
lrw.add(sql2.execute());
DeleteConditionStep<TDocumentRecord> sql3 = DSL.using(t)
// @formatter:off
.deleteFrom(T_DOCUMENT)
.where(T_DOCUMENT.PK.eq(pk));
// @formatter:on
LOGGER.debug("{}", sql3.toString());
lrw.add(sql3.execute());
});
return lrw.getCounter();
}
}

View File

@ -1,9 +1,12 @@
package de.jottyfan.camporganizer.module.admin;
import java.io.IOException;
import java.util.List;
import javax.validation.Valid;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@ -17,6 +20,7 @@ import de.jottyfan.camporganizer.module.mail.MailRepository;
*/
@Service
public class AdminService {
private static final Logger LOGGER = LogManager.getLogger(AdminService.class);
@Autowired
private MailRepository mailRepository;
@ -54,6 +58,21 @@ public class AdminService {
* @return the number of affected database lines
*/
public Integer updateDocument(@Valid DocumentBean bean) {
try {
bean.encodeUpload();
} catch (IOException e) {
LOGGER.error(e.getMessage());
}
return adminRepository.upsert(bean);
}
/**
* delete document with id from the database
*
* @param id the ID of the document
* @return the number of affected database lines
*/
public Integer deleteDocument(Integer id) {
return adminRepository.deleteDocument(id);
}
}

View File

@ -5,11 +5,11 @@ import java.io.InputStream;
import java.io.Serializable;
import java.util.Base64;
import javax.servlet.http.Part;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import org.apache.commons.io.IOUtils;
import org.springframework.web.multipart.MultipartFile;
import de.jottyfan.camporganizer.db.jooq.enums.EnumCamprole;
import de.jottyfan.camporganizer.db.jooq.enums.EnumDocument;
@ -33,9 +33,14 @@ public class DocumentBean implements Serializable{
@NotNull
private EnumFiletype filetype;
@NotNull
private Part uploadfile;
private MultipartFile uploadfile;
private EnumCamprole[] roles;
/**
* convert the uploadfile to the document
*
* @throws IOException on IO errors
*/
public void encodeUpload() throws IOException {
if (uploadfile != null) {
InputStream inputStream = uploadfile.getInputStream();
@ -82,11 +87,11 @@ public class DocumentBean implements Serializable{
return filetype;
}
public Part getUploadfile() {
public MultipartFile getUploadfile() {
return uploadfile;
}
public void setUploadfile(Part uploadfile) {
public void setUploadfile(MultipartFile uploadfile) {
this.uploadfile = uploadfile;
}

View File

@ -104,10 +104,10 @@ div {
.mainpage {
background-color: rgba(255, 255, 255, 0.4);
padding: 8px;
padding: 16px;
overflow: auto;
width: 100%;
height: calc(100vh - 60px);
height: calc(100vh - 72px);
/* 60 px is the current height of the headline; TODO: calculate it */
}

View File

@ -76,13 +76,16 @@
<div class="col-sm-10">
<input type="submit" class="btn btn-success" value="Ok" />
<a th:href="@{/admin/document}" class="btn btn-outline-secondary">Abbrechen</a>
<div class="dropdown" style="display: inline">
<button class="btn btn-outline-danger dropdown-toggle" type="button" data-bs-toggle="dropdown" aria-expanded="false">
<i class="fas fa-trash-alt"></i>
</button>
<ul class="dropdown-menu">
<li><a class="dropdown-item" th:href="@{/admin/document/delete/{id}(id=${bean.pk})}">Dokument endgültig löschen</a>
</ul>
</div>
</div>
</div>
<!-- TODO:
delete from db th:if="${bean.pk}"
-->
</div>
</div>
</form>