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 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515
| package com.consoft.jsf;
import com.consoft.entity.Client;
import com.consoft.entity.Reference;
import com.consoft.jsf.Controller;
import com.consoft.stateless.client.ClientBeanRemote;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
//import javax.sql.rowset.serial.SerialBlob;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
//import java.awt.Graphics2D;
//import java.awt.Image;
//import java.io.File;
import java.io.IOException;
//import java.io.InputStream;
//import java.io.OutputStream;
import java.io.Serializable;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.faces.event.ValueChangeEvent;
import javax.faces.model.SelectItem;
//import javax.imageio.ImageIO;
//import java.awt.image.BufferedImage;
import org.ajax4jsf.io.parser.BufferedStringState;
import org.apache.myfaces.custom.fileupload.UploadedFile;
import org.jboss.logging.Logger;
public class ClientController extends Controller implements Serializable {
/**
*
*/
Logger l = Logger.getLogger(ClientController.class);
private static final long serialVersionUID = 1L;
// Init
// ----------------------------------------------------------------------
// -----------------
private UploadedFile uploadedFile;
private String fileName;
// Actions
// ------------------------------------------------------------------
// ------------------
// ======================================
// = Attributs =
// ======================================
private Client client = new Client();
private String keyword;
private List<Client> clients;
private Map<Integer, Boolean> selectedIds = new HashMap<Integer, Boolean>();
private Boolean logocheck;
private Boolean inputLogo;
private List<Client> selectedClients;
private List<SelectItem> result = new ArrayList<SelectItem>();
ClientBeanRemote TB() throws NamingException {
// Context jndiContext = getInitialContext();
Context jndiContext;
jndiContext = new InitialContext();
Object ref = jndiContext.lookup("sam/ClientBean/remote");
ClientBeanRemote clientBean = (ClientBeanRemote) ref;
return clientBean;
}
// ======================================
// = Constantes =
// ======================================
// ======================================
// = Constructeurs =
// ======================================
// ======================================
// = Methodes publiques =
// ======================================
public String doCreateClient() {
// final String mname = "doCreateClient";
// logger.entering(cname, mname);
String navigateTo = null;
try {
// If the client Exist (have the same name)
List<Client> Exist = null;
Exist = TB().searchClients(client.getNom());
if (!(Exist.size() > 0 && Exist != null)) {
if (uploadedFile.getContentType().contains("image")) {
// Create the Client
client.setLogo(uploadedFile.getBytes());
client.setNombreref(0);
client.setLogotype(uploadedFile.getContentType());
// View the cntent type of uploaded file
System.out.println(uploadedFile.getContentType());
client = TB().createClient(client);
navigateTo = doFindClients();
// Supprimer l'instance de client
// Show succes message.
FacesContext.getCurrentInstance().addMessage(
"uploadForm",
new FacesMessage(FacesMessage.SEVERITY_INFO,
"Client ajouté avec succés!", null));
} else {
FacesContext.getCurrentInstance().addMessage(
"uploadForm",
new FacesMessage(FacesMessage.SEVERITY_ERROR,
"Le logo choisit n'est pas une image!",
null));
}
} else {
// Show succes message.
FacesContext.getCurrentInstance().addMessage(
"uploadForm",
new FacesMessage(FacesMessage.SEVERITY_ERROR,
"Le client existe déja!", null));
}
} catch (IOException e) {
// Show error message.
FacesContext.getCurrentInstance().addMessage(
"uploadForm",
new FacesMessage(FacesMessage.SEVERITY_ERROR,
"File upload failed with I/O error.", null));
// Always log stacktraces.
e.printStackTrace();
} catch (Exception e) {
System.out.println(e);// addMessage(cname, mname, e);
}
// logger.exiting(cname, mname, navigateTo);
return navigateTo;
}
// Ldap
public String doAuthentificate() {
String navigateTo = null;
// client instance clear attribut
Clear();
try {
if (TB().Authentificate())
navigateTo = "clients.displayed";
else
navigateTo = "technologies.displayed";
} catch (Exception e) {
logger.warning(e.getMessage());
e.printStackTrace();
// addMessage(cname, mname, e);
}
// logger.exiting(cname, mname, navigateTo);
return navigateTo;
}
// ldap
public String doFindClients() {
// final String mname = "doFindClients";
// logger.entering(cname, mname);
String navigateTo = null;
// client instance clear attribut
Clear();
try {
//
// category = catalogBean.findCategory(getParamId("categoryId"));
// Rechercher tous les clients
clients = TB().findClients();
navigateTo = "clients.displayed";
} catch (Exception e) {
logger.warning(e.getMessage());
e.printStackTrace();
// addMessage(cname, mname, e);
}
// logger.exiting(cname, mname, navigateTo);
return navigateTo;
}
/*
* doShowClient() used when we try to update they lookup for the client
*/
public String doShowClient() {
String navigateTo = null;
try {
// ligne provisoire
clients = TB().findClients();
client = TB().findClient(getParamId("clientId"));
// description = technologie.getDescription();
navigateTo = "client.showed";
} catch (Exception e) {
// addMessage(cname, mname, e);
}
/*
* System.out.println(client.getNom());
* System.out.println(client.getSiteweb());
* System.out.println(client.getLogotype());
* System.out.println(getParamId("clientId"));
*/
return navigateTo;
}
public Client doFindClient(int clientId) {
Client client = null;
try {
client = TB().findClient(clientId);
} catch (Exception e) {
// addMessage(cname, mname, e);
System.out.println("Error While looking for Client" + e);
e.printStackTrace();
}
/*
* System.out.println(client.getNom());
* System.out.println(client.getSiteweb());
* System.out.println(client.getLogotype());
* System.out.println(getParamId("clientId"));
*/
return client;
}
public String doUpdateClient() {
// final String mname = "doUpdateClient";
// logger.entering(cname, mname);
String navigateTo = null;
try {
List<Client> Exist = null;
Exist = TB().searchClients(client.getNom());
boolean occurence = false;
for (Client c : Exist) {
boolean equals = c.getNom().equalsIgnoreCase(client.getNom());
if (c.getNoclient() != client.getNoclient() && equals)
occurence = true;
}
if (occurence == false) {
// Update the Client
System.out.println("aloooorrr" + logocheck.toString());
if (logocheck) {
if (uploadedFile.getContentType().contains("image")) {
client.setLogo(uploadedFile.getBytes());
client.setLogotype(uploadedFile.getContentType());
client = TB().updateClient(client);
// Show succes message.
FacesContext.getCurrentInstance().addMessage(
"uploadForm",
new FacesMessage(FacesMessage.SEVERITY_INFO,
"Client modifié avec succés!", null));
client.setNoclient(0);
navigateTo = doFindClients();
} else {
FacesContext.getCurrentInstance().addMessage(
"uploadForm",
new FacesMessage(FacesMessage.SEVERITY_ERROR,
"Le logo choisit n'est pas une image!",
null));
}
} else {
client = TB().updateClient(client);
// Show succes message.
FacesContext.getCurrentInstance().addMessage(
"uploadForm",
new FacesMessage(FacesMessage.SEVERITY_INFO,
"Client modifié avec succés!", null));
client.setNoclient(0);
navigateTo = doFindClients();
}
// to refresh the list of clients
// navigateTo = "client.updated";
} else {
FacesContext.getCurrentInstance().addMessage(
"uploadForm",
new FacesMessage(FacesMessage.SEVERITY_ERROR,
"Le client existe déja !", null));
}
} catch (Exception e) {
// addMessage(cname, mname, e);
e.printStackTrace();
FacesContext.getCurrentInstance().addMessage(
"uploadForm",
new FacesMessage(FacesMessage.SEVERITY_ERROR,
"File upload failed with I/O error.", null));
}
// logger.exiting(cname, mname, navigateTo);
return navigateTo;
}
public String doSearch() {
// final String mname = "doSearch";
// logger.entering(cname, mname);
String navigateTo = null;
try {
clients = TB().searchClients(keyword);
navigateTo = "clients.found";
} catch (Exception e) {
// addMessage(cname, mname, e);
}
// logger.exiting(cname, mname, navigateTo);
return navigateTo;
}
public String DeleteSelectedClients() {
String navigateTo = null;
List<Reference> listr = null;
ReferenceController rc = new ReferenceController();
boolean msg = false;
StringBuffer erreur = new StringBuffer();
try {
for (Client clt : clients) {
if (selectedIds.containsKey(clt.getNoclient())) {
if (selectedIds.get(clt.getNoclient()).booleanValue() == true) {
// Verify whether the client is attached to a reference
listr = rc.TB().searchClientReferences(
clt.getNoclient());
if (listr.size() > 0) {
// remove from selected Ids
selectedIds.remove(clt.getNoclient());
erreur.append(clt.getNom() + " | ");
msg = true;
} else {
// Delete the client
TB().deleteClient(clt);
navigateTo = "client.deleted";
}
}
}
}
// Message d'erreur
if (msg)
FacesContext
.getCurrentInstance()
.addMessage(
"clientForm",
new FacesMessage(
FacesMessage.SEVERITY_ERROR,
"Impossible de supprimer un client appartenant à une référence! ",
null));
} catch (Exception e) {
// addMessage(cname, mname, e);
}
navigateTo = doFindClients();
return navigateTo; // Navigation case.
}
public List<SelectItem> doFillClients() {
try {
clients = TB().findClients();
for (Client client : clients) {
// SelectItem item = new
// SelectItem(client.getNoclient(),client.getNom());
// En passe le client et son nom
SelectItem item = new SelectItem(client.getNoclient(),
client.getNom());
result.add(item);
}
} catch (Exception e) {
// addMessage(cname, mname, e);
}
return result;
}
public void changeInputLogo(ValueChangeEvent value) {
setInputLogo(!(Boolean) value.getNewValue());
FacesContext.getCurrentInstance().renderResponse();
}
// Clear pour vider les champs d'un client
public void Clear() {
this.keyword = null;
getClient().setNoclient(0);
getClient().setLogotype(null);
getClient().setNom(null);
getClient().setLogo(null);
getClient().setSiteweb(null);
setLogocheck(false);
setInputLogo(true);
}
// //////////////////////////////////////////////
// }
// ======================================
// = Methodes Protégées =
// ======================================
// ======================================
// = Accesseurs =
// ======================================
public void addSelectedClient(Client clt) {
selectedClients.add(clt);
}
public Map<Integer, Boolean> getSelectedIds() {
return selectedIds;
}
public void setSelectedIds(Map<Integer, Boolean> selectedIds) {
this.selectedIds = selectedIds;
}
public Client getClient() {
return client;
}
public void setClient(Client client) {
this.client = client;
}
public String getKeyword() {
return keyword;
}
public void setKeyword(String keyword) {
this.keyword = keyword;
}
public UploadedFile getUploadedFile() {
return uploadedFile;
}
public String getFileName() {
return fileName;
}
public void setUploadedFile(UploadedFile uploadedFile) {
this.uploadedFile = uploadedFile;
}
// ======================================
// = Methodes Privées =
// ======================================
public List<Client> getClients() {
return clients;
}
public Boolean getLogocheck() {
return logocheck;
}
public void setLogocheck(Boolean logocheck) {
this.logocheck = logocheck;
}
public void setClients(List<Client> clients) {
this.clients = clients;
}
public Boolean getInputLogo() {
return inputLogo;
}
public void setInputLogo(Boolean inputLogo) {
this.inputLogo = inputLogo;
}
// ======================================
// = Methodes hash, equals, toString =
// ======================================
public static Context getInitialContext()
throws javax.naming.NamingException {
return new javax.naming.InitialContext();
}
} |
Partager