/* ======================================================================
   Parts Copyright 2006 University of Leeds, Oxford University, University of the Highlands and Islands.

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.

====================================================================== */

package org.bodington.server;


import java.security.acl.NotOwnerException;

import org.bodington.server.realm.Acl;
import org.bodington.server.realm.AclEntry;
import org.bodington.server.realm.Group;
import org.bodington.server.realm.Permission;
import org.bodington.server.resources.EasyEditResource;
import org.bodington.server.resources.QuickLink;
import org.bodington.server.resources.Resource;
import org.bodington.server.resources.ResourceTreeManager;
import org.bodington.server.resources.ResourceUtils;
import org.bodington.servlet.FacilityList;
import org.bodington.servlet.facilities.Facility;

public class HomeSessionImpl extends BuildingSessionImpl implements
    HomeSession
{

    public HomeSessionImpl()
    {
        super();
    }

    public void createBookmark(Resource resource)
        throws BuildingServerException
    {
        Resource container = getBookmarkContainer();
        QuickLink link = new QuickLink();
        
        
        link.setDescription(resource.getDescription());
        link.setTitle(resource.getTitle());
        
        String name = BuildingSessionManagerImpl.getSession(container)
            .findUniqueName(resource.getName());
        link.setName(name);
        link.setIntroduction("<!-- -->");
        link.setUseParentAcl(true);
        link.setResourceIdLink(resource.getResourceId());
        link.setHttpFacilityNo(FacilityList.getFacilities().get("quicklink").id.intValue());
        try
        {
            ResourceTreeManager.getInstance().addResource(getBookmarkContainer(),link);
        }
        catch (BuildingServerException bse)
        {
            ResourceTreeManager.getInstance().removeResource(link);
        }
    }
    
    public Resource getBookmarkContainer() throws BuildingServerException
    {
        Resource container = findBookmarkContainer();
        if (container == null)
            container = createBookmarkContainer();
    	return container;
    }
    
    Resource findBookmarkContainer() throws BuildingServerException
    {
        return getResource().findChild("bookmarks");
    }
    
    Resource createBookmarkContainer() throws BuildingServerException
    {
        Resource container = new Resource("bookmarks", "My Bookmarks", "All your bookmarks are placed here.", "Here are the bookmarks that I've stored.");
        container.setHttpFacilityNo(FacilityList.getFacilities().get("suite").id.intValue());
        ResourceTreeManager.getInstance().addResource(getResource(), container);
        return container;
    }

    public boolean isPublic() throws BuildingServerException
    {
        return ResourceUtils.anyPrincipal( getResource(), 
            Facility.getPublicGroups(), Permission.VIEW );
    }

    public boolean isVisible() throws BuildingServerException
    {
        return ResourceUtils.anyPrincipal( getResource(),
            Facility.getPublicGroups(), Permission.SEE );
    }
    
    

    public void makePublic() throws BuildingServerException, NotOwnerException
    {
        Group group = Group.findGroupByName("public");
        Acl acl = getResource().getAcl();
        AclEntry aclEntry = acl.getAclEntry( group.getGroupId(), false);
        if (aclEntry == null)
        {
            aclEntry = new AclEntry();
            aclEntry.setGroupId(group.getGroupId());
            acl.addEntry(aclEntry);
        }
        aclEntry.addPermission(Permission.SEE);
        aclEntry.addPermission(Permission.VIEW);
        acl.save();
    }

    public void makeVisible() throws BuildingServerException, NotOwnerException
    {
        Group group = Group.findGroupByName("public");
        Acl acl = getResource().getAcl();
        AclEntry aclEntry = acl.getAclEntry( group.getGroupId(), false);
        if (aclEntry == null)
        {
            aclEntry = new AclEntry();
            aclEntry.setGroupId(group.getGroupId());
            acl.addEntry(aclEntry);
        }
        aclEntry.addPermission(Permission.SEE);
        acl.save();
    }
    
    public void createNotes() throws BuildingServerException
    {
        EasyEditResource notes = new EasyEditResource();
        notes.setName("notes");
        notes.setTitle("My Notes");
        notes.setDescription("Your own personal notes.");
        notes.setIntroduction("Your own personal notes.");
        notes.setUseParentAcl(false);
        int httpNo = FacilityList.getFacilities().get("easyedit").id.intValue();
        notes.setHttpFacilityNo(httpNo);
        try
        {
            ResourceTreeManager.getInstance().addResource(getResource(), notes);
        }
        catch (BuildingServerException bse)
        {
            ResourceTreeManager.getInstance().removeResource(notes);
            throw bse;
        }
    }
}
