delete documents
This commit is contained in:
parent
6ae60fad8c
commit
27ee382f4a
@ -102,4 +102,11 @@ public class AdminController extends CommonController {
|
|||||||
service.updateDocument(bean);
|
service.updateDocument(bean);
|
||||||
return "redirect:/admin/document";
|
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";
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,7 +1,9 @@
|
|||||||
package de.jottyfan.camporganizer.module.admin;
|
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_DOCUMENT;
|
||||||
import static de.jottyfan.camporganizer.db.jooq.Tables.T_DOCUMENTROLE;
|
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.ArrayList;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
@ -19,6 +21,7 @@ import org.jooq.InsertReturningStep;
|
|||||||
import org.jooq.Record5;
|
import org.jooq.Record5;
|
||||||
import org.jooq.SelectSeekStep1;
|
import org.jooq.SelectSeekStep1;
|
||||||
import org.jooq.UpdateConditionStep;
|
import org.jooq.UpdateConditionStep;
|
||||||
|
import org.jooq.UpdateSetMoreStep;
|
||||||
import org.jooq.exception.DataAccessException;
|
import org.jooq.exception.DataAccessException;
|
||||||
import org.jooq.impl.DSL;
|
import org.jooq.impl.DSL;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
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.EnumCamprole;
|
||||||
import de.jottyfan.camporganizer.db.jooq.enums.EnumDocument;
|
import de.jottyfan.camporganizer.db.jooq.enums.EnumDocument;
|
||||||
import de.jottyfan.camporganizer.db.jooq.enums.EnumFiletype;
|
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.TDocumentRecord;
|
||||||
import de.jottyfan.camporganizer.db.jooq.tables.records.TDocumentroleRecord;
|
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;
|
import de.jottyfan.camporganizer.module.camplist.LambdaResultWrapper;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -125,14 +130,19 @@ public class AdminRepository {
|
|||||||
jooq.transaction(c -> {
|
jooq.transaction(c -> {
|
||||||
Integer pk = bean.getPk();
|
Integer pk = bean.getPk();
|
||||||
if (bean.getPk() != null) {
|
if (bean.getPk() != null) {
|
||||||
UpdateConditionStep<TDocumentRecord> sql = DSL.using(c)
|
UpdateSetMoreStep<TDocumentRecord> sqlPart = DSL.using(c)
|
||||||
// @formatter:off
|
// @formatter:off
|
||||||
.update(T_DOCUMENT)
|
.update(T_DOCUMENT)
|
||||||
.set(T_DOCUMENT.NAME, bean.getName())
|
.set(T_DOCUMENT.NAME, bean.getName())
|
||||||
.set(T_DOCUMENT.DOCTYPE, bean.getDoctype())
|
.set(T_DOCUMENT.DOCTYPE, bean.getDoctype());
|
||||||
.set(T_DOCUMENT.DOCUMENT, bean.getDocument())
|
// @formatter:on
|
||||||
.set(T_DOCUMENT.FILETYPE, bean.getFiletype())
|
if (bean.getDocument() != null) {
|
||||||
.where(T_DOCUMENT.PK.eq(bean.getPk()));
|
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
|
// @formatter:on
|
||||||
LOGGER.debug("{}", sql.toString());
|
LOGGER.debug("{}", sql.toString());
|
||||||
lrw.add(sql.execute());
|
lrw.add(sql.execute());
|
||||||
@ -183,4 +193,53 @@ public class AdminRepository {
|
|||||||
});
|
});
|
||||||
return lrw.getCounter();
|
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();
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -1,9 +1,12 @@
|
|||||||
package de.jottyfan.camporganizer.module.admin;
|
package de.jottyfan.camporganizer.module.admin;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import javax.validation.Valid;
|
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.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
@ -17,6 +20,7 @@ import de.jottyfan.camporganizer.module.mail.MailRepository;
|
|||||||
*/
|
*/
|
||||||
@Service
|
@Service
|
||||||
public class AdminService {
|
public class AdminService {
|
||||||
|
private static final Logger LOGGER = LogManager.getLogger(AdminService.class);
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
private MailRepository mailRepository;
|
private MailRepository mailRepository;
|
||||||
@ -54,6 +58,21 @@ public class AdminService {
|
|||||||
* @return the number of affected database lines
|
* @return the number of affected database lines
|
||||||
*/
|
*/
|
||||||
public Integer updateDocument(@Valid DocumentBean bean) {
|
public Integer updateDocument(@Valid DocumentBean bean) {
|
||||||
|
try {
|
||||||
|
bean.encodeUpload();
|
||||||
|
} catch (IOException e) {
|
||||||
|
LOGGER.error(e.getMessage());
|
||||||
|
}
|
||||||
return adminRepository.upsert(bean);
|
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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -5,11 +5,11 @@ import java.io.InputStream;
|
|||||||
import java.io.Serializable;
|
import java.io.Serializable;
|
||||||
import java.util.Base64;
|
import java.util.Base64;
|
||||||
|
|
||||||
import javax.servlet.http.Part;
|
|
||||||
import javax.validation.constraints.NotBlank;
|
import javax.validation.constraints.NotBlank;
|
||||||
import javax.validation.constraints.NotNull;
|
import javax.validation.constraints.NotNull;
|
||||||
|
|
||||||
import org.apache.commons.io.IOUtils;
|
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.EnumCamprole;
|
||||||
import de.jottyfan.camporganizer.db.jooq.enums.EnumDocument;
|
import de.jottyfan.camporganizer.db.jooq.enums.EnumDocument;
|
||||||
@ -33,9 +33,14 @@ public class DocumentBean implements Serializable{
|
|||||||
@NotNull
|
@NotNull
|
||||||
private EnumFiletype filetype;
|
private EnumFiletype filetype;
|
||||||
@NotNull
|
@NotNull
|
||||||
private Part uploadfile;
|
private MultipartFile uploadfile;
|
||||||
private EnumCamprole[] roles;
|
private EnumCamprole[] roles;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* convert the uploadfile to the document
|
||||||
|
*
|
||||||
|
* @throws IOException on IO errors
|
||||||
|
*/
|
||||||
public void encodeUpload() throws IOException {
|
public void encodeUpload() throws IOException {
|
||||||
if (uploadfile != null) {
|
if (uploadfile != null) {
|
||||||
InputStream inputStream = uploadfile.getInputStream();
|
InputStream inputStream = uploadfile.getInputStream();
|
||||||
@ -82,11 +87,11 @@ public class DocumentBean implements Serializable{
|
|||||||
return filetype;
|
return filetype;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Part getUploadfile() {
|
public MultipartFile getUploadfile() {
|
||||||
return uploadfile;
|
return uploadfile;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setUploadfile(Part uploadfile) {
|
public void setUploadfile(MultipartFile uploadfile) {
|
||||||
this.uploadfile = uploadfile;
|
this.uploadfile = uploadfile;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -104,10 +104,10 @@ div {
|
|||||||
|
|
||||||
.mainpage {
|
.mainpage {
|
||||||
background-color: rgba(255, 255, 255, 0.4);
|
background-color: rgba(255, 255, 255, 0.4);
|
||||||
padding: 8px;
|
padding: 16px;
|
||||||
overflow: auto;
|
overflow: auto;
|
||||||
width: 100%;
|
width: 100%;
|
||||||
height: calc(100vh - 60px);
|
height: calc(100vh - 72px);
|
||||||
/* 60 px is the current height of the headline; TODO: calculate it */
|
/* 60 px is the current height of the headline; TODO: calculate it */
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -76,13 +76,16 @@
|
|||||||
<div class="col-sm-10">
|
<div class="col-sm-10">
|
||||||
<input type="submit" class="btn btn-success" value="Ok" />
|
<input type="submit" class="btn btn-success" value="Ok" />
|
||||||
<a th:href="@{/admin/document}" class="btn btn-outline-secondary">Abbrechen</a>
|
<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>
|
||||||
</div>
|
</div>
|
||||||
<!-- TODO:
|
|
||||||
|
|
||||||
delete from db th:if="${bean.pk}"
|
|
||||||
-->
|
|
||||||
|
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</form>
|
</form>
|
||||||
|
Loading…
x
Reference in New Issue
Block a user