JSF faire un CRUD dans un seul manageBean
Bonjour,
Je viens vers vous car j'ai un soucis sur comment anticiper la chose que je vais vous expliquer :
Comment est-ce que je peux fusionner ces deux classes en une seule ? C'est à dire "ArticleAddBean.java" + "ArticleReadBean.java" = "ArticleBean.java" tout en faisant la distinction de ce que je souhaite initialiser dans mon @PostConstrut init() (par exemple si je suis dans "monsite/views/add/article.xhtml" j'initialise tout ce qui concerne l'ajout d'un nouvelle article dans le init() mais je ne m'occupe pas du tout de ce qui concerne la lecture d'un article ? Je dois faire une condition genre if(monURL.contain("add/user")) User user = new User(); else ... ?
Voiçi un exemple de mes 2 classes ArticleAddBean.java et ArticleReadBean.java:
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
| package com.og.manageBean;
import java.io.Serializable;
import javax.annotation.PostConstruct;
import javax.faces.context.FacesContext;
import javax.faces.view.ViewScoped;
import javax.inject.Inject;
import javax.inject.Named;
import javax.servlet.http.HttpServletRequest;
import com.og.model.Article;
import com.og.service.GenericService;
import com.og.util.ServiceException;
@Named
@ViewScoped
public class ArticleReadBean implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
// ARTICLE
private Article article;
// GENERIC SERVICE
@Inject
private transient GenericService service;
@PostConstruct
public void init() {
// GET ARTICLE BY ID (FROM PARAM)
HttpServletRequest request = (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext()
.getRequest();
try {
this.article = service.get(Article.class, Integer.parseInt(request.getParameter("id")));
} catch (NumberFormatException e) {
System.out.println(e.getMessage());
} catch (ServiceException e) {
System.out.println(e.getMessage());
}
}
/**
* @return the article
*/
public Article getArticle() {
return article;
}
/**
* @param article
* the article to set
*/
public void setArticle(Article article) {
this.article = article;
}
} |
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249
| package com.og.manageBean;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.faces.view.ViewScoped;
import javax.inject.Inject;
import javax.inject.Named;
import com.og.model.Article;
import com.og.model.ArticleCountry;
import com.og.model.ArticleFolder;
import com.og.model.ArticleImage;
import com.og.model.ArticleSupport;
import com.og.model.ArticleTag;
import com.og.model.Author;
import com.og.model.Country;
import com.og.model.DateTime;
import com.og.model.Folder;
import com.og.model.Image;
import com.og.model.Support;
import com.og.model.Tag;
import com.og.service.GenericService;
import com.og.util.ServiceException;
@Named
@ViewScoped
public class ArticleAddBean implements Serializable {
private static final long serialVersionUID = 1L;
// INIT CHOICE LIST (TO FORM)
private List<Author> authors;
private List<Country> countries;
private List<Folder> folders;
private List<Image> images;
private List<Support> supports;
private List<Tag> tags;
// INIT CHOICED LIST (FROM FORM)
private List<Country> countriesSelected;
private List<Folder> foldersSelected;
private List<Image> imagesSelected;
private List<Support> supportsSelected;
private List<Tag> tagsSelected;
// INIT BEAN (TO SAVE)
@Inject
private Article article;
// INIT SERVICE
@Inject
private transient GenericService service;
@PostConstruct
public void init() {
// SET ARTICLE ACTIVE TRUE
this.article.setIsActive((byte) 1);
// SET CHOICE LIST (TO SELECT FROM FORM)
try {
this.images = this.service.getAll(Image.class);
this.supports = this.service.getAll(Support.class);
this.tags = this.service.getAll(Tag.class);
this.authors = this.service.getAll(Author.class);
this.folders = this.service.getAll(Folder.class);
this.countries = this.service.getAll(Country.class);
} catch (ServiceException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* CONSTRUCT AND ADD ARTICLE TO DB
*/
public void save() {
// INIT MANYTOMANY RELATIONSHIPS
List<ArticleCountry> ac = new ArrayList<ArticleCountry>();
List<ArticleFolder> af = new ArrayList<ArticleFolder>();
List<ArticleImage> ai = new ArrayList<ArticleImage>();
List<ArticleSupport> as = new ArrayList<ArticleSupport>();
List<ArticleTag> at = new ArrayList<ArticleTag>();
// SET MANYTOMANY RELATIONSHIPS
this.countriesSelected.forEach((n) -> ac.add(new ArticleCountry(n, this.article)));
this.foldersSelected.forEach((n) -> af.add(new ArticleFolder(n, this.article)));
this.imagesSelected.forEach((n) -> ai.add(new ArticleImage(n, this.article)));
this.supportsSelected.forEach((n) -> as.add(new ArticleSupport(n, this.article)));
this.tagsSelected.forEach((n) -> at.add(new ArticleTag(n, this.article)));
// SET MANYTOMANY RELATIONSHIPS TO ARTICLE
this.article.setArticleCountries(ac);
this.article.setArticleFolders(af);
this.article.setArticleImages(ai);
this.article.setArticleSupports(as);
this.article.setArticleTags(at);
// INIT/SET CURRENT DATETIME TO ARTICLE
this.article.setDateTime(new DateTime(this.article));
// ADD ARTICLE TO DB
try {
this.service.add(this.article);
FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_INFO, "Publication réussie",
"L'article a bien été publiée.");
FacesContext.getCurrentInstance().addMessage(null, message);
} catch (ServiceException e) {
System.out.println("Une erreur est survenue: " + e.getMessage());
}
}
/**
* @return the article
*/
public Article getArticle() {
return article;
}
/**
* @param article
* the article to set
*/
public void setArticle(Article article) {
this.article = article;
}
/**
* @return the authors
*/
public List<Author> getAuthors() {
return authors;
}
/**
* @return the folders
*/
public List<Folder> getFolders() {
return folders;
}
/**
* @return the images
*/
public List<Image> getImages() {
return images;
}
/**
* @return the tags
*/
public List<Tag> getTags() {
return tags;
}
/**
* @return the countries
*/
public List<Country> getCountries() {
return countries;
}
/**
* @return the supports
*/
public List<Support> getSupports() {
return supports;
}
/**
* @return the countriesSelected
*/
public List<Country> getCountriesSelected() {
return countriesSelected;
}
/**
* @param countriesSelected
* the countriesSelected to set
*/
public void setCountriesSelected(List<Country> countriesSelected) {
this.countriesSelected = countriesSelected;
}
/**
* @return the foldersSelected
*/
public List<Folder> getFoldersSelected() {
return foldersSelected;
}
/**
* @param foldersSelected
* the foldersSelected to set
*/
public void setFoldersSelected(List<Folder> foldersSelected) {
this.foldersSelected = foldersSelected;
}
/**
* @return the imagesSelected
*/
public List<Image> getImagesSelected() {
return imagesSelected;
}
/**
* @param imagesSelected
* the imagesSelected to set
*/
public void setImagesSelected(List<Image> imagesSelected) {
this.imagesSelected = imagesSelected;
}
/**
* @return the supportsSelected
*/
public List<Support> getSupportsSelected() {
return supportsSelected;
}
/**
* @param supportsSelected
* the supportsSelected to set
*/
public void setSupportsSelected(List<Support> supportsSelected) {
this.supportsSelected = supportsSelected;
}
/**
* @return the tagsSelected
*/
public List<Tag> getTagsSelected() {
return tagsSelected;
}
/**
* @param tagsSelected
* the tagsSelected to set
*/
public void setTagsSelected(List<Tag> tagsSelected) {
this.tagsSelected = tagsSelected;
}
} |
PS: si vous avez des remaques à faire sur mon code, je suis preneur.
Bien à vous.
RS-28