JR Utily

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

1 -package org.legrog.presentation;
2 -
3 -import org.legrog.application.PublisherRevisionService;
4 -import org.legrog.application.PublisherService;
5 -import org.legrog.entities.Publisher;
6 -import org.legrog.entities.PublisherRevision;
7 -import org.slf4j.Logger;
8 -import org.slf4j.LoggerFactory;
9 -
10 -import javax.annotation.PostConstruct;
11 -import javax.enterprise.context.RequestScoped;
12 -import javax.inject.Inject;
13 -import javax.inject.Named;
14 -import java.util.ArrayList;
15 -import java.util.Iterator;
16 -import java.util.List;
17 -
18 -@Named
19 -@RequestScoped
20 -public class ListPublisherRevisionsView {
21 - Logger logger = LoggerFactory.getLogger(getClass());
22 -
23 - @Inject
24 - PublisherRevisionService publisherRevisionService;
25 -
26 - private Integer publisherId;
27 -
28 - private boolean viewAll;
29 -
30 - private List<PublisherRevision> publisherRevisions;
31 -
32 - public List<PublisherRevision> getPublisherRevisions() {
33 - return publisherRevisions;
34 - }
35 -/*
36 - if (!viewAll) {
37 - logger.info("!viewAll");
38 - ArrayList<PublisherRevision> filteredPublisherRevisions= new ArrayList<PublisherRevision>();
39 - Iterator<PublisherRevision> publisherRevisionIterator = filteredPublisherRevisions.iterator();
40 - while (publisherRevisionIterator.hasNext()) {
41 - PublisherRevision publisherRevision = publisherRevisionIterator.next();
42 - if (publisherRevision.getPublisher().getPublisherId() == publisherId.intValue()) {
43 - filteredPublisherRevisions.add(publisherRevision);
44 - }
45 - }
46 - publisherRevisions = filteredPublisherRevisions;
47 - }
48 -*/
49 - @PostConstruct
50 - public void init() {
51 - publisherRevisions = publisherRevisionService.getAllPublisherRevisions();
52 - logger.info("init");
53 - }
54 -
55 - public void setView() {
56 - logger.info("setView");
57 - logger.info("publisherId = " + publisherId);
58 - viewAll = false;
59 - if (publisherId == null) {
60 - viewAll = true;
61 - }
62 - }
63 -
64 - public Integer getPublisherId() {
65 - return publisherId;
66 - }
67 -
68 - public void setPublisherId(Integer publisherId) {
69 - this.publisherId = publisherId;
70 - }
71 -
72 - public boolean isViewAll() {
73 - return viewAll;
74 - }
75 -}
1 -package org.legrog.web.publisher;
2 -
3 -import org.legrog.entities.Publisher;
4 -import org.legrog.entities.PublisherRevision;
5 -import org.slf4j.Logger;
6 -import org.slf4j.LoggerFactory;
7 -
8 -import javax.annotation.PostConstruct;
9 -import javax.enterprise.context.RequestScoped;
10 -import javax.faces.context.FacesContext;
11 -import javax.inject.Inject;
12 -import javax.inject.Named;
13 -import java.util.ArrayList;
14 -import java.util.List;
15 -import java.util.Map;
16 -
17 -/*
18 - Vue de listPublisherRevisions.xhtml
19 - Permet de voir l'ensemble des versions des éditeurs.
20 - Permet de voir l'ensemble des versions d'un éditeur et de valider l'une d'elles.
21 - */
22 -@Named
23 -@RequestScoped
24 -public class ListPublisherRevisionsBean {
25 - Logger logger = LoggerFactory.getLogger(getClass());
26 -
27 - @Inject
28 - PublisherService publisherService;
29 -
30 - private Integer publisherId;
31 -
32 - private boolean viewAll;
33 -
34 - private List<PublisherRevision> publisherRevisions;
35 -
36 - public void validate() {
37 - logger.debug("validate");
38 - Publisher publisher = publisherService.getPublisher(publisherId);
39 - logger.debug("publisher (pre-validate) = {}", publisher);
40 - Map<String,String> params = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap();
41 - Integer publisherRevisionId = new Integer(params.get("publisherRevisionId"));
42 -
43 - publisherService.validatePublisherRevision(publisher, publisherRevisionId);
44 -// TODO : comprendre pourquoi ceci est nécessaire
45 - publisherRevisions = publisherService.getAllPublisherRevisions();
46 - publisherRevisions = filter();
47 - logger.debug("publisher (post-validate) = {}", publisher);
48 - }
49 -
50 - @PostConstruct
51 - public void init() {
52 - publisherRevisions = publisherService.getAllPublisherRevisions();
53 - logger.debug("init");
54 - }
55 -
56 - protected List<PublisherRevision> filter() {
57 - ArrayList<PublisherRevision> filteredPublisherRevisions= new ArrayList<PublisherRevision>();
58 -
59 - for(PublisherRevision publisherRevision : publisherRevisions) {
60 - if (publisherRevision.getPublisher().getPublisherId() == publisherId) {
61 - filteredPublisherRevisions.add(publisherRevision);
62 - }
63 - }
64 - return filteredPublisherRevisions;
65 - }
66 -
67 - public void setView() {
68 - logger.debug("setView");
69 - logger.debug("publisherId = " + publisherId);
70 - viewAll = ( publisherId == null ) ;
71 - if (!viewAll) {
72 - logger.debug("!viewAll");
73 - publisherRevisions = filter();
74 - }
75 - }
76 -
77 - // get, set et is
78 - public List<PublisherRevision> getPublisherRevisions() {
79 - return publisherRevisions;
80 - }
81 -
82 - public Integer getPublisherId() {
83 - return publisherId;
84 - }
85 -
86 - public void setPublisherId(Integer publisherId) {
87 - this.publisherId = publisherId;
88 - }
89 -
90 - public boolean isViewAll() {
91 - return viewAll;
92 - }
93 -}
...@@ -5,12 +5,11 @@ import org.legrog.entities.PublisherVersion; ...@@ -5,12 +5,11 @@ import org.legrog.entities.PublisherVersion;
5 import org.slf4j.Logger; 5 import org.slf4j.Logger;
6 import org.slf4j.LoggerFactory; 6 import org.slf4j.LoggerFactory;
7 7
8 -import javax.enterprise.context.RequestScoped; 8 +import javax.faces.view.ViewScoped;
9 -import javax.faces.context.FacesContext;
10 import javax.inject.Inject; 9 import javax.inject.Inject;
11 import javax.inject.Named; 10 import javax.inject.Named;
11 +import java.io.Serializable;
12 import java.util.List; 12 import java.util.List;
13 -import java.util.Map;
14 import java.util.stream.Collectors; 13 import java.util.stream.Collectors;
15 14
16 /* 15 /*
...@@ -19,8 +18,8 @@ import java.util.stream.Collectors; ...@@ -19,8 +18,8 @@ import java.util.stream.Collectors;
19 Permet de voir l'ensemble des versions d'un éditeur et de valider l'une d'elles. 18 Permet de voir l'ensemble des versions d'un éditeur et de valider l'une d'elles.
20 */ 19 */
21 @Named 20 @Named
22 -@RequestScoped 21 +@ViewScoped
23 -public class ListPublisherVersionsView { 22 +public class ListPublisherVersionsView implements Serializable {
24 23
25 Logger logger = LoggerFactory.getLogger(getClass()); 24 Logger logger = LoggerFactory.getLogger(getClass());
26 25
...@@ -35,6 +34,7 @@ public class ListPublisherVersionsView { ...@@ -35,6 +34,7 @@ public class ListPublisherVersionsView {
35 this.publisherService = publisherService; 34 this.publisherService = publisherService;
36 } 35 }
37 36
37 +
38 // View Action being executed at view loading 38 // View Action being executed at view loading
39 public void setView() { 39 public void setView() {
40 logger.trace("setView"); 40 logger.trace("setView");
...@@ -54,12 +54,10 @@ public class ListPublisherVersionsView { ...@@ -54,12 +54,10 @@ public class ListPublisherVersionsView {
54 54
55 } 55 }
56 56
57 - public void validate() { 57 + public void validate(Integer publisherVersionId) {
58 logger.trace("validate"); 58 logger.trace("validate");
59 Publisher publisher = publisherService.getPublisher(publisherId); 59 Publisher publisher = publisherService.getPublisher(publisherId);
60 logger.debug("publisher (pre-validate) = {}", publisher); 60 logger.debug("publisher (pre-validate) = {}", publisher);
61 - Map<String,String> params = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap();
62 - Integer publisherVersionId = new Integer(params.get("publisherVersionId"));
63 61
64 publisherService.validatePublisherVersion(publisher, publisherVersionId); 62 publisherService.validatePublisherVersion(publisher, publisherVersionId);
65 63
......
...@@ -39,9 +39,8 @@ ...@@ -39,9 +39,8 @@
39 <p jsf:rendered="#{version.publisherVersionId == version.publisher.validatedVersion.publisherVersionId}">Validé</p> 39 <p jsf:rendered="#{version.publisherVersionId == version.publisher.validatedVersion.publisherVersionId}">Validé</p>
40 <p jsf:rendered="#{listPublisherVersionsView.viewAll and version.publisherVersionId != version.publisher.validatedVersion.publisherVersionId}">Non validé</p> 40 <p jsf:rendered="#{listPublisherVersionsView.viewAll and version.publisherVersionId != version.publisher.validatedVersion.publisherVersionId}">Non validé</p>
41 <!-- https://www.mkyong.com/jsf2/4-ways-to-pass-parameter-from-jsf-page-to-backing-bean/ f:param, problème avec Method expression --> 41 <!-- https://www.mkyong.com/jsf2/4-ways-to-pass-parameter-from-jsf-page-to-backing-bean/ f:param, problème avec Method expression -->
42 - <form jsf:id="validateVersion" action="" jsf:rendered="#{not listPublisherVersionsView.viewAll and version.publisherVersionId != version.publisher.validatedVersion.publisherVersionId}"> 42 + <form action="" jsf:rendered="#{not listPublisherVersionsView.viewAll and version.publisherVersionId != version.publisher.validatedVersion.publisherVersionId}">
43 - <button jsf:action="#{listPublisherVersionsView.validate}">Valider 43 + <button jsf:action="#{listPublisherVersionsView.validate(version.publisherVersionId)}">Valider
44 - <f:param name="publisherVersionId" value="#{version.publisherVersionId}"/>
45 </button> 44 </button>
46 </form> 45 </form>
47 </h:column> 46 </h:column>
......