/*
 * $Id: VKCSubCategory.java,v 1.26 2005/08/09 19:03:21 jonyp Exp $
 *
 * Copyright (c) Siemens AG 2008 
 *           (c) Universität Paderborn 2008 
 * Licensed under EUPL 1.1. 
 *
 * $Log: VKCSubCategory.java,v $
 * Revision 1.26  2005/08/09 19:03:21  jonyp
 * added methods to display lastChnage/lastAccessor as User.Info.Lastname format
 *
 * Revision 1.25  2005/06/12 20:34:24  jonyp
 * added methods for creatorId and creator
 *
 * Revision 1.24  2005/01/28 09:38:15  jonyp
 * changed order of images in getFolderImage
 *
 * Revision 1.23  2004/10/03 05:18:47  jonyp
 * + added comment
 *
 * Revision 1.22  2004/09/09 08:15:13  jonyp
 * changed method name "isInTreeOf" to "isInTree"
 *
 * Revision 1.21  2004/09/08 19:44:59  marbobo
 * + isInTreeOf() - Zur Bestimmung ob die angegebene Kategorie im Baumstruktur
 * der aktuellen Kategorie sich befindet
 *
 * Revision 1.20  2004/08/04 23:18:26  marbobo
 * + Status auf Flag umgestellt
 *
 * Revision 1.19  2004/07/30 23:41:19  jonyp
 * added flag var and consider it inside getFolderImage/Shaded
 *
 * Revision 1.18  2004/07/01 21:26:41  marbobo
 * + Status
 *
 * Revision 1.17  2004/06/27 17:16:55  marbobo
 * + Quellcode maintenance
 *
 * Revision 1.16  2004/06/04 09:16:58  jonyp
 * changed logic of isEmpty-computating: now done in VKCCategorySerializer
 *
 * Revision 1.15  2004/06/02 09:17:06  jonyp
 * - added "isEmpty" method 
 * - added "hasPermission" method
 *
 * Revision 1.14  2004/05/04 12:28:11  jonyp
 * getFolderImage: added image for null-category
 *
 * Revision 1.13  2004/04/16 08:18:44  jonyp
 * added functions to find out weather this category is 
 * projectroot / templateroot / functionalviewroot
 *
 * Revision 1.12  2004/04/15 14:37:49  jonyp
 * added functions to find out weather this category is 
 * projectroot / templateroot / functionalviewroot
 *
 * Revision 1.11  2004/04/15 09:24:53  jonyp
 * added getter for searchquery
 *
 * Revision 1.10  2004/02/20 16:38:56  bigmac
 * Überflüssigen import entfernt
 *
 * Revision 1.9  2004/02/17 09:34:41  jonyp
 * timestamp -> date
 *
 * Revision 1.8  2004/02/11 14:58:11  bigmac
 * *** empty log message ***
 *
 * Revision 1.7  2004/02/09 20:45:08  bigmac
 * +isProjectFolder()
 *
 * Revision 1.6  2003/12/24 10:12:12  bigmac
 * Projektweite Import-Optimierung
 *
 * Revision 1.5  2003/11/03 09:44:00  lawern
 * VKCSubCategory um Zugriffsdaten für WebDAV erweitert.
 *
 * Revision 1.4  2003/09/15 13:17:13  jonyp
 * accessible category flag
 *
 * Revision 1.3  2003/07/31 01:20:11  koppy
 * + cutOut: boolean
 *
 * Revision 1.2  2002/11/04 02:04:32  koppy
 * long --> Integer
 *
 * Revision 1.1  2002/10/21 14:35:39  bigmac
 * *** empty log message ***
 *
 * Revision 1.1  2002/09/16 12:13:54  koppy
 * Initial version
 *
 */
package vkc.db.entities;

import java.io.Serializable;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

import vkc.db.serializer.VKCCategorySerializer;
import vkc.db.serializer.VKCSysParameterSerializer;
import vkc.db.serializer.VKCUserSerializer;
import vkc.db.serializer.VKCLanguageSerializer;
import vkc.exception.DBException;
import vkc.security.PermissionManager;
import vkc.security.VKCAcl;
import vkc.util.CategoryUtils;
import vkc.db.serializer.VKCAclSerializer;
import vkc.view.bean.UserInfo;
import vkc.view.WebShare;
import org.apache.struts.util.MessageResources;


/**
 * Wrapper-Klasse für eine Unterkategorie im VKC-System dar
 *
 * @author koppy
 */
public class VKCSubCategory implements Serializable {
    private Boolean accessible = new Boolean( true );

    private Integer creatorId = new Integer(-1);
    private Date created = new Date( System.currentTimeMillis() );
    private Integer lastChangerId = new Integer(-1);
    private Date lastChanged = new Date( created.getTime() );
    private Integer lastAccessorId = new Integer(-1);
    private Date lastAccessed = new Date( created.getTime() );

    private Integer subCategoryId = new Integer(-1);
    private Integer status = VKCCategory.STATUS_ACTIVE;
    private Integer flag = VKCCategory.FLAG_ACTIVE;    
    
    private String name = "";
    
    private boolean cutOut = false;
    private boolean empty = true;

    /**
     * Konstruktor
     */
    public VKCSubCategory() {
    }

    /**
     * Konstruktor
     *
     * @param subCategoryId ID der Unterkategorie
     * @param name Name der Unterkategorie
     */
    public VKCSubCategory( Integer subCategoryId, String name ) {
        this.subCategoryId = subCategoryId;
        this.name = name;
    }

    public void setSubCategoryId( Integer i ) {
        subCategoryId = i;
    }

    public Integer getSubCategoryId() {
        return subCategoryId;
    }

    public void setName( String s ) {
        name = s.trim();
    }

    public String getName() {
        return name;
    }

    public String toString() {
        return getName() + " (ID = " + getSubCategoryId() + ")";
    }

    public boolean getCutOut() {
        return cutOut;
    }

    public void setCutOut( boolean b ) {
        cutOut = b;
    }

    public Boolean getAccessible() {
        return accessible;
    }

    public void setAccessible( Boolean b ) {
        accessible = b;
    }

    public Date getCreated() {
        return created;
    }

    public Date getLastAccessed() {
        return lastAccessed;
    }

    public Date getLastChanged() {
        return lastChanged;
    }

    public void setCreated( Date d ) {
        created = d;
    }

    public void setLastAccessed( Date d ) {
        lastAccessed = d;
    }

    public void setLastChanged( Date d ) {
        lastChanged = d;
    }
    
	public VKCSearchQuery getSearchQuery() throws DBException {
		VKCSearchQuery sq = VKCCategorySerializer.retrieveSearchQuery( getSubCategoryId() ); 
		
		if ( sq == null || sq.isEmpty() ) {
		    return null;
		}
		
		return sq;
	}
	
	public boolean isProjectsRootCategory() {
		if ( VKCSysParameterSerializer.getProjectsRootCategoryId() == getSubCategoryId().intValue() ) {
			return true;
		}
		
		return false;	
	}
	
	public boolean isFunctionalViewsRootCategory() {
		if ( VKCSysParameterSerializer.getFunctionalViewsRootCategoryId() == getSubCategoryId().intValue() ) {
			return true;
		}
        
		return false; 		
	}
	
	public boolean isTemplatesRootCategory() {
		if ( VKCSysParameterSerializer.getTemplatesRootCategoryId() == getSubCategoryId().intValue() ) {
			return true;	
		}
        
		return false;	
	}
	
	public String getFolderImage() {
		if ( getSubCategoryId() == null || getSubCategoryId().intValue() == 0 ) {
			return ( "folder-notexistent.gif" );
		}
		
		if ( getFlag().equals( VKCCategory.FLAG_DELETED ) ) {
			return ( "folder-deleted.gif" ); 
		}
		
		if ( isProjectsRootCategory() ) {
			return ( "folder-projects-root.gif" );
		}
		
		if ( isFunctionalViewsRootCategory() ) {
			return ( "folder-views-root.gif" );
		}
		
		if ( isTemplatesRootCategory() ) {
			return ( "folder-templates-root.gif" );
		}

		try {
			if ( getSearchQuery() != null ) {
				return ( "folder-with-virtual-docs.gif" );
			}
		} catch ( DBException e ) {
		}
		
		if ( isEmpty() ) {
			return ( "folder-empty.gif" );
		}


        //TODO: add image, if subcat is not accessible
		return ( "folder.gif" );
	}

	public String getFolderImageShaded() {
		if ( getSubCategoryId() == null || getSubCategoryId().intValue() == 0 ) {
			return ( "folder-notexistent.gif" );
		}
		
		if ( getFlag().equals( VKCCategory.FLAG_DELETED ) ) {
			return ( "folder-deleted.gif" ); 
		}
		
		if ( isProjectsRootCategory() ) {
			return ( "folder-projects-root-shaded.gif" );
		}
		
		if ( isFunctionalViewsRootCategory() ) {
			return ( "/folder-views-root-shaded.gif" );
		}
		
		if ( isTemplatesRootCategory() ) {
			return ( "folder-templates-root-shaded.gif" );
		}

		if ( isEmpty() ) {
			return ( "folder-empty-shaded.gif" );
		}

		try {
			if ( getSearchQuery() != null ) {
				return ( "folder-with-virtual-docs-shaded.gif" );
			}
		} catch ( DBException e ) {
		}

		return ( "folder-shaded.gif" );
	}

    public void setEmpty( boolean b ) {
    	empty = b;	
    }
	
	public boolean isEmpty() {
        return empty;
	}
	
	public boolean hasPermission( VKCUser user, char permToken ) {
		try {
			if ( getSubCategoryId() != null ) {
				if ( !user.hasAdminPrivileges() ) {
					VKCAcl acl = VKCAclSerializer.getAclByCategoryId( getSubCategoryId() );
				
					return PermissionManager.hasPermission( acl, user, permToken );
				} 

				return true;
			}	
		} catch ( Exception e ) {
		}

		return false;
	}

    public Integer getStatus() {
        return status;
    }

    public void setStatus( Integer i ) {
        status = i;
    }

	public Integer getFlag() {
		return flag;
	}

	public void setFlag( Integer i ) {
		flag = i;
	}

	/**
	 * Diese Methode bestimmt ob die Kategorie mit der ID "catId" sich im 
	 * hierarchischen Baumstruktur dieser Kategorie befindet
	 */
	public boolean isInTree( Integer catId ) {
	    try {
            return CategoryUtils.isInTree( getSubCategoryId(), catId );
        } catch ( DBException e ) {
            return false;
        }
	}

	public Integer getCreatorId() {
		return creatorId;
	}
	
	public void setCreatorId(Integer creatorId) {
		this.creatorId = creatorId;
	}
	
	public String getCreator() throws DBException {
		if (creatorId == null || creatorId.intValue() <= 0)
			return "";
		
		return VKCUserSerializer.retrieveUserInfo(creatorId).getLastnameLabel();
	}

	public String getToolTipLabel(VKCUser user) throws DBException {
		
		if (creatorId == null || creatorId.intValue() <= 0 || created == null || user == null)
			return "";

		String toolTipLabel = "";
		Locale locale = VKCLanguageSerializer.getDefaultLanguage().getLocale();
		MessageResources res = WebShare.resources;	    
		SimpleDateFormat sdf = new SimpleDateFormat( res.getMessage(locale, "date.medium.format"));		
		UserInfo ui = VKCUserSerializer.retrieveUserInfo(creatorId);
		
		if (ui != null ) {
		    toolTipLabel = res.getMessage(locale, "vkc.administration.creator") + ": ";
		    toolTipLabel += ui.getLastnameLabel() + "\n";
		}
	    
		if (created != null) {
		    toolTipLabel += res.getMessage(locale, "vkc.administration.created") + ": ";
		    toolTipLabel += sdf.format(created) + "\n";
	    }

		ui = VKCUserSerializer.retrieveUserInfo(lastChangerId);

		if (ui != null ) {
		    toolTipLabel += res.getMessage(locale, "vkc.lastchanger") + ": ";
		    toolTipLabel += ui.getLastnameLabel() + "\n";
		}

		if (lastChanged != null) {
		    toolTipLabel += res.getMessage(locale, "vkc.lastchanger") + ": ";
		    toolTipLabel += sdf.format(lastChanged) + "\n";
	    }

		ui = VKCUserSerializer.retrieveUserInfo(lastAccessorId);

		if (ui != null ) {
		    toolTipLabel += res.getMessage(locale, "vkc.lastaccessor") + ": ";
		    toolTipLabel += ui.getLastnameLabel() + "\n";
		}

		if (lastAccessed != null) {
		    toolTipLabel += res.getMessage(locale, "vkc.lastaccessed") + ": ";
		    toolTipLabel += sdf.format(lastAccessed);
	    }
		
		return toolTipLabel;
	}
	
	public Integer getLastAccessorId() {
		return lastAccessorId;
	}

	public void setLastAccessorId(Integer lastAccessorId) {
		this.lastAccessorId = lastAccessorId;
	}

	public Integer getLastChangerId() {
		return lastChangerId;
	}

	public void setLastChangerId(Integer lastChangerId) {
		this.lastChangerId = lastChangerId;
	}
}