com.webbfontaine.valuewebb.action.tt.TtLogger.java Source code

Java tutorial

Introduction

Here is the source code for com.webbfontaine.valuewebb.action.tt.TtLogger.java

Source

package com.webbfontaine.valuewebb.action.tt;

import com.webbfontaine.valuewebb.model.TtGen;
import com.webbfontaine.valuewebb.model.TtLog;
import com.webbfontaine.valuewebb.model.util.Utils;
import com.webbfontaine.valuewebb.model.validators.tt.ValidatorUtils;
import com.webbfontaine.valuewebb.utils.TTMailService;
import com.webbfontaine.valuewebb.validation.ErrorHandling;
import com.webbfontaine.valuewebb.validation.FacesError;
import com.webbfontaine.valuewebb.validation.FacesMessageText;
import org.apache.commons.lang3.StringUtils;
import org.jboss.seam.Component;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.bpm.Actor;

import javax.faces.context.FacesContext;
import javax.persistence.EntityManager;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import static com.webbfontaine.valuewebb.model.constants.Constants.*;

/**
 * Copyrights 2002-2010 Webb Fontaine
 * This software is the proprietary information of Webb Fontaine.
 * Its use is subject to License terms.
 * User: nigiyan
 * Date: Dec 3, 2010
 */

@Name("ttLogger")
public class TtLogger {

    /**
     * Adds new entry to log list of TT based on user entered message saved in 'log' variable
     *
     * @return empty string
     */
    public String addUserLog() {
        TtGen ttGen = TtGenHome.getComponentInstance(false).getInstance();
        if (!StringUtils.isEmpty(ttGen.getLog())) {
            TtLog ttLog = new TtLog(new Date(), Actor.instance().getId(), null, ttGen.getLog(), CHAT_MESSAGE,
                    ttGen);
            persistLog(ttLog);

            reloadLogs(ttLog);
            ttGen.setLog(null);
        }
        return "";
    }

    public String deleteUserLog(TtLog log) {
        EntityManager entityManager = null;
        try {
            entityManager = Utils.createEntityManager();
            entityManager.remove(entityManager.merge(log));
            entityManager.flush();

            reloadLogs(log);
        } finally {
            if (entityManager != null) {
                entityManager.close();
            }
        }
        return "";
    }

    /**
     * Used to refresh collection of logs kept in TT since TT does not manage persistence of logs and logs are obtained from TT in Support Information tab
     *
     * @param ttLog
     */
    private void reloadLogs(TtLog ttLog) {
        TtGenHome ttGenHome = TtGenHome.getComponentInstance(false);

        List<TtLog> ttLogs = ttGenHome.getInstance().getLogs();
        int indexOfLogToRemove = -1;
        for (int i = 0; i < ttLogs.size(); i++) {
            if (ttLogs.get(i).getId().equals(ttLog.getId())) {
                indexOfLogToRemove = i;
                break;
            }
        }
        if (indexOfLogToRemove == -1) {
            if (ttLog.getTtGen().getStatus().equals(TT_STORED)) {
                ttLog = Utils.getEntityManager().merge(ttLog); // put instance to conversation Persistence context for make logs deleted at TT remove
            }
            ttLogs.add(ttLog);
        } else {
            ttLogs.remove(ttLog);
        }
    }

    private void persistLog(TtLog ttLog) {
        EntityManager entityManager = null;
        try {
            entityManager = Utils.createEntityManager();
            entityManager.persist(ttLog);
            entityManager.flush();
        } finally {
            if (entityManager != null) {
                entityManager.close();
            }
        }
    }

    /**
     * Adds new entry to log list when user clicks on '(clear message)' link next to the warning/error message
     *
     * @param fieldId     field id error message concerns to
     * @param facesErrors list of errors/warnings
     */

    public void addClearWarningMessageToLogger(String fieldId, String errorMessage,
            ArrayList<FacesError> facesErrors, TtGen ttGen, Boolean sendMail, Boolean shouldBeLogged) {
        String errorMessageToLog = "";

        for (FacesError err : facesErrors) {
            if (err.getFieldId().equals(fieldId) && err.getDetailedErrorDescription().contains(errorMessage)) {
                for (FacesMessageText msg : err.getFacesMessageTexts()) {
                    errorMessageToLog = errorMessageToLog.concat(msg.getErrorText() + " ");
                }
            }
        }
        if (!errorMessageToLog.equals("")) {
            if (shouldBeLogged) {
                addMessageToLogger(fieldId, errorMessageToLog, facesErrors);
            }
            ValidatorUtils validatorUtils = (ValidatorUtils) Component.getInstance(ValidatorUtils.class);
            validatorUtils.setIgnoreWarning(true);
            ErrorHandling.getInstanceFromAnyScope().removeErrorWithLinks(fieldId, facesErrors, errorMessageToLog);
        }
        if (sendMail) {
            TTMailService.getInstance().sendMailForDuplicationWarning(ttGen);
        }
    }

    private void addMessageToLogger(String fieldId, String errorMessage, ArrayList<FacesError> facesErrors) {
        boolean isTTOpened = FacesContext.getCurrentInstance().getViewRoot().getViewId().contains("TtGenEdit");

        String actorId = Actor.instance().getId();
        String searchableMessage = errorMessage + " (message is cleared)";
        int msgHashCode = searchableMessage.hashCode();
        searchableMessage = searchableMessage.length() > 1000 ? searchableMessage.substring(0, 1000)
                : searchableMessage;

        TtLog ttLog;

        TtGenHome ttGenHome = TtGenHome.getComponentInstance(false);

        if (isTTOpened) {

            TtGen ttGen = ttGenHome.getInstance();
            ttLog = new TtLog(new Date(), actorId, "Add warning message", searchableMessage, DUPLICATION_MESSAGE,
                    ttGen);

            ttLog.setMsgHashCode(msgHashCode);

            if (ttGen.getId() == null) {
                ttGen.getLogs().add(ttLog);
            } else {
                persistLog(ttLog);
                reloadLogs(ttLog);
            }
        } else {
            Long ttId = Long.valueOf(facesErrors.get(0).getFieldId());
            TtGen ttGen = ttGenHome.getEntityManager().find(TtGen.class, ttId);
            ttLog = new TtLog(new Date(), actorId, "Add warning message", searchableMessage, DUPLICATION_MESSAGE,
                    ttGen);
            ttLog.setMsgHashCode(msgHashCode);

            persistLog(ttLog);
        }

    }
}