package page.tools.entity;

import java.util.Collection;

import org.wikiwebserver.core.Privilege;
import org.wikiwebserver.core.SecurityMan;

/**
 * A NodeData object stores the information required to maintain data
 * about a node.
 * 
 * @author Michael Gardiner
 *
 */
public class NodeData extends ProtectedStorable {
    
    private static final String TYPE = "NodeData";
    private static final Privilege MODIFY_BY_CODE_PRIVILEGE = Privilege.MODERATOR;    
    private static final Privilege CLEAR_BY_CODE_PRIVILEGE = Privilege.MODERATOR;    
    
    public String getType() {
        return TYPE;
    }
    
    NodeData() {
        super();
    }
    
    public NodeData(String id) {
        super(id);
    }   
    
    public String getName() {
    	return (String) get("name");
    }
    
    public void setName(String name) {
        removeFromIndex(TYPE, "Names", getName());
        putInIndex(TYPE, "Names", name, getId());
        put("name", name);
    }     
    
    public boolean requiresPassword() {
        Object reqPW = get("password");
        if (reqPW == null) return false;
        return ((Boolean) reqPW).booleanValue();
    }
    
    public void setRequiresPassword(boolean requiresPassword) {
        put("password", new Boolean(requiresPassword));       
    }
    
    public boolean isBlocked() {
        Object blocked = get("blocked");
        if (blocked == null) return false;
        return ((Boolean) blocked).booleanValue();
    }
    
    public void setBlocked(boolean blocked) {
        put("blocked", new Boolean(blocked));       
    }    
    
    public User getOwner() {
        String ownerId = (String) get("owner");
        if (ownerId == null) return null;
        return User.getUserById(ownerId);
    }
    
    public void setOwner(User owner) {
        put("owner", owner.getId());
    }
     
    public static NodeData getNodeDataById(String id) {
        NodeData b = new NodeData(id);     
        if (b.isValid()) return b;
        // Cleanup invalid, old node data
        b.clear();
        
        // Not created or valid
        return null;
    }    
    
    public static NodeData getNodeDataByName(String name) {
        String id = getFromIndex(TYPE, "Names", name);
        if (id == null) return null;
        NodeData nodeData = getNodeDataById(id);
        if (nodeData == null) {
            removeFromIndex(TYPE, "Names", name);
        }
        return nodeData;
    }    
    
    public static Collection<String> listIds() {
        return listIds(TYPE);
    }      
    
    public static String allocateId() {
        return Storable.allocateId(TYPE);
    }
    
    public void clear() {
        removeFromIndex(TYPE, "Names", getName());        
        super.clear();
    }
    
    protected void checkCanModify() {
        super.checkCanModify();
        Privilege privilege = SecurityMan.getCodePrivilege();
        if (privilege.isBelow(MODIFY_BY_CODE_PRIVILEGE)) {
            throw new SecurityException(privilege.getLabel() +
                      " code is not permitted to modify this entity");
        }
    }  
    
    protected void checkCanClear() {
        Privilege privilege = SecurityMan.getCodePrivilege();
        if (privilege.isBelow(CLEAR_BY_CODE_PRIVILEGE)) {
            throw new SecurityException(privilege.getLabel() +
                      " code is not permitted to clear this entity");
        }
    }    
}

