JR Utily

fix error in validation (@PostConstruct / @ViewedScope problem )

package org.legrog.presentation;
import org.legrog.application.PublisherRevisionService;
import org.legrog.application.PublisherService;
import org.legrog.entities.Publisher;
import org.legrog.entities.PublisherRevision;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.annotation.PostConstruct;
import javax.enterprise.context.RequestScoped;
import javax.inject.Inject;
import javax.inject.Named;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
@Named
@RequestScoped
public class ListPublisherRevisionsView {
Logger logger = LoggerFactory.getLogger(getClass());
@Inject
PublisherRevisionService publisherRevisionService;
private Integer publisherId;
private boolean viewAll;
private List<PublisherRevision> publisherRevisions;
public List<PublisherRevision> getPublisherRevisions() {
return publisherRevisions;
}
/*
if (!viewAll) {
logger.info("!viewAll");
ArrayList<PublisherRevision> filteredPublisherRevisions= new ArrayList<PublisherRevision>();
Iterator<PublisherRevision> publisherRevisionIterator = filteredPublisherRevisions.iterator();
while (publisherRevisionIterator.hasNext()) {
PublisherRevision publisherRevision = publisherRevisionIterator.next();
if (publisherRevision.getPublisher().getPublisherId() == publisherId.intValue()) {
filteredPublisherRevisions.add(publisherRevision);
}
}
publisherRevisions = filteredPublisherRevisions;
}
*/
@PostConstruct
public void init() {
publisherRevisions = publisherRevisionService.getAllPublisherRevisions();
logger.info("init");
}
public void setView() {
logger.info("setView");
logger.info("publisherId = " + publisherId);
viewAll = false;
if (publisherId == null) {
viewAll = true;
}
}
public Integer getPublisherId() {
return publisherId;
}
public void setPublisherId(Integer publisherId) {
this.publisherId = publisherId;
}
public boolean isViewAll() {
return viewAll;
}
}
package org.legrog.web.publisher;
import org.legrog.entities.Publisher;
import org.legrog.entities.PublisherRevision;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.annotation.PostConstruct;
import javax.enterprise.context.RequestScoped;
import javax.faces.context.FacesContext;
import javax.inject.Inject;
import javax.inject.Named;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
/*
Vue de listPublisherRevisions.xhtml
Permet de voir l'ensemble des versions des éditeurs.
Permet de voir l'ensemble des versions d'un éditeur et de valider l'une d'elles.
*/
@Named
@RequestScoped
public class ListPublisherRevisionsBean {
Logger logger = LoggerFactory.getLogger(getClass());
@Inject
PublisherService publisherService;
private Integer publisherId;
private boolean viewAll;
private List<PublisherRevision> publisherRevisions;
public void validate() {
logger.debug("validate");
Publisher publisher = publisherService.getPublisher(publisherId);
logger.debug("publisher (pre-validate) = {}", publisher);
Map<String,String> params = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap();
Integer publisherRevisionId = new Integer(params.get("publisherRevisionId"));
publisherService.validatePublisherRevision(publisher, publisherRevisionId);
// TODO : comprendre pourquoi ceci est nécessaire
publisherRevisions = publisherService.getAllPublisherRevisions();
publisherRevisions = filter();
logger.debug("publisher (post-validate) = {}", publisher);
}
@PostConstruct
public void init() {
publisherRevisions = publisherService.getAllPublisherRevisions();
logger.debug("init");
}
protected List<PublisherRevision> filter() {
ArrayList<PublisherRevision> filteredPublisherRevisions= new ArrayList<PublisherRevision>();
for(PublisherRevision publisherRevision : publisherRevisions) {
if (publisherRevision.getPublisher().getPublisherId() == publisherId) {
filteredPublisherRevisions.add(publisherRevision);
}
}
return filteredPublisherRevisions;
}
public void setView() {
logger.debug("setView");
logger.debug("publisherId = " + publisherId);
viewAll = ( publisherId == null ) ;
if (!viewAll) {
logger.debug("!viewAll");
publisherRevisions = filter();
}
}
// get, set et is
public List<PublisherRevision> getPublisherRevisions() {
return publisherRevisions;
}
public Integer getPublisherId() {
return publisherId;
}
public void setPublisherId(Integer publisherId) {
this.publisherId = publisherId;
}
public boolean isViewAll() {
return viewAll;
}
}
......@@ -5,12 +5,11 @@ import org.legrog.entities.PublisherVersion;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.enterprise.context.RequestScoped;
import javax.faces.context.FacesContext;
import javax.faces.view.ViewScoped;
import javax.inject.Inject;
import javax.inject.Named;
import java.io.Serializable;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
/*
......@@ -19,8 +18,8 @@ import java.util.stream.Collectors;
Permet de voir l'ensemble des versions d'un éditeur et de valider l'une d'elles.
*/
@Named
@RequestScoped
public class ListPublisherVersionsView {
@ViewScoped
public class ListPublisherVersionsView implements Serializable {
Logger logger = LoggerFactory.getLogger(getClass());
......@@ -35,6 +34,7 @@ public class ListPublisherVersionsView {
this.publisherService = publisherService;
}
// View Action being executed at view loading
public void setView() {
logger.trace("setView");
......@@ -54,12 +54,10 @@ public class ListPublisherVersionsView {
}
public void validate() {
public void validate(Integer publisherVersionId) {
logger.trace("validate");
Publisher publisher = publisherService.getPublisher(publisherId);
logger.debug("publisher (pre-validate) = {}", publisher);
Map<String,String> params = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap();
Integer publisherVersionId = new Integer(params.get("publisherVersionId"));
publisherService.validatePublisherVersion(publisher, publisherVersionId);
......
......@@ -39,9 +39,8 @@
<p jsf:rendered="#{version.publisherVersionId == version.publisher.validatedVersion.publisherVersionId}">Validé</p>
<p jsf:rendered="#{listPublisherVersionsView.viewAll and version.publisherVersionId != version.publisher.validatedVersion.publisherVersionId}">Non validé</p>
<!-- https://www.mkyong.com/jsf2/4-ways-to-pass-parameter-from-jsf-page-to-backing-bean/ f:param, problème avec Method expression -->
<form jsf:id="validateVersion" action="" jsf:rendered="#{not listPublisherVersionsView.viewAll and version.publisherVersionId != version.publisher.validatedVersion.publisherVersionId}">
<button jsf:action="#{listPublisherVersionsView.validate}">Valider
<f:param name="publisherVersionId" value="#{version.publisherVersionId}"/>
<form action="" jsf:rendered="#{not listPublisherVersionsView.viewAll and version.publisherVersionId != version.publisher.validatedVersion.publisherVersionId}">
<button jsf:action="#{listPublisherVersionsView.validate(version.publisherVersionId)}">Valider
</button>
</form>
</h:column>
......