Code development platform for open source projects from the European Union institutions :large_blue_circle: EU Login authentication by SMS will be completely phased out by mid-2025. To see alternatives please check here

Skip to content
Snippets Groups Projects
Commit 49967538 authored by Joze RIHTARSIC's avatar Joze RIHTARSIC
Browse files

Pull request #40: Synchronize create payload version

Merge in EDELIVERY/smp from bugfix/EDELIVERY-11680-smp-edit-resources-resource-documents-created-through-api-have-document to development

* commit '5e9593a0':
  Synchronize create payload version
parents c256f53c 5e9593a0
Branches EDELIVERY-12495-update-eupl-licence-for-ddc
No related tags found
No related merge requests found
Pipeline #114164 passed with warnings
Showing
with 81 additions and 48 deletions
import {SearchTableEntity} from '../search-table/search-table-entity.model';
import {VisibilityEnum} from "../enums/visibility.enum";
export interface DocumentRo {
mimeType?: string;
name?: string;
......@@ -8,6 +5,7 @@ export interface DocumentRo {
currentResourceVersion?:number;
allVersions?: number[];
payloadVersion?:number;
payloadCreatedOn?: Date;
payload?:string;
}
......@@ -35,20 +35,29 @@
<div [formGroup]="documentForm"
style="float: right; vertical-align:middle;display: flex;align-items: center;justify-content: center; gap:0.4em;padding-right: 10px">
<span style="font-size: 0.8em">Show version:</span>
<select matNativeControl style="width: 100px; border-bottom: gray solid 1px"
placeholder="All document version"
matTooltip="Select version to display."
formControlName="payloadVersion"
id="document version_id"
(change)="onSelectionDocumentVersionChanged()"
<select matNativeControl style="width: 100px; border-bottom: grey solid 1px"
placeholder="All document version"
matTooltip="Select version to display."
formControlName="payloadVersion"
id="document version_id"
(change)="onSelectionDocumentVersionChanged()"
>
<option *ngFor="let version of getDocumentVersions"
[value]="version"
[value]="version"
>
{{version}}
</option>
</select>
<span style="font-size: 0.8em">Created on:</span>
<input id="payloadCreatedOn_id"
matInput [ngxMatDatetimePicker]="payloadCreatedOnPicker"
formControlName="payloadCreatedOn"
readonly>
<mat-datepicker-toggle matSuffix [for]="payloadCreatedOnPicker" style="visibility: hidden"></mat-datepicker-toggle>
<ngx-mat-datetime-picker #payloadCreatedOnPicker [showSpinners]="true" [showSeconds]="false"
[hideTime]="false"></ngx-mat-datetime-picker>
</div>
</mat-toolbar-row>
</mat-toolbar>
......
......@@ -56,6 +56,7 @@ export class ResourceDocumentPanelComponent implements AfterViewInit, BeforeLeav
'mimeType': new FormControl({value: null}),
'name': new FormControl({value: null}),
'currentResourceVersion': new FormControl({value: null}),
'payloadCreatedOn': new FormControl({value: null}),
'payloadVersion': new FormControl({value: null}),
'payload': new FormControl({value: null}),
});
......@@ -109,6 +110,7 @@ export class ResourceDocumentPanelComponent implements AfterViewInit, BeforeLeav
this.documentForm.controls['name'].setValue(value.name);
this.documentForm.controls['currentResourceVersion'].setValue(value.currentResourceVersion);
this.documentForm.controls['payloadVersion'].setValue(value.payloadVersion);
this.documentForm.controls['payloadCreatedOn'].setValue(value.payloadCreatedOn);
this.documentForm.controls['payload'].setValue(!value.payload?"":value.payload);
this.documentForm.controls['payload'].enable();
// the method documentVersionsExists already uses the current value to check if versions exists
......@@ -122,6 +124,7 @@ export class ResourceDocumentPanelComponent implements AfterViewInit, BeforeLeav
this.documentForm.controls['payload'].setValue("");
this.documentForm.controls['currentResourceVersion'].setValue("");
this.documentForm.controls['payloadVersion'].setValue("");
this.documentForm.controls['payloadCreatedOn'].setValue("");
this.documentForm.controls['payload'].setValue("");
}
this.documentForm.markAsPristine();
......
......@@ -33,7 +33,7 @@
<div [formGroup]="documentForm"
style="float: right; vertical-align:middle;display: flex;align-items: center;justify-content: center; gap:0.4em;padding-right: 10px">
<span style="font-size: 0.8em">Show version:</span>
<select matNativeControl style="width: 100px; border-bottom: gray solid 1px"
<select matNativeControl style="width: 100px; border-bottom: grey solid 1px"
placeholder="All document version"
matTooltip="Select version to display."
formControlName="payloadVersion"
......@@ -45,8 +45,15 @@
>
{{version}}
</option>
</select>
<span style="font-size: 0.8em">Created on:</span>
<input id="payloadCreatedOn_id"
matInput [ngxMatDatetimePicker]="payloadCreatedOnPicker"
formControlName="payloadCreatedOn"
readonly>
<mat-datepicker-toggle matSuffix [for]="payloadCreatedOnPicker" style="visibility: hidden"></mat-datepicker-toggle>
<ngx-mat-datetime-picker #payloadCreatedOnPicker [showSpinners]="true" [showSeconds]="false"
[hideTime]="false"></ngx-mat-datetime-picker>
</div>
</mat-toolbar-row>
</mat-toolbar>
......
......@@ -72,6 +72,7 @@ export class SubresourceDocumentPanelComponent implements AfterViewInit, BeforeL
'name': new FormControl({value: null}),
'currentResourceVersion': new FormControl({value: null}),
'payloadVersion': new FormControl({value: null}),
'payloadCreatedOn': new FormControl({value: null}),
'payload': new FormControl({value: null}),
});
this.documentForm.controls['payload'].setValue("")
......@@ -145,6 +146,7 @@ export class SubresourceDocumentPanelComponent implements AfterViewInit, BeforeL
this.documentForm.controls['name'].setValue(value.name);
this.documentForm.controls['currentResourceVersion'].setValue(value.currentResourceVersion);
this.documentForm.controls['payloadVersion'].setValue(value.payloadVersion);
this.documentForm.controls['payloadCreatedOn'].setValue(value.payloadCreatedOn);
this.documentForm.controls['payload'].setValue(!value.payload?"":value.payload);
this.documentForm.controls['payload'].enable();
......@@ -159,6 +161,7 @@ export class SubresourceDocumentPanelComponent implements AfterViewInit, BeforeL
this.documentForm.controls['payload'].setValue("");
this.documentForm.controls['currentResourceVersion'].setValue("");
this.documentForm.controls['payloadVersion'].setValue("");
this.documentForm.controls['payloadCreatedOn'].setValue("");
this.documentForm.controls['payload'].setValue("");
}
this.documentForm.markAsPristine();
......
......@@ -103,10 +103,9 @@ public class DBDocument extends BaseEntity {
protected int getNextVersionIndex(){
List<DBDocumentVersion> list = getDocumentVersions();
return list.stream()
return list.stream()
.map(DBDocumentVersion::getVersion)
.reduce(-1, (a, b) -> Integer.max(a, b))
.intValue() + 1;
.reduce(0, (a, b) -> Integer.max(a, b)) + 1;
}
......
package eu.europa.ec.edelivery.smp.data.ui;
import java.time.OffsetDateTime;
import java.util.ArrayList;
import java.util.List;
......@@ -12,6 +13,7 @@ public class DocumentRo {
String name;
Integer payloadVersion;
String payload;
OffsetDateTime payloadCreatedOn;
public String getDocumentId() {
return documentId;
......@@ -67,4 +69,12 @@ public class DocumentRo {
public void setPayload(String payload) {
this.payload = payload;
}
public OffsetDateTime getPayloadCreatedOn() {
return payloadCreatedOn;
}
public void setPayloadCreatedOn(OffsetDateTime payloadCreatedOn) {
this.payloadCreatedOn = payloadCreatedOn;
}
}
......@@ -78,32 +78,25 @@ public class UIDocumentService {
RequestData data = resourceHandlerService.buildRequestDataForResource(domainResourceDef.getDomain(),
resource, null);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ResponseData responseData = new SpiResponseData(bos);
try {
resourceHandler.generateResource(data, responseData, Collections.emptyList());
} catch (ResourceException e) {
throw new SMPRuntimeException(ErrorCode.INVALID_REQUEST, "StoreResourceValidation", ExceptionUtils.getRootCauseMessage(e));
}
String genDoc = new String(bos.toByteArray());
LOG.info("Generate document [{}]", genDoc);
DocumentRo result = new DocumentRo();
result.setPayload(genDoc);
return result;
return getDocumentRo(resourceHandler, data);
}
@Transactional
public DocumentRo generateDocumentForSubresource(Long subresourceId, Long resourceId, DocumentRo documentRo) {
LOG.info("Generate document");
DBResource parentEntity = resourceDao.find(resourceId);
DBSubresource enitity = subresourceDao.find(subresourceId);
DBSubresourceDef subresourceDef = enitity.getSubresourceDef();
DBSubresource entity = subresourceDao.find(subresourceId);
DBSubresourceDef subresourceDef = entity.getSubresourceDef();
ResourceHandlerSpi resourceHandler = resourceHandlerService.getSubresourceHandler(subresourceDef, subresourceDef.getResourceDef());
RequestData data = resourceHandlerService.buildRequestDataForSubResource(parentEntity.getDomainResourceDef().getDomain(),
parentEntity, enitity);
parentEntity, entity);
return getDocumentRo(resourceHandler, data);
}
private DocumentRo getDocumentRo(ResourceHandlerSpi resourceHandler, RequestData data) {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ResponseData responseData = new SpiResponseData(bos);
try {
......@@ -111,7 +104,7 @@ public class UIDocumentService {
} catch (ResourceException e) {
throw new SMPRuntimeException(ErrorCode.INVALID_REQUEST, "StoreResourceValidation", ExceptionUtils.getRootCauseMessage(e));
}
String genDoc = new String(bos.toByteArray());
String genDoc = bos.toString();
LOG.info("Generate document [{}]", genDoc);
DocumentRo result = new DocumentRo();
result.setPayload(genDoc);
......@@ -163,9 +156,9 @@ public class UIDocumentService {
* return version, if version does not exists return current version. if current version does not exists
* return last version
*
* @param resourceId
* @param version
* @return
* @param resourceId resource id of the document
* @param version version of the payload for the document
* @return DocumentRo with payload and version
*/
@Transactional
public DocumentRo getDocumentForResource(Long resourceId, int version) {
......@@ -198,6 +191,9 @@ public class UIDocumentService {
documentVersion.setVersion(version + 1);
documentVersion.setDocument(document);
documentVersion.setContent(baos.toByteArray());
// to get the current persist time
documentVersion.prePersist();
document.getDocumentVersions().add(documentVersion);
document.setCurrentVersion(documentVersion.getVersion());
return convert(document, documentVersion);
......@@ -231,6 +227,7 @@ public class UIDocumentService {
documentRo.setName(document.getName());
documentRo.setCurrentResourceVersion(document.getCurrentVersion());
if (version != null) {
documentRo.setPayloadCreatedOn(version.getCreatedOn());
documentRo.setPayloadVersion(version.getVersion());
documentRo.setPayload(new String(version.getContent()));
}
......
......@@ -29,7 +29,7 @@ public class DocumentDaoTest extends AbstractBaseDao {
assertNotNull(document.getId());
assertEquals(2, document.getDocumentVersions().size());
assertEquals(1, document.getCurrentVersion());
assertEquals(2, document.getCurrentVersion());
}
......@@ -40,7 +40,7 @@ public class DocumentDaoTest extends AbstractBaseDao {
assertTrue(result.isPresent());
assertEquals(testUtilsDao.getDocumentD1G1RD1(), result.get());
// the default setup createResources sets two versions (0 and 1 ) with current version 1
assertEquals(1, result.get().getCurrentVersion());
assertEquals(2, result.get().getCurrentVersion());
}
......@@ -58,7 +58,7 @@ public class DocumentDaoTest extends AbstractBaseDao {
assertTrue(result.isPresent());
// the default setup createResources sets two versions (0 and 1 ) with current version 1
assertEquals(1, result.get().getVersion());
assertEquals(2, result.get().getVersion());
assertEquals(testUtilsDao.getDocumentD1G1RD1().getDocumentVersions().get(1), result.get());
}
......@@ -76,8 +76,8 @@ public class DocumentDaoTest extends AbstractBaseDao {
Optional<DBDocumentVersion> result = testInstance.getCurrentDocumentVersionForSubresource(testUtilsDao.getSubresourceD1G1RD1_S1());
assertTrue(result.isPresent());
// the default setup createResources sets two versions (0 and 1 ) with current version 1
assertEquals(1, result.get().getVersion());
// the default setup createResources sets two versions (1 and 2 ) with current version 2
assertEquals(2, result.get().getVersion());
assertEquals(testUtilsDao.getDocumentD1G1RD1_S1().getDocumentVersions().get(1), result.get());
}
}
......@@ -63,16 +63,18 @@ public class ResourceDaoTest extends AbstractBaseDao {
Assert.assertTrue(optResult.isPresent());
Assert.assertNotNull(optResult.get().getDocument());
Assert.assertNotNull(optResult.get().getDocument().getId());
Assert.assertEquals(0, optResult.get().getDocument().getCurrentVersion());
Assert.assertEquals(1, optResult.get().getDocument().getCurrentVersion());
Assert.assertEquals(1, optResult.get().getDocument().getDocumentVersions().size());
Assert.assertNotNull(optResult.get().getDocument().getDocumentVersions().get(0).getId());
Assert.assertEquals(0, optResult.get().getDocument().getDocumentVersions().get(0).getVersion());
Assert.assertEquals(1, optResult.get().getDocument().getDocumentVersions().get(0).getVersion());
}
@Test
@Transactional
public void persistNewVersionToResourceWithDocument() {
Optional<DBResource> optResource = testInstance.getResource(TEST_SG_ID_1, TEST_SG_SCHEMA_1, testUtilsDao.getResourceDefSmp(), testUtilsDao.getD1());
Optional<DBResource> optResource = testInstance.getResource(TEST_SG_ID_1, TEST_SG_SCHEMA_1,
testUtilsDao.getResourceDefSmp(), testUtilsDao.getD1());
Assert.assertTrue(optResource.isPresent());
DBResource resource = testInstance.find(optResource.get().getId());
int docCount = resource.getDocument().getDocumentVersions().size();
......
......@@ -25,6 +25,8 @@ import org.springframework.transaction.annotation.Transactional;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import java.util.Optional;
import static eu.europa.ec.edelivery.smp.testutil.TestConstants.*;
import static eu.europa.ec.edelivery.smp.testutil.TestDBUtils.*;
import static org.junit.jupiter.api.Assertions.assertEquals;
......@@ -248,9 +250,15 @@ public class TestUtilsDao {
@Transactional
public void deactivateUser(String username) {
DBUser user = userDao.findUserByUsername(username).get();
Optional<DBUser> userOpt = userDao.findUserByUsername(username);
if (!userOpt.isPresent()) {
LOG.warn("User [{}] not found and cannot be deactivated!", username);
return;
}
DBUser user = userOpt.get();
user.setActive(false);
persistFlushDetach(user);
}
......@@ -490,7 +498,7 @@ public class TestUtilsDao {
assertNotNull(document.getDocumentVersions().get(i).getId());
}
// current version is the last version
assertEquals(versions-1, document.getCurrentVersion());
assertEquals(versions, document.getCurrentVersion());
return document;
}
......@@ -615,9 +623,6 @@ public class TestUtilsDao {
return memEManager.find(clazz, id);
}
public void clear() {
memEManager.clear();
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment