com.levalo.contacts.view.ContactView.java Source code

Java tutorial

Introduction

Here is the source code for com.levalo.contacts.view.ContactView.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.levalo.contacts.view;

import com.levalo.contacts.ejb.ContactService;
import com.levalo.contacts.ejb.ServiceException;
import com.levalo.contacts.entity.contact.Contact;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.io.StringWriter;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.annotation.PostConstruct;
import javax.enterprise.context.RequestScoped;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.faces.event.ValueChangeEvent;
import javax.inject.Inject;
import javax.inject.Named;
import javax.servlet.http.Part;
import org.apache.commons.io.IOUtils;
import org.primefaces.model.DefaultStreamedContent;
import org.primefaces.model.StreamedContent;

/**
 *
 * @author Levan
 */

@Named
@RequestScoped
public class ContactView {
    @Inject
    private ContactService contactService;
    @Inject
    private Logger logger;

    private Contact contact;

    public void doResetForm() {
        this.init();
    }

    public List<Contact> doGetUserContacts() {
        try {
            List<Contact> contacts = contactService.getUserContacts();
            return contacts;
        } catch (ServiceException ex) {
            logger.log(Level.WARNING, ex.getMessage());
            return null;
        }
    }

    public void doCreateOrUpdateContact() {
        try {
            contactService.createOrUpdate(this.contact);
            this.init();
        } catch (ServiceException ex) {
            logger.log(Level.WARNING, ex.getMessage());
        }
    }

    //shesacvlelia
    public void doGetContact(Contact contact) {
        try {
            this.contact = contactService.getContactById(contact);
        } catch (ServiceException ex) {
            logger.log(Level.WARNING, ex.getMessage());
        }
    }

    public void doRemoveContact(Contact contact) {
        try {
            contactService.remove(contact);
        } catch (ServiceException ex) {
            logger.log(Level.WARNING, ex.getMessage());
        }
    }

    public StreamedContent doExportContactToXml(Contact contact) {
        try {
            String xmlContactString = contactService.getXmlFromContact(contact);
            InputStream xmlContactStream = new ByteArrayInputStream(xmlContactString.getBytes());
            String title = contact.getFirstname() + "_" + contact.getLastname() + ".xml";
            StreamedContent xmlContact = new DefaultStreamedContent(xmlContactStream, "text/xml", title);
            return xmlContact;
        } catch (Exception ex) {
            logger.log(Level.WARNING, ex.getMessage());
            return null;
        }
    }

    public void importContactFromXml(ValueChangeEvent event) {
        try {
            Part uploadedXmlContact = (Part) event.getNewValue();
            StringWriter sw = new StringWriter();
            IOUtils.copy(uploadedXmlContact.getInputStream(), sw);
            this.contact = contactService.getContactFromXml(sw.toString());
        } catch (Exception ex) {
            FacesContext.getCurrentInstance().addMessage(null,
                    new FacesMessage(FacesMessage.SEVERITY_ERROR, "Error", "Invalid xml file"));
            logger.log(Level.INFO, ex.getMessage());
        }
    }

    public Contact getContact() {
        return contact;
    }

    public void setContact(Contact contact) {
        this.contact = contact;
    }

    @PostConstruct
    private void init() {
        contact = new Contact();
    }
}