/*
 * $Id: NewDocumentForm.java,v 1.39 2007/05/04 12:41:10 korowa Exp $
 *
 * Copyright (c) Siemens AG 2008 
 *           (c) Universität Paderborn 2008 
 * Licensed under EUPL 1.1. 
 * 
 * $Log: NewDocumentForm.java,v $
 * Revision 1.39  2007/05/04 12:41:10  korowa
 * rt-Ticket 104
 * Der name der Datei geht bei hinzufügen des Attributs nicht mehr verloren
 *
 * Revision 1.38  2007/03/01 15:02:56  korowa
 * Beschreibungen zur Kategorien und Dokumenten und News werden jetzt in eigener Tabelle NOTES über VKCNoteSerializer gespeichert und sind nicht begrenzt an Zeichenanzahl
 *
 * Revision 1.37  2006/07/31 11:39:36  korowa
 * Falls Dokument gelockt ist, wird beim Versuch eine neue Version hinzufügen Infomeldung ausgegeben(Zustand+User) und die Aktion wird blockiert
 *
 * Revision 1.36  2006/07/13 23:15:15  marbobo
 * + TDF-Format aus der Auswahlbox entfernt
 *
 * Revision 1.35  2005/09/14 00:54:26  marbobo
 * + Url für Webkopien korrigiert
 *
 * Revision 1.34  2005/09/07 23:51:19  marbobo
 * + Bugfix
 *
 * Revision 1.33  2005/09/04 23:20:10  marbobo
 * + Neues Metadaten-Attribute (Erstellungsdatum) für Dokumente hinzugefügt
 *
 * Revision 1.32  2005/08/17 13:53:54  marbobo
 * + autoName in autoTitle umbenannt
 *
 * Revision 1.31  2005/08/16 08:51:26  korowa
 * Ermöglicht Abschalten der Convertergenerierung von Namen, Schlüsselwörter und Beschreibung
 *
 * Revision 1.30  2005/03/21 20:27:48  marbobo
 * + DocSize auf Type Long umgestellt
 *
 * Revision 1.29  2005/02/09 08:59:42  bigmac
 * SessionID wird aus Dateinamen entfernt
 *
 * Revision 1.28  2004/11/10 15:02:48  koppy
 * Projektweite Überarbeitung von Error-Logs, Stacktraces,..
 *
 * Revision 1.27  2004/11/09 14:35:36  marbobo
 * + Umstellung von authorId auf publisherId
 * + Hinzufügen von AuthorName für Dokumente
 * + Nötwendige Änderungen betreffend AuthorName und PublisherId
 *
 * Revision 1.26  2004/10/22 12:40:37  marbobo
 * + Verfallsdatum darf nicht den heutigen Datum haben
 *
 * Revision 1.25  2004/10/17 20:05:27  marbobo
 * + Globale Dokumentattribute überarbeitet
 * + Publikationszeitpunkt frei wählbar
 *
 * Revision 1.24  2004/10/14 14:20:41  marbobo
 * + Publikationsdatum wählbar (noch wird es nicht berücksichtigt)
 *
 * Revision 1.23  2004/07/20 15:20:23  jonyp
 * changed due to move of VKCQuotaInfo from util to view.bean
 *
 *
 */

package vkc.view.form;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import java.util.Vector;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts.action.ActionError;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.upload.FormFile;
import org.apache.struts.util.LabelValueBean;

import vkc.db.entities.VKCAddAttribute;
import vkc.db.entities.VKCCategory;
import vkc.db.entities.VKCDocumentType;
import vkc.db.entities.VKCEntity;
import vkc.db.entities.VKCProxyUser;
import vkc.db.serializer.VKCAddAttributeSerializer;
import vkc.db.serializer.VKCCategorySerializer;
import vkc.db.serializer.VKCDocumentClassSerializer;
import vkc.db.serializer.VKCDocumentStatusSerializer;
import vkc.db.serializer.VKCDocumentTypeSerializer;
import vkc.db.serializer.VKCProxyUserSerializer;
import vkc.db.serializer.VKCUserSerializer;
import vkc.exception.DBException;
import vkc.misc.VKCLogManager;
import vkc.util.DateUtils;
import vkc.util.FileUtils;
import vkc.util.ServletUtils;
import vkc.util.StringUtils;
import vkc.view.bean.VKCQuotaInfo;
import vkc.util.sort.DocumentTypeSortFactory;
import vkc.util.webcopy.WebCopy;
import vkc.util.webcopy.WebCopyResult;
import vkc.view.PageId;
import vkc.view.WebShare;

public class NewDocumentForm extends BasicActionForm {
	private boolean autoTitle			 = true;
	private boolean autoDescription		 = true;
	private boolean autoKeyword			 = true;
	private boolean classifyFromCategory = false;

    private Collection classifyResults	= new ArrayList();
	private Collection proxyUsers 		= null;

    private FormFile newDocumentFormFile = null;

    private HashMap addDocAttributes	= new HashMap();
	private HashMap	allAddDocAttributes	= null;

	private Integer categoryId 			= VKCEntity.ID_UNDEFINED;
	private Integer	index 				= new Integer(0);
	private Integer newDocumentClassId 	= new Integer(0);
	private Integer newDocumentPreDocId = new Integer(0);
	private Integer newDocumentStatus 	= new Integer(0);
	private Integer proxyUserId 		= new Integer(0);
	private Integer selectedId 			= VKCEntity.ID_UNDEFINED;
	private Integer sourcePageId 		= PageId.CATEGORY_VIEW;
	private Integer subTargetId 		= null;
	private Integer targetId 			= null;

	private List documentStati 		= new ArrayList();
	private List documentTypes 		= null;
    private List dynaAttributes 	= new ArrayList();

	private Long newDocumentSize 	= new Long(0);
    
	private String conversionResponse 		= null;
	private String newDocumentAuthorName 	= "";
	private String newDocumentArchiveId 	= "";
	private String newDocumentCreationDate 	= "";
	private String newDocumentDescription 	= "";
	private String newDocumentExpDate 		= "";
	private String newDocumentExtension 	= "";
	private String newDocumentKeywords 		= "";
	private String newDocumentName 			= null;
	private String newDocumentPubDate 		= "";
	private String newDocumentTitle 		= "";
	private String newDocumentUploadType 	= "file";
	private String newDocumentVersionDesc 	= "";
    private String newDocumentVersionHigh 	= "";
    private String newDocumentVersionLow 	= "";
    private String newDocumentWebcopyName   = null;

	private VKCCategory category = null;

	private VKCQuotaInfo quotaInfo = null;

	public void reset(ActionMapping mapping, HttpServletRequest request) {
		super.reset(mapping, request);

		autoTitle			 = false;
		autoDescription		 = false;
		autoKeyword			 = false;
		classifyFromCategory = false;

	    classifyResults	= new ArrayList();
		proxyUsers 		= null;

	    newDocumentFormFile = null;

	    addDocAttributes	= new HashMap();
		allAddDocAttributes	= null;

		categoryId 			= VKCEntity.ID_UNDEFINED;
		index 				= new Integer(0);
		newDocumentClassId 	= new Integer(0);
		newDocumentPreDocId = new Integer(0);
		newDocumentSize 	= new Long(0);
		newDocumentStatus 	= new Integer(0);
		proxyUserId 		= new Integer(0);
		selectedId 			= VKCEntity.ID_UNDEFINED;
		sourcePageId 		= PageId.CATEGORY_VIEW;
		subTargetId 		= null;
		targetId 			= null;

		documentStati 	= new ArrayList();
		documentTypes 	= null;
	    dynaAttributes 	= new ArrayList();

		conversionResponse 		= null;
		newDocumentAuthorName 	= getUser().getFirstname() + " " + getUser().getLastname();
		newDocumentArchiveId 	= "";
		newDocumentCreationDate = "";
		newDocumentDescription 	= "";
		newDocumentExpDate 		= "";
		newDocumentExtension 	= "";
		newDocumentKeywords 	= "";
		newDocumentName 		= null;
		newDocumentPubDate 		= "";
		newDocumentTitle 		= "";
		newDocumentUploadType 	= "file";
		newDocumentVersionDesc 	= "";
	    newDocumentVersionHigh 	= "";
	    newDocumentVersionLow 	= "";
	    newDocumentWebcopyName  = null;
	    	    
	    category = null;

		quotaInfo = null;
		
	}

	public ActionErrors validate(ActionMapping mapping, HttpServletRequest request) {
		ActionErrors errors = new ActionErrors();

		try {
			synchronizeDynaAttributes(request);
			setAutoTitle(request.getParameter("autoTitle") != null);
			setAutoDescription(request.getParameter("autoDescription") != null);
			setAutoKeyword(request.getParameter("autoKeyword") != null);

			if (isFileUpload() && isButtonAction(request) && isEmpty(getNewDocumentArchiveId())) { // Datei war noch nicht übertragen
				if (getNewDocumentFormFile() == null || isEmpty(getNewDocumentFormFile().getFileName())) {
					setNewDocumentName("");
					setNewDocumentArchiveId("");
					setNewDocumentSize(new Long(0));
					errors.add("file", new ActionError("error.document.no.file"));

				} else if (getNewDocumentFormFile().getFileSize() == 0) { 
					setNewDocumentName("");
					setNewDocumentArchiveId("");
					setNewDocumentSize(new Long(0));
					errors.add("file", new ActionError("error.document.file.empty"));
				} else {
					String name = ServletUtils.removeSessionID(newDocumentFormFile.getFileName());
					setNewDocumentName(name);
					
					setNewDocumentArchiveId(WebShare.archive.store(newDocumentFormFile.getInputStream()));
					setNewDocumentSize(new Long(newDocumentFormFile.getFileSize()));
				}
			}

			if (isWebCopyUpload() && isButtonAction(request) && isEmpty(getNewDocumentArchiveId())) {
				if (getNewDocumentName().length() == 0) {
					errors.add("file", new ActionError("error.url.empty"));
				} else {
					// Webseite muss noch geholt werden
					WebCopyResult webCopyResult = null;
					
					try {
						webCopyResult = WebCopy.tmpStoreAsZipFile(getNewDocumentName());
						
						if (webCopyResult != null && webCopyResult.getFileSize() > 0) {					
							// Save the file on the server
							setNewDocumentArchiveId(WebShare.archive.store(webCopyResult.getInputStream()));
							setNewDocumentSize(new Long(webCopyResult.getFileSize()));
							URL url = new URL(getNewDocumentName());
							String rootUrl = url.getProtocol() + "://" + url.getHost() + (url.getPort() != -1 ? ":" + url.getPort() : "");
							String path = webCopyResult.getRootFilename();
							path = path.startsWith("/") ? path : "/" + path;
							setNewDocumentWebcopyName(rootUrl + path);
						} else {
							errors.add("file", new ActionError("error.getting.webcopy"));
						}
					} catch (MalformedURLException e) {
						errors.add("file", new ActionError("error.malformed.url"));
					} finally {
						if (webCopyResult != null) {
							FileUtils.deleteFile(webCopyResult.getZippedFile());
						}
					}
				}
			}

			if (request.getParameter( "action.publish" ) != null) {
				 // überprüfen, ob jemand zwischendurch den Dokument gesperrt hat
				 String lockUser = vkc.webdav.WebDavServlet.getLockUserName(getNewDocumentPreDocId());			     
			        if (lockUser != null) {
			           errors.add("",new ActionError( "vkc.string.lockUserInfo", lockUser));
			        }
				
				
				// maximal erlaubte Länge der Beschreibung checken
			    /*
			     * Speichermechanismus für Beschreibung ist geändert worden =>
				 * keine länge Begrenzung für description
				if (!isLengthValid(getNewDocumentDescription(), "document.description")) {
					errors.add("description", new ActionError("error.document.description.size",
						InputFieldConstraints.getMaxLength("document.description")));
				}
				*/
				if (!isEmpty(getNewDocumentCreationDate())) {
					if (!isParsableDate(getNewDocumentCreationDate())) {
						errors.add("expirationDate", new ActionError("error.creation.date"));
					}
				}

				if (!isEmpty(getNewDocumentExpDate())) {
					if (!isParsableDate(getNewDocumentExpDate())) {
						errors.add("expirationDate", new ActionError("error.expiration.date"));
					} else {
						Date expDate = DateUtils.parseDate(getNewDocumentExpDate());
						Date refDate = new Date(System.currentTimeMillis());

						if (refDate.after(expDate)) {
							errors.add("expirationDate", new ActionError("error.expiration.date.elapsed"));
						}
					}
				}

				if (!isEmpty(getNewDocumentPubDate())) {
					if (!isParsableDate(getNewDocumentPubDate())) {
						errors.add("publishingDate", new ActionError("error.publishing.date"));
					} else {
						Date pubDate = DateUtils.getMaximizedDayTime(DateUtils.parseDate(getNewDocumentPubDate()));
						Date refDate = DateUtils.getMaximizedDayTime(new Date());

						if (refDate.after(pubDate)) {
							errors.add("publishingDate", new ActionError("error.publishing.date.before.today"));
						}
					}
				}

				if (isEmpty(getNewDocumentAuthorName())) {
				    setNewDocumentAuthorName(getUser().getFirstname() + " " + getUser().getLastname());
				}
				
				if (isEmpty(getNewDocumentTitle())) {
					errors.add("title", new ActionError("error.document.title"));
				}

                if (!isEmpty(getNewDocumentVersionHigh())) {
                    if (!isParsableInteger(getNewDocumentVersionHigh())) {
                        errors.add("newDocumentVersionHigh", new ActionError("error.document.version.high"));
                    }
                }

                if (!isEmpty(getNewDocumentVersionLow())) {
                    if (!isParsableInteger(getNewDocumentVersionLow())) {
                        errors.add("newDocumentVersionLow", new ActionError("error.document.version.low"));
                    }
                }

                // check, if any global attribute must not be empty
                Iterator it = getAllAddDocAttributes().keySet().iterator();
                
                while (it.hasNext()) {
                    String key = (String)it.next();
                    VKCAddAttribute attr = (VKCAddAttribute)getAllAddDocAttributes().get(key);
                    
                    if (attr.getAllowNull().intValue() == 0 && isEmpty((String)getAddDocAttributeValue(key))) {
                        errors.add(key, new ActionError("error.document.globalattribute.violation", attr.getName()));
                    }
                }

                // check, if any dyna attribute name must not be empty
                List dynaAttrList = getDynaAttributes();
                
                for (int i = 0; i < dynaAttrList.size(); i++) {
                    LabelValueBean dynaAttr = (LabelValueBean)dynaAttrList.get(i);
                    
                    if (isEmpty(dynaAttr.getLabel())) {
                        errors.add("dynaAttributeLabel_" + i, new ActionError("error.document.dynaattribute.name", String.valueOf(i + 1)));
                    }
                }
			}

		} catch (Exception e) {
			VKCLogManager.errorLog(this, "", e.getMessage(), e);
			errors.add("error", new ActionError("error.general", e.getMessage()));
		}
		
		return errors;
	}

	private boolean isButtonAction(HttpServletRequest request) {
		return 
			request.getParameter("action.publish") != null || 
			request.getParameter("action.classify") != null ||
			request.getParameter("action.add.dynaattribute") != null ||
			request.getParameter("action.analyse") != null;
	}
	
	public HashMap getAddDocAttributes() {
		return addDocAttributes;
	}

	public void setAddDocAttributes(HashMap m) {
		addDocAttributes = m;
	}

	public Object getAddDocAttributeValue(String key) {
		return getAddDocAttributes().get(key);
	}

	public void setAddDocAttributeValue(String key, Object value) {		
	    getAddDocAttributes().put(key, value);
	}

	public Set getAddDocAttributeKeys() {
		return getAddDocAttributes().keySet();
	}
	
	public boolean getAddDocAttributeAllowNull(String key) throws DBException {
	    if (!isEmpty(key)) {
	        if (getAllAddDocAttributes().containsKey(key)) {
	            Integer allowNull = ((VKCAddAttribute)getAllAddDocAttributes().get(key)).getAllowNull();
	            
	            if (allowNull != null && allowNull.intValue() == 0) {
	                return false;
	            }
	        }
	    }
	    
	    return true;
	}

	public String getAddDocAttributeName(String key) throws DBException {
	    if (!isEmpty(key)) {
	        if (getAllAddDocAttributes().containsKey(key)) {
	            return ((VKCAddAttribute)getAllAddDocAttributes().get(key)).getName();
	        }
	    }
	    
	    return key;
	}

	public HashMap getAllAddDocAttributes() throws DBException {
		if (allAddDocAttributes == null) {
			allAddDocAttributes = new HashMap();
		    Vector addDocAttr = VKCAddAttributeSerializer.getAllAddDocAttributes();
	        
			for (int i = 0; i < addDocAttr.size(); i++) {
				VKCAddAttribute addAttribute = (VKCAddAttribute)addDocAttr.get(i);
				allAddDocAttributes.put("adddocattrId_" + addAttribute.getAttrId(), addAttribute);
			}
		    
		}
		
	    return allAddDocAttributes;
	}
    
	public Collection getProxyUsers() throws DBException {
		if (proxyUsers == null && getUser() != null) {
            proxyUsers = new ArrayList();
			Collection proxies = VKCProxyUserSerializer.getProxyUsersForUser(getUser().getUserId(), true);
            
			if (proxies == null) {
                return proxyUsers;
            }
			
			for (Iterator iter = proxies.iterator() ; iter.hasNext() ;) {
				try {
					VKCProxyUser proxy = (VKCProxyUser)iter.next();
					proxyUsers.add(VKCUserSerializer.retrieveUserInfo(proxy.getProxyUserId()));
				} catch (DBException e) {
					VKCLogManager.errorLog(this, "", e.getMessage(),e);
				}
			}
		}
		
		return proxyUsers;
	}

	public VKCCategory getCategory() throws DBException {
		if (category == null) {
			category = VKCCategorySerializer.retrieveCategory(categoryId);
		}

		return category;
	}

	public VKCQuotaInfo getQuotaInfo() throws DBException {
		if (quotaInfo == null) {
			if (getUser() != null) {
				quotaInfo = WebShare.database.getQuotaInfo(getUser().getUserId());
			}
		}
		
		return quotaInfo;
	}

    private void synchronizeDynaAttributes(HttpServletRequest request) {
        getDynaAttributes().clear();
        int i=0;

        while (request.getParameter("dynaAttributeLabel_" + i) != null)  {
            String name  = StringUtils.normalize(request.getParameter("dynaAttributeLabel_" + i));
            String value = StringUtils.normalize(request.getParameter("dynaAttributeValue_" + i));
            getDynaAttributes().add(new LabelValueBean(name, value));
            i++;
        }
    }

	public List getDocumentTypes() {
		if (documentTypes == null) {
            documentTypes = VKCDocumentTypeSerializer.getDocumentTypes();
            
            for (Iterator iter = documentTypes.iterator() ; iter.hasNext() ;) {
                VKCDocumentType type = (VKCDocumentType)iter.next();
                
                if ("LINK".equals(type.getExtension())) {
                    iter.remove();
                } else if ("NOTE".equals(type.getExtension())) {
                    iter.remove();
                } else if ("TDF".equals(type.getExtension())) {
                    iter.remove();
                }
            }

            DocumentTypeSortFactory.sort(documentTypes, DocumentTypeSortFactory.SORT_BY_EXTENSION_UP);
		}

		return documentTypes;
	}

	public List getDocumentStati() {
		if (documentStati.size() == 0) {
			documentStati = VKCDocumentStatusSerializer.getDocumentStati();
		}

		return documentStati;
	}

	public List getDocumentClasses() {
		return VKCDocumentClassSerializer.getDocumentClasses();
	}

    public List getDynaAttributes() {
        return dynaAttributes;
    }

    public void setDynaAttributes(List list) {
        dynaAttributes = list;
    }

	public FormFile getNewDocumentFormFile() {
		return newDocumentFormFile;
	}

	public String getNewDocumentName() {
		return newDocumentName;
	}

	public String getNewDocumentUploadType() {
		return newDocumentUploadType;
	}

	public void setNewDocumentFormFile(FormFile file) throws Exception {
		newDocumentFormFile = file;		
	}

	public void setNewDocumentName(String s) {
		newDocumentName = StringUtils.normalize(s);
	}

	public void setNewDocumentUploadType(String s) {
		newDocumentUploadType = StringUtils.normalize(s);
	}

	public Integer getNewDocumentClassId() {
		return newDocumentClassId;
	}

	public void setNewDocumentClassId(Integer i) {
		newDocumentClassId = i;
	}

	public String getNewDocumentAuthorName() {
		return newDocumentAuthorName;
	}

	public void setNewDocumentAuthorName(String s) {
		newDocumentAuthorName = StringUtils.normalize(s);
	}

	public String getNewDocumentArchiveId() {
		return newDocumentArchiveId;
	}

	public void setNewDocumentArchiveId(String s) {
		newDocumentArchiveId = StringUtils.normalize(s);
	}

	public String getNewDocumentTitle() {
		return newDocumentTitle;
	}

	public void setNewDocumentTitle(String s) {
		newDocumentTitle = StringUtils.normalize(s);
	}

	public String getNewDocumentDescription() {
		return newDocumentDescription;
	}

	public void setNewDocumentDescription(String s) {
		newDocumentDescription = StringUtils.normalize(s);
	}

	public String getNewDocumentKeywords() {
		return newDocumentKeywords;
	}

	public void setNewDocumentKeywords(String s) {
		newDocumentKeywords = StringUtils.normalize(s);
	}

	public String getNewDocumentExtension() {
		return newDocumentExtension;
	}

	public void setNewDocumentExtension(String s) {
		newDocumentExtension = StringUtils.normalize(s);
	}

	public Long getNewDocumentSize() {
		return newDocumentSize;
	}

	public void setNewDocumentSize(Long i) {
		newDocumentSize = i;
	}

	public Integer getSourcePageId() {
		return sourcePageId;
	}

	public void setSourcePageId(Integer i) {
		sourcePageId = i;
	}

	public Integer getNewDocumentStatus() {
		return newDocumentStatus;
	}

	public void setNewDocumentStatus(Integer i) {
		newDocumentStatus = i;
	}

	public String getNewDocumentCreationDate() {
		return newDocumentCreationDate;
	}

	public void setNewDocumentCreationDate(String s) {
		newDocumentCreationDate = StringUtils.normalize(s);
	}

	public String getNewDocumentExpDate() {
		return newDocumentExpDate;
	}

	public void setNewDocumentExpDate(String s) {
		newDocumentExpDate = StringUtils.normalize(s);
	}

	public String getNewDocumentPubDate() {
		return newDocumentPubDate;
	}

	public void setNewDocumentPubDate(String s) {
		newDocumentPubDate = StringUtils.normalize(s);
	}

	public Integer getProxyUserId() {
		return proxyUserId;
	}

	public void setProxyUserId(Integer i) {
		proxyUserId = i;
	}

	public boolean getClassifyFromCategory() {
		return classifyFromCategory;
	}

	public void setClassifyFromCategory(boolean b) {
		classifyFromCategory = b;
	}

	public void setCategoryId(Integer i) {
		categoryId = i;
		category = null;
	}

	public Integer getCategoryId() {
		return categoryId;
	}	

	public Integer getNewDocumentPreDocId() {
		return newDocumentPreDocId;
	}

	public void setNewDocumentPreDocId(Integer i) {
		newDocumentPreDocId = i;
	}

	public String getNewDocumentVersionDesc() {
		return newDocumentVersionDesc;
	}

	public void setNewDocumentVersionDesc(String s) {
		newDocumentVersionDesc = StringUtils.normalize(s);
	}
	
	public boolean isWebCopyUpload() {
		return getNewDocumentUploadType().equals("webcopy");
	}

	public boolean isLinkUpload() {
		return getNewDocumentUploadType().equals("link");
	}

	public boolean isNoteUpload() {
		return getNewDocumentUploadType().equals("note");
	}

	public boolean isFileUpload() {
		return getNewDocumentUploadType().equals("file");
	}

    public String getNewDocumentVersionHigh() {
        return newDocumentVersionHigh;
    }

    public void setNewDocumentVersionHigh(String s) {
        newDocumentVersionHigh = StringUtils.normalize(s);
    }

    public String getNewDocumentVersionLow() {
        return newDocumentVersionLow;
    }

    public void setNewDocumentVersionLow(String s) {
        newDocumentVersionLow = StringUtils.normalize(s);
    }
    
    public boolean getRequestSourceIsProjectView() {
        return sourcePageId.intValue() == PageId.PROJECT_VIEW.intValue();
    }

	public Integer getSelectedId() {
		return selectedId;
	}

	public void setSelectedId(Integer i) {
		selectedId = i;
	}

    public Collection getClassifyResults() {
        return classifyResults;
    }

    public void setClassifyResults(Collection collection) {
        classifyResults = collection == null ? new ArrayList() : collection;
    }
    
    public Date getCreationDate() {
        if (!isEmpty(getNewDocumentCreationDate()) && isParsableDate(getNewDocumentCreationDate())) {
            return DateUtils.parseDate(getNewDocumentCreationDate());
        } 

        return null;
    }

    public Date getExpirationDate() {
        if (!isEmpty(getNewDocumentExpDate()) && isParsableDate(getNewDocumentExpDate())) {
            return DateUtils.getMaximizedDayTime(DateUtils.parseDate(getNewDocumentExpDate()));
        } 

        return null;
    }

    public Date getPublishingDate() {
        if (!isEmpty(getNewDocumentPubDate()) && isParsableDate(getNewDocumentPubDate())) {
            Date now = DateUtils.getMinimizedDayTime(new Date(System.currentTimeMillis()));
            Date p = DateUtils.getMinimizedDayTime(DateUtils.parseDate(getNewDocumentPubDate()));
            
            if (!now.equals(p) && now.before(p)) {
                return DateUtils.getMinimizedDayTime(DateUtils.parseDate(getNewDocumentPubDate()));
            }
        } 

        return new Date(System.currentTimeMillis());
    }

	public boolean getPublishNow() {
        if (!isEmpty(getNewDocumentPubDate()) && isParsableDate(getNewDocumentPubDate())) {
            Date now = DateUtils.getMinimizedDayTime(new Date(System.currentTimeMillis()));
            Date p = DateUtils.getMinimizedDayTime(DateUtils.parseDate(getNewDocumentPubDate()));
            
            if (!now.equals(p) && now.before(p)) {
                return false;
            }
        } 

        return true;
	}
	
	public Integer getIndex() {
		return index;
	}

	public void setIndex(Integer i) {
		index = i;
	}

	public String getConversionResponse() {
		return conversionResponse;
	}

	public void setConversionResponse(String string) {
		conversionResponse = string;
	}

	public Integer getSubTargetId() {
		return subTargetId;
	}

	public Integer getTargetId() {
		return targetId;
	}

	public void setSubTargetId(Integer i) {
		subTargetId = i;
	}

	public void setTargetId(Integer i) {
		targetId = i;
	}

	public boolean isAutoDescription() {
        return autoDescription;
    }
    
	public void setAutoDescription(boolean autoDescription) {
        this.autoDescription = autoDescription;
    }
    
	public boolean isAutoKeyword() {
        return autoKeyword;
    }
    
	public void setAutoKeyword(boolean autoKeyword) {
        this.autoKeyword = autoKeyword;
    }
    
	public boolean isAutoTitle() {
        return autoTitle;
    }
    
	public void setAutoTitle(boolean autoTitle) {
        this.autoTitle = autoTitle;
    }
    
	public boolean isAdminAutoTitle()  {
	    if (Integer.parseInt(WebShare.getSysParameterValue("DOCUMENT", "EnableDocumentTitle", "1" ))  != 0) {
	        return true;
	    }
	    
	    return false;
	}
    
	public boolean isAdminAutoDescription()  {
	    if (Integer.parseInt(WebShare.getSysParameterValue("DOCUMENT", "EnableDocumentDescription", "1")) != 0)	{
	        return true;
	    }
	    
	    return false;
	}
	
    public boolean isAdminAutoKeyword()  {
	    if (Integer.parseInt(WebShare.getSysParameterValue("DOCUMENT", "EnableDocumentKeyword", "1")) != 0) {
	        return true;
	    }

	    return false;
	}

	public String getNewDocumentWebcopyName() {
		return newDocumentWebcopyName;
	}

	public void setNewDocumentWebcopyName(String newDocumentWebcopyName) {
		this.newDocumentWebcopyName = StringUtils.normalize(newDocumentWebcopyName);
	}
}