head	1.19;
access;
symbols
	TIGRIS_1_1_0RC2:1.12
	TIGRIS_1_1_0RC1:1.12
	TIGRIS_1_1:1.12.0.4
	TIGRIS_1_0_8:1.12
	TIGRIS_1_0_8RC3:1.12
	TIGRIS_1_0_8RC2:1.12
	TIGRIS_1_0_8RC1:1.12
	TIGRIS_1_0_7:1.12
	TIGRIS_1_0_7RC3:1.12
	TIGRIS_1_0_7RC2:1.12
	TIGRIS_1_0_7RC1:1.12
	TIGRIS_1_0_6:1.12
	TIGRIS_1_0_6RC5:1.12
	TIGRIS_1_0_6RC4:1.12
	TIGRIS_1_0_6RC3:1.12
	TIGRIS_1_0_6RC2:1.12
	TIGRIS_1_0_6RC1:1.12
	TIGRIS_1_0_5:1.12
	TIGRIS_1_0_5RC6:1.12
	TIGRIS_1_0_5RC5:1.12
	TIGRIS_1_0_5RC4:1.12
	TIGRIS_1_0_5RC3:1.12
	TIGRIS_1_0_5RC2:1.12
	TIGRIS_1_0_5RC1:1.12
	TIGRIS_1_0_4:1.12
	TIGRIS_1_0_3:1.12
	TIGRIS_1_0_2:1.12
	TIGRIS_1_0_1:1.12
	TIGRIS_1_0:1.12.0.6
	TIGRIS_1_0_0:1.12
	TIGRIS_1_0_0_RC1:1.12.0.2
	dlr:1.1.1
	HELM_PEER_PORT_BRANCH:1.1.1.1;
locks; strict;
comment	@# @;


1.19
date	2001.08.11.09.08.03;	author dlr;	state Exp;
branches;
next	1.18;

1.18
date	2001.07.23.20.14.33;	author dlr;	state Exp;
branches;
next	1.17;

1.17
date	2001.06.12.23.10.43;	author leonardr;	state Exp;
branches;
next	1.16;

1.16
date	2001.06.02.01.17.31;	author elicia;	state Exp;
branches;
next	1.15;

1.15
date	2001.06.01.00.32.53;	author elicia;	state Exp;
branches;
next	1.14;

1.14
date	2001.05.25.16.57.15;	author leonardr;	state Exp;
branches;
next	1.13;

1.13
date	2001.05.23.01.50.13;	author dlr;	state Exp;
branches;
next	1.12;

1.12
date	2001.03.13.01.41.32;	author elicia;	state Exp;
branches;
next	1.11;

1.11
date	2001.03.08.03.30.42;	author dlr;	state Exp;
branches;
next	1.10;

1.10
date	2001.03.07.01.53.22;	author lucas;	state Exp;
branches;
next	1.9;

1.9
date	2001.02.27.23.16.25;	author elicia;	state Exp;
branches;
next	1.8;

1.8
date	2001.02.27.02.15.20;	author elicia;	state Exp;
branches;
next	1.7;

1.7
date	2001.02.26.20.21.18;	author dlr;	state Exp;
branches;
next	1.6;

1.6
date	2001.02.24.11.43.01;	author dlr;	state Exp;
branches;
next	1.5;

1.5
date	2001.02.24.03.59.53;	author dlr;	state Exp;
branches;
next	1.4;

1.4
date	2001.02.22.01.21.27;	author dlr;	state Exp;
branches;
next	1.3;

1.3
date	2001.02.22.00.38.46;	author dlr;	state Exp;
branches;
next	1.2;

1.2
date	2001.02.21.03.02.22;	author edk;	state Exp;
branches;
next	1.1;

1.1
date	2001.02.09.23.43.46;	author elicia;	state dead;
branches
	1.1.1.1
	1.1.2.1;
next	;

1.1.1.1
date	2001.02.21.00.33.05;	author dlr;	state Exp;
branches;
next	;

1.1.2.1
date	2001.02.09.23.43.46;	author elicia;	state Exp;
branches;
next	1.1.2.2;

1.1.2.2
date	2001.02.10.00.03.08;	author elicia;	state Exp;
branches;
next	1.1.2.3;

1.1.2.3
date	2001.02.10.01.10.27;	author dlr;	state Exp;
branches;
next	1.1.2.4;

1.1.2.4
date	2001.02.10.02.20.28;	author leonardr;	state Exp;
branches;
next	1.1.2.5;

1.1.2.5
date	2001.02.12.16.24.06;	author leonardr;	state Exp;
branches;
next	1.1.2.6;

1.1.2.6
date	2001.02.12.21.24.29;	author leonardr;	state Exp;
branches;
next	1.1.2.7;

1.1.2.7
date	2001.02.12.23.29.16;	author elicia;	state Exp;
branches;
next	1.1.2.8;

1.1.2.8
date	2001.02.13.01.07.37;	author elicia;	state Exp;
branches;
next	1.1.2.9;

1.1.2.9
date	2001.02.13.22.45.20;	author elicia;	state Exp;
branches;
next	1.1.2.10;

1.1.2.10
date	2001.02.16.01.25.42;	author elicia;	state Exp;
branches;
next	1.1.2.11;

1.1.2.11
date	2001.02.16.03.01.27;	author dlr;	state Exp;
branches;
next	1.1.2.12;

1.1.2.12
date	2001.02.16.21.39.15;	author elicia;	state Exp;
branches;
next	1.1.2.13;

1.1.2.13
date	2001.02.17.00.20.37;	author elicia;	state Exp;
branches;
next	1.1.2.14;

1.1.2.14
date	2001.02.17.20.33.46;	author elicia;	state Exp;
branches;
next	1.1.2.15;

1.1.2.15
date	2001.02.18.00.49.44;	author elicia;	state Exp;
branches;
next	1.1.2.16;

1.1.2.16
date	2001.02.19.23.46.26;	author dlr;	state Exp;
branches;
next	1.1.2.17;

1.1.2.17
date	2001.02.20.22.43.31;	author dlr;	state Exp;
branches;
next	;


desc
@@


1.19
log
@Upgrading to Turbine 3.0-dev, Fulcrum 1.0, and Torque 1.0.
@
text
@package org.tigris.helm.om;

import java.util.*;

import org.apache.commons.util.exception.NestableException;

import org.apache.fulcrum.velocity.TurbineVelocity;

import org.apache.torque.om.NumberKey;
import org.apache.torque.util.Criteria;

import org.apache.velocity.VelocityContext;
import org.apache.velocity.context.Context;

import org.tigris.helm.om.*;
import org.tigris.helm.security.*;
import org.tigris.helm.tool.*;
import org.apache.turbine.util.Log;

/** 
 * News item.
 */
public class NewsItem extends BaseNewsItem
    implements ViewableBy
{
    /**
     * The name of the template used to create the body of the news item
     * disapproval email.
     */
    private static final String DISAPPROVE_TMPL =
        TemplateTool.getPathToMessageTemplate("NewsItemDisapprove.vm");

    /**
     * The approval permission.
     */
    private static final String APPROVE_PERM = "Project News - Approve";

    /**
     * Gets user that proposed the news item.
     */
    public final User getProposedByUser()
        throws Exception
    {
        return getUserRelatedByProposedBy();
    }

    /**
     * Gets user that approved the news item.
     */
    public User getApprovedByUser()
        throws Exception
    {
        return getUserRelatedByApprovedBy();
    }

    /**
     * Gets event date in short format, as a string.
     */
    public final String getEventDateString()
        throws Exception
    {
        return FormatDate.format("yyyy-MM-dd hh:mm:ss.S", 
                                 "MMM d, yyyy", 
                                 getEventDate().toString());
    }

    /**
     * Gets publish date in short format, as a string.
     */
    public final String getPublishDateString()
        throws Exception
    {
        return FormatDate.format("yyyy-MM-dd hh:mm:ss.S", 
                                 "MMM d, yyyy", 
                                 getPublishDate().toString());
    }

    /**
     * Gets remove date in short format, as a string.
     */
    public final String getRemoveDateString()
        throws Exception
    {
        return FormatDate.format("yyyy-MM-dd hh:mm:ss.S", 
                                 "MMM d, yyyy", 
                                 getRemoveDate().toString());
    }

    /**
     * Determines whether a news item should be posted now
     * or at a later date 
     */
    public boolean isPosted()
    {
        return (getPublishDate().compareTo(new Date()) <= 0);
    }

    /**
     * Determines whether a news item should be posted now
     * Or if it has already passed the removeDate() date
     */
    public boolean isRemoved()
    {
        boolean isRemoved = false;
        if (getRemoveDate() != null)
        {
            if (getRemoveDate().compareTo(new Date()) < 0)
                isRemoved = true;
        }
        return isRemoved;
    }

    /**
     * Determines whether a news item has been approved
     */
    public boolean isApproved()
        throws Exception
    {
        return !getApprovedBy().equals(UserPeer.getNobodyUser().getUserId());
    }


    public static NewsItem add(AccessControlList acl, Project project,
                               Date eventDate, String headline,
                               String body, Date publishDate,
                               Date removeDate, int priority,
                               String imageUrl, String articleUrl)
        throws Exception
    {
        // FIXME: ACL assertion must be done in this order to prevent error.
        NewsItem news = new NewsItem();
        news.setProjectId(project.getProjectId());
        news.assertAddPermission(acl);

        news.setHeadline(headline);
        news.setBody(body);
        news.setProposedBy(acl.getUser().getUserId());
        NumberKey approvedBy = (acl.hasPermission(APPROVE_PERM, project) ?
                                news.getProposedBy() : 
                                UserPeer.getNobodyUser().getUserId());
        news.setApprovedBy(approvedBy);
        news.setEventDate(eventDate);
        news.setPublishDate(publishDate);
        news.setRemoveDate(removeDate);
        news.setPriority(priority);
        news.setImageUrl(imageUrl);
        news.setOriginalArticleUrl(articleUrl);
        news.save();
        return news;
    }

    /**
     * Updates a news item.
     *
     * @@param acl           The access control list of the current user.
     * @@param description   A description of the news item.
     * @@param approvedBy    The database key of the approving user.
     * @@param isPrivate     Whether the news item is private.
     *
     * @@exception ACLException Insufficient permission.
     */
    public void edit(AccessControlList acl, Date eventDate, String headline,
                     String body, Date publishDate,  Date removeDate,
                     String priority, String imageUrl, String articleUrl)
        throws ACLException, Exception
    {
        assertEditPermission(acl);

        if (eventDate != null) setEventDate(eventDate);
        if (headline != null) setHeadline(headline);
        if (body != null) setBody(body);
        if (publishDate != null) setPublishDate(publishDate);
        if (removeDate != null) setRemoveDate(removeDate);
        if (priority != null) setPriority(Integer.parseInt(priority));
        if (imageUrl != null) setImageUrl(imageUrl);
        if (articleUrl != null) setOriginalArticleUrl(articleUrl);
        save();
    }

    /**
     * Approves this news item.
     */
    public void approve(AccessControlList acl)
        throws ACLException, Exception
    {
        if (acl != null) 
        {
            acl.assertPermission(APPROVE_PERM, getProject());
            setApprovedBy(acl.getUser().getUserId());
            save();
        }

    }

    /**
     * Disapproves this news item.
     *
     * @@param acl       The ACL to check for permission.
     * @@param rationale The reason for disapproving this news item.
     *
     * @@exception ACLException Insufficient permission.
     */
    public void disapprove(AccessControlList acl, String rationale)
        throws ACLException, Exception
    {
        acl.assertPermission(APPROVE_PERM, getProject());

        String subject = "News item for Project " + getProject().getName() +
            " disapproved.";
        Context ctx = new VelocityContext();
        ctx.put("disapprovingUser", acl.getUser());
        ctx.put("news", this);
        ctx.put("rationale", rationale);
        String body = TurbineVelocity.handleRequest(ctx, DISAPPROVE_TMPL);
        HelmEmailer emailer = new HelmEmailer();
        emailer.sendEmail(getProposedByUser().getEmail(), subject, body);
        delete();
    }

    /**
     * Deletes this news item.
     *
     * @@param acl The ACL to check for permission.
     *
     * @@exception ACLException Insufficient permission.
     */
    public void delete(AccessControlList acl)
        throws ACLException, Exception
    {
        assertDeletePermission(acl);
        delete();
    }

    /**
     * Performs the delete with no ACL checking.
     */
    private void delete()
        throws Exception
    {
        NewsItemPeer.doDelete(this);
    }

    //
    // Permission methods
    //

    /**
     * Asserts addition and/or approval permission for this news item.
     *
     * @@param acl The ACL to check for permission.
     *
     * @@exception ACLException Insufficient permission.
     */
    public void assertAddPermission(AccessControlList acl)
        throws ACLException, Exception
    {
        if (acl == null) {
            throw new Exception("No ACL provided");
        }
        else {
            acl.assertPermission("Project News - Suggest", getProject());
        }
    }

    /**
     * Asserts edit permission for this news item.  The proposer of this news
     * has lesser ACL requirements.
     *
     * @@param acl The ACL to check for permission.
     *
     * @@exception ACLException Insufficient permission.
     */
    public void assertEditPermission(AccessControlList acl)
        throws ACLException, Exception
    {
        if (acl == null) {
            throw new Exception("No ACL provided");
        }
        else if (getProposedBy().equals(acl.getUser().getUserId())) {
            acl.assertPermission("Project News - View", getProject());
        }
        else {
            acl.assertPermission("Project News - Edit", getProject());
        }
    }

    /**
     * Asserts deletion and/or disapproval permission for this news item.
     *
     * @@param acl The ACL to check for permission.
     *
     * @@exception ACLException Insufficient permission.
     */
    public void assertDeletePermission(AccessControlList acl)
        throws ACLException, Exception
    {
        if (acl == null) {
            throw new Exception("No ACL provided");
        }
        else if (getProposedBy().equals(acl.getUser().getUserId())) {
            acl.assertPermission("Project News - View", getProject());
        }
        else {
            acl.assertPermission("Project News - Delete", getProject());
        }
    }

    /**
     * Returns true if the given ACL can see this news item.
     */
    public boolean isViewableBy(AccessControlList acl)
        throws Exception
    {
        return (acl.hasPermission(requiredViewPermission(acl), getProject()));
    }

    /**
     * Returns the appropriate permission needed 
     */
    public String requiredViewPermission(AccessControlList acl)
        throws Exception
    {
        String permission = "Project News - View";
        if ( (!isApproved() || !isPosted() || isRemoved())
             && (!getProposedBy().equals(acl.getUser().getUserId())) )
        {
            permission = "Project News - Approve";
        }
        return permission;
    }
}
@


1.18
log
@Clean up of import statements.
@
text
@d5 6
a10 5
import org.apache.turbine.om.*;
import org.apache.turbine.util.db.Criteria;
import org.apache.turbine.util.Log;
import org.apache.turbine.util.TurbineException;
import org.apache.turbine.services.velocity.TurbineVelocity;
d12 1
d18 1
d210 1
a210 1
        Context ctx = TurbineVelocity.getContext();
@


1.17
log
@PeerBackedList performance boost and refactoring. This may be more complicated than it needs to be; rather than getting ever-increasing proportions of the table, it might be faster in most cases to get the whole table, so long as you stop processing results when the list is full enough.
@
text
@a4 2
import org.apache.velocity.context.Context;

a5 1
import org.apache.turbine.om.peer.BasePeer;
a6 3
import org.apache.turbine.util.ObjectUtils;
import org.apache.turbine.util.StringUtils;
import org.apache.turbine.util.ParameterParser;
a8 3
import org.apache.turbine.util.TurbineException;
import org.apache.turbine.util.db.pool.DBConnection;
import org.apache.turbine.services.TurbineServices;
d11 2
a14 1

@


1.16
log
@added extra fields for image URL and original article URL to add and edit methods
@
text
@d29 1
@


1.15
log
@added methods to return the date fields in shortened, String form
@
text
@d130 2
a131 1
                               Date removeDate, int priority)
d150 2
d168 1
a168 1
                     String priority)
d179 2
@


1.14
log
@.wm is now .vm
@
text
@d61 33
@


1.13
log
@Ported Helm from WebMacro to Velocity.
@
text
@d35 1
a35 1
        TemplateTool.getPathToMessageTemplate("NewsItemDisapprove.wm");
@


1.12
log
@fixed equality test in delete permission check
@
text
@d5 1
a5 1
import org.webmacro.servlet.WebContext;
d14 2
d18 1
a18 1
import org.apache.turbine.services.webmacro.TurbineWebMacro;
d176 1
a176 1
        WebContext ctx = TurbineWebMacro.getContext();
d180 1
a180 1
        String body = TurbineWebMacro.handleRequest(ctx, DISAPPROVE_TMPL);
@


1.11
log
@Pushed getID() method into base class as a final method.
@
text
@d264 1
a264 1
        else if (getProposedBy() == acl.getUser().getUserId()) {
@


1.10
log
@Corrected regex madness for Torque upgrade.  -dlr
@
text
@a39 10
    //
    // Accessor methods
    //


    public final String getID()
    {
        return getPrimaryKey().toString();
    }

@


1.9
log
@changed equality test for getProposedBy to use .equals instead of =
@
text
@d18 1
a18 2
import org.tigris.helm.om.peer.*;
import org.tigris.helm.om.base.*;
@


1.8
log
@fixed getApprovedBy method
@
text
@d254 1
a254 1
        else if (getProposedBy() == acl.getUser().getUserId()) {
d300 1
a300 1
             && (getProposedBy() != acl.getUser().getUserId()) )
@


1.7
log
@Changes to keep in step with the latest Torque-generated code.
@
text
@d99 1
a99 1
        return getApprovedBy() != UserPeer.getNobodyUser().getUserId();
@


1.6
log
@Make use of the TurbineWebMacro facade of WebMacroService for a more convienient interface.
@
text
@d117 1
a117 1
        SimpleKey approvedBy = (acl.hasPermission(APPROVE_PERM, project) ?
@


1.5
log
@Sync'd with latest Turbine changes.  Mostly this consists of the use of the new SimpleKey class to handle primary database keys.
@
text
@d16 1
a16 1
import org.apache.turbine.services.webmacro.WebMacroService;
d185 1
a185 3
        WebMacroService wmSrvc = (WebMacroService)
            TurbineServices.getInstance().getService(WebMacroService.SERVICE_NAME);
        WebContext ctx = wmSrvc.getContext();
d189 1
a189 1
        String body = wmSrvc.handleRequest(ctx, DISAPPROVE_TMPL);
@


1.4
log
@Find the mail template in the right place.
@
text
@d7 1
a7 1
import org.apache.turbine.om.BaseObject;
d48 1
a48 7
        return (String)getPrimaryKey();
    }

    public Project getProject()
         throws Exception
    {
        return ProjectPeer.getInstance(Integer.toString(getProjectId()));
d54 1
a54 1
    public User getProposedByUser()
d57 1
a57 3
        return (getProposedBy() <= 0) ?
                UserPeer.getAnonymousUser() :
                UserPeer.retrieveByPK(getProposedBy());
d66 1
a66 3
        return (getApprovedBy() <= 0) ?
                UserPeer.getAnonymousUser() :
                UserPeer.retrieveByPK(getApprovedBy());
d117 3
a119 3
        int approvedBy = (acl.hasPermission(APPROVE_PERM, project) ?
                          news.getProposedBy() : 
                          UserPeer.getNobodyUser().getUserId());
@


1.3
log
@Now using TurbineWebMacroService instead of TemplateInterpolator.
@
text
@a28 1

d33 2
a34 1
    private static final String DISAPPROVE_TMPL = "NewsItemDisapprove.wm";
@


1.2
log
@adding files from the branch to the head
@
text
@d5 1
a5 1
import org.webmacro.engine.BuildContext;
d15 2
d195 3
a197 1
        BuildContext ctx = TemplateInterpolator.createContext();
d201 1
a201 2
        String body = TemplateInterpolator.interpolate(DISAPPROVE_TMPL, ctx,
                                                       getClass());
@


1.1
log
@file NewsItem.java was initially added on branch HELM_PEER_PORT_BRANCH.
@
text
@d1 315
@


1.1.1.1
log
@Replacing the HEAD with the HELM_PEER_PORT_BRANCH.
@
text
@a0 315
package org.tigris.helm.om;

import java.util.*;

import org.webmacro.engine.BuildContext;

import org.apache.turbine.om.BaseObject;
import org.apache.turbine.om.peer.BasePeer;
import org.apache.turbine.util.db.Criteria;
import org.apache.turbine.util.ObjectUtils;
import org.apache.turbine.util.StringUtils;
import org.apache.turbine.util.ParameterParser;
import org.apache.turbine.util.Log;
import org.apache.turbine.util.db.pool.DBConnection;

import org.tigris.helm.om.peer.*;
import org.tigris.helm.om.base.*;
import org.tigris.helm.security.*;

import org.tigris.helm.tool.*;

/** 
 * News item.
 */
public class NewsItem extends BaseNewsItem
{

    /**
     * The name of the template used to create the body of the news item
     * disapproval email.
     */
    private static final String DISAPPROVE_TMPL = "NewsItemDisapprove.wm";

    /**
     * The approval permission.
     */
    private static final String APPROVE_PERM = "Project News - Approve";

    //
    // Accessor methods
    //


    public final String getID()
    {
        return (String)getPrimaryKey();
    }

    public Project getProject()
         throws Exception
    {
        return ProjectPeer.getInstance(Integer.toString(getProjectId()));
    }

    /**
     * Gets user that proposed the news item.
     */
    public User getProposedByUser()
        throws Exception
    {
        return (getProposedBy() <= 0) ?
                UserPeer.getAnonymousUser() :
                UserPeer.retrieveByPK(getProposedBy());
    }

    /**
     * Gets user that approved the news item.
     */
    public User getApprovedByUser()
        throws Exception
    {
        return (getApprovedBy() <= 0) ?
                UserPeer.getAnonymousUser() :
                UserPeer.retrieveByPK(getApprovedBy());
    }

    /**
     * Determines whether a news item should be posted now
     * or at a later date 
     */
    public boolean isPosted()
    {
        return (getPublishDate().compareTo(new Date()) <= 0);
    }

    /**
     * Determines whether a news item should be posted now
     * Or if it has already passed the removeDate() date
     */
    public boolean isRemoved()
    {
        boolean isRemoved = false;
        if (getRemoveDate() != null)
        {
            if (getRemoveDate().compareTo(new Date()) < 0)
                isRemoved = true;
        }
        return isRemoved;
    }

    /**
     * Determines whether a news item has been approved
     */
    public boolean isApproved()
        throws Exception
    {
        return getApprovedBy() != UserPeer.getNobodyUser().getUserId();
    }


    public static NewsItem add(AccessControlList acl, Project project,
                               Date eventDate, String headline,
                               String body, Date publishDate,
                               Date removeDate, int priority)
        throws Exception
    {
        // FIXME: ACL assertion must be done in this order to prevent error.
        NewsItem news = new NewsItem();
        news.setProjectId(project.getProjectId());
        news.assertAddPermission(acl);

        news.setHeadline(headline);
        news.setBody(body);
        news.setProposedBy(acl.getUser().getUserId());
        int approvedBy = (acl.hasPermission(APPROVE_PERM, project) ?
                          news.getProposedBy() : 
                          UserPeer.getNobodyUser().getUserId());
        news.setApprovedBy(approvedBy);
        news.setEventDate(eventDate);
        news.setPublishDate(publishDate);
        news.setRemoveDate(removeDate);
        news.setPriority(priority);
        news.save();
        return news;
    }

    /**
     * Updates a news item.
     *
     * @@param acl           The access control list of the current user.
     * @@param description   A description of the news item.
     * @@param approvedBy    The database key of the approving user.
     * @@param isPrivate     Whether the news item is private.
     *
     * @@exception ACLException Insufficient permission.
     */
    public void edit(AccessControlList acl, Date eventDate, String headline,
                     String body, Date publishDate,  Date removeDate,
                     String priority)
        throws ACLException, Exception
    {
        assertEditPermission(acl);

        if (eventDate != null) setEventDate(eventDate);
        if (headline != null) setHeadline(headline);
        if (body != null) setBody(body);
        if (publishDate != null) setPublishDate(publishDate);
        if (removeDate != null) setRemoveDate(removeDate);
        if (priority != null) setPriority(Integer.parseInt(priority));
        save();
    }

    /**
     * Approves this news item.
     */
    public void approve(AccessControlList acl)
        throws ACLException, Exception
    {
        if (acl != null) 
        {
            acl.assertPermission(APPROVE_PERM, getProject());
            setApprovedBy(acl.getUser().getUserId());
            save();
        }

    }

    /**
     * Disapproves this news item.
     *
     * @@param acl       The ACL to check for permission.
     * @@param rationale The reason for disapproving this news item.
     *
     * @@exception ACLException Insufficient permission.
     */
    public void disapprove(AccessControlList acl, String rationale)
        throws ACLException, Exception
    {
        acl.assertPermission(APPROVE_PERM, getProject());

        String subject = "News item for Project " + getProject().getName() +
            " disapproved.";
        BuildContext ctx = TemplateInterpolator.createContext();
        ctx.put("disapprovingUser", acl.getUser());
        ctx.put("news", this);
        ctx.put("rationale", rationale);
        String body = TemplateInterpolator.interpolate(DISAPPROVE_TMPL, ctx,
                                                       getClass());
        HelmEmailer emailer = new HelmEmailer();
        emailer.sendEmail(getProposedByUser().getEmail(), subject, body);
        delete();
    }

    /**
     * Deletes this news item.
     *
     * @@param acl The ACL to check for permission.
     *
     * @@exception ACLException Insufficient permission.
     */
    public void delete(AccessControlList acl)
        throws ACLException, Exception
    {
        assertDeletePermission(acl);
        delete();
    }

    /**
     * Performs the delete with no ACL checking.
     */
    private void delete()
        throws Exception
    {
        NewsItemPeer.doDelete(this);
    }

    //
    // Permission methods
    //

    /**
     * Asserts addition and/or approval permission for this news item.
     *
     * @@param acl The ACL to check for permission.
     *
     * @@exception ACLException Insufficient permission.
     */
    public void assertAddPermission(AccessControlList acl)
        throws ACLException, Exception
    {
        if (acl == null) {
            throw new Exception("No ACL provided");
        }
        else {
            acl.assertPermission("Project News - Suggest", getProject());
        }
    }

    /**
     * Asserts edit permission for this news item.  The proposer of this news
     * has lesser ACL requirements.
     *
     * @@param acl The ACL to check for permission.
     *
     * @@exception ACLException Insufficient permission.
     */
    public void assertEditPermission(AccessControlList acl)
        throws ACLException, Exception
    {
        if (acl == null) {
            throw new Exception("No ACL provided");
        }
        else if (getProposedBy() == acl.getUser().getUserId()) {
            acl.assertPermission("Project News - View", getProject());
        }
        else {
            acl.assertPermission("Project News - Edit", getProject());
        }
    }

    /**
     * Asserts deletion and/or disapproval permission for this news item.
     *
     * @@param acl The ACL to check for permission.
     *
     * @@exception ACLException Insufficient permission.
     */
    public void assertDeletePermission(AccessControlList acl)
        throws ACLException, Exception
    {
        if (acl == null) {
            throw new Exception("No ACL provided");
        }
        else if (getProposedBy() == acl.getUser().getUserId()) {
            acl.assertPermission("Project News - View", getProject());
        }
        else {
            acl.assertPermission("Project News - Delete", getProject());
        }
    }

    /**
     * Returns true if the given ACL can see this news item.
     */
    public boolean isViewableBy(AccessControlList acl)
        throws Exception
    {
        return (acl.hasPermission(requiredViewPermission(acl), getProject()));
    }

    /**
     * Returns the appropriate permission needed 
     */
    public String requiredViewPermission(AccessControlList acl)
        throws Exception
    {
        String permission = "Project News - View";
        if ( (!isApproved() || !isPosted() || isRemoved())
             && (getProposedBy() != acl.getUser().getUserId()) )
        {
            permission = "Project News - Approve";
        }
        return permission;
    }
}
@


1.1.2.1
log
@renamed ProjectNews to NewsItem
@
text
@a0 103
package org.tigris.helm.om;

import java.util.*;

import org.apache.turbine.om.BaseObject;
import org.apache.turbine.om.peer.BasePeer;
import org.apache.turbine.util.db.Criteria;
import org.apache.turbine.util.ObjectUtils;
import org.apache.turbine.util.StringUtils;
import org.apache.turbine.util.ParameterParser;
import org.apache.turbine.util.Log;
import org.apache.turbine.util.db.pool.DBConnection;

import org.tigris.helm.om.peer.*;

/** 
    The skeleton for this class was autogenerated by Torque on: Fri Jan 26 17:54:39 PST 2001. 
    You should add additional methods to this class to meet the
    application requirements.  This class will only be generated as
    long as the firstTime file does not exist in the build directory.
*/
public class NewsItem extends BaseNewsItem
{

    //
    // Accessor methods
    //



    /**
     * Determines whether a news item should be posted now
     * or at a later date 
     */
    public boolean isPosted()
    {
        boolean isPosted = true;
        if  (getPublishOn().compareTo(new Date()) > 0)
        {
            isPosted = false;
        }
        
        return isPosted;
    }

    /**
     * Determines whether a news item should be posted now
     * Or if it has already passed the removeOn() date
     */
    public boolean isRemoved()
    {
        boolean isRemoved = false;
        if (getRemoveOn() != null)
        {
            if (getRemoveOn().compareTo(new Date()) < 0)
                isRemoved = true;
        }
        return isRemoved;
    }

    /**
     * Determines whether a news item has been approved
     */
    public boolean isApproved()
        throws Exception
    {
        return getApprovedBy() != UserPeer.getNobodyUser().getUserId();
    }

    /**
     * TODO: List other parameters.
     */
    public static NewsItem add(Project project, Object otherParamsHere)
        throws Exception
    {
        NewsItem news = new NewsItem();
        // TODO: Set attributes.
        //news.setXXX(yyy);
        NewsItemPeer.doInsert(news);
        return news;
    }

    //
    // Permission methods
    //

    /**
     * Returns the appropriate permission needed 
     */
    public String requiredViewPermission()
        throws Exception
    {
        String permission = "Project News - Edit";
        if (isPosted() && !isRemoved() && isApproved())
        {
            permission = "Project News - View";
        } else if (!isApproved()) {
            permission = "Project News - Approve";
        }
        return permission;
    }

}
@


1.1.2.2
log
@added getID method

added arguments to add method
@
text
@a14 1
import org.tigris.helm.security.AccessControlList;
a29 4
    public String getID()
    {
        return (String)getPrimaryKey();
    }
d73 1
a73 4
    public static NewsItem add(Project project, AccessControlList acl,
                               Date eventDate, String headline,
                               String body, Date publishDate,   
                               Date removeDate, String priority)
@


1.1.2.3
log
@Sub of approve().
@
text
@d15 1
a15 1
import org.tigris.helm.security.*;
d18 5
a22 2
 * News item.
 */
a88 14
    }

    /**
     * Approves this news item.
     */
    public void approve(AccessControlList acl)
        throws ACLException, Exception
    {
        if (acl != null) {
            acl.assertPermissionForProject("Project News - Approve",
                                           null);
            // TODO
            save();
        }
@


1.1.2.4
log
@Added new news item methods.
@
text
@a4 2
import org.webmacro.engine.BuildContext;

a16 2
import org.tigris.helm.tool.*;

a22 6
    /**
     * The name of the template used to create the body of the news item
     * disapproval email.
     */
    private static final String DISAPPROVE_TMPL = "NewsItemDisapprove.wm";

a88 34
     * Updates a news item.
     *
     * @@param acl           The access control list of the current user.
     * @@param description   A description of the news item.
     * @@param approvedBy    The database key of the approving user.
     * @@param isPrivate     Whether the news item is private.
     *
     * @@exception SQLException
     * @@exception DataSetException
     */
    public void edit (AccessControlList acl, Date eventDate,
                      String headline, String body, Date publishOn,
                      Date removeOn, String priority)
        throws ACLException, Exception
    {
        String action;
        if (Integer.parseInt(acl.getUserID()) == getProposedBy())
        {
            action = "Project News - View";
        } else {
            action = "Project News - Edit";
        }
        acl.assertPermissionForProject(action, getProject());

        if (eventDate != null) setEventDate(eventDate);
        if (headline != null) setHeadline(headline);
        if (body != null) setBody(body);
        if (publishOn != null) setPublishOn(publishOn);
        if (removeOn != null) setRemoveOn(removeOn);
        if (priority != null) setPriority(Integer.parseInt(priority));
        save();
    }

    /**
d94 5
a98 3
        if (acl == null) 
        {
            throw new Exception("No ACL provided");
a99 54

        acl.assertPermissionForProject("Project News - Approve",
                                       null);
        User u = acl.getUser();
        setApprovedBy(u.getUserId());
        save();
    }

    /**
     * Approves this news item.
     */
    public void disapprove(AccessControlList acl, String rationale)
        throws ACLException, Exception
    {
        if (acl == null) 
        {
            throw new Exception("No ACL provided.");
        }

        acl.assertPermissionForProject("Project News - Approve",
                                       null);

        String subject = "News item for Project " + getProject().getName() +
            " disapproved.";
        BuildContext ctx = TemplateInterpolator.createContext();
        ctx.put("disapprovingUser", acl.getUser());
        ctx.put("news", this);
        ctx.put("rationale", rationale);
        String body = TemplateInterpolator.interpolate(DISAPPROVE_TMPL, ctx,
                                                       getClass());
        HelmEmailer emailer = new HelmEmailer();
        emailer.sendEmail(UserPeer.getInstance(String.valueOf(getProposedBy()))
                          .getEmail(), subject, body);
        deleteNC();
    }

    public void delete(AccessControlList acl)
        throws ACLException, Exception
    {
        if (acl == null) 
        {
            throw new Exception("No ACL provided.");
        }
        acl.assertPermissionForProject("Project News - Delete",
                                       null);
        deleteNC();
    }

    private void deleteNC()
        throws Exception
    {
        Criteria c = new Criteria()
            .add(NewsItemPeer.NEWS_ID, getNewsId());
        NewsItemPeer.doDelete(c);
@


1.1.2.5
log
@Pass the appropriate object to permission checking.
@
text
@d121 1
a121 1
        acl.assertPermission(action, getProject());
d143 2
a144 2
        acl.assertPermission("Project News - Approve",
                                       getProject());
d161 2
a162 2
        acl.assertPermission("Project News - Approve",
                                       getProject());
d185 2
a186 2
        acl.assertPermission("Project News - Delete",
                                       getProject());
@


1.1.2.6
log
@Added isViewableBy method.
@
text
@a202 9
     * Returns true iff the given ACL can see this news item.
     */
    public boolean isViewableBy(AccessControlList acl)
        throws Exception
    {
        return (acl.hasPermission(requiredViewPermission(), getProject()));
    }

    /**
@


1.1.2.7
log
@added attributes to add method

added getProject method
@
text
@a42 7
    public Project getProject()
         throws Exception
    {
        return ProjectPeer.getInstance(Integer.toString(getProjectId()));
    }


d87 2
a88 2
                               String body, Date publishOn,   
                               Date removeOn, int priority)
d92 2
a93 6
        news.setHeadline(headline);
        news.setBody(body);
        news.setEventDate(eventDate);
        news.setPublishOn(publishOn);
        news.setRemoveOn(removeOn);
        news.setPriority(priority);
@


1.1.2.8
log
@changed field names from removeOn and publishOn to removeDate and publishDate
@
text
@d57 1
a57 1
        if  (getPublishDate().compareTo(new Date()) > 0)
d67 1
a67 1
     * Or if it has already passed the removeDate() date
d72 1
a72 1
        if (getRemoveDate() != null)
d74 1
a74 1
            if (getRemoveDate().compareTo(new Date()) < 0)
d89 3
a91 1

d94 2
a95 2
                               String body, Date publishDate,   
                               Date removeDate, int priority)
d102 2
a103 2
        news.setPublishDate(publishDate);
        news.setRemoveDate(removeDate);
d121 2
a122 2
                      String headline, String body, Date publishDate,
                      Date removeDate, String priority)
d137 2
a138 2
        if (publishDate != null) setPublishDate(publishDate);
        if (removeDate != null) setRemoveDate(removeDate);
@


1.1.2.9
log
@added permission methods
@
text
@a32 5
    /**
     * The approval permission.
     */
    private static final String APPROVE_PERM = "Project News - Approve";

d90 4
a93 4
    public static NewsItem add(AccessControlList acl, Date eventDate,
                               String headline, String body, 
                               Date publishDate, Date removeDate,
                               int priority)
a102 1
        news.assertAddPermission(acl);
d124 7
a130 1
        assertEditPermission(acl);
d152 2
a153 1
        acl.assertPermission(APPROVE_PERM, getProject());
d160 1
a160 1
     * Disapproves this news item.
d170 2
a171 1
        acl.assertPermission(APPROVE_PERM, getProject());
d194 2
a195 1
        assertDeletePermission(acl);
d212 1
a212 44
     * Asserts addition and/or approval permission for this news item.
     *
     * @@param acl The ACL to check for permission.
     */
    public void assertAddPermission(AccessControlList acl)
        throws ACLException, Exception
    {
        acl.assertPermission("Project News - Suggest", getProject());
    }

    /**
     * Asserts edit permission for this news item.
     *
     * @@param acl The ACL to check for permission.
     */
    public void assertEditPermission(AccessControlList acl)
        throws ACLException, Exception
    {
        if (getProposedBy() == acl.getUser().getUserId())
        {
            acl.assertPermission("Project News - View", getProject());
        } else {
            acl.assertPermission("Project News - Edit", getProject());
        }
    }

    /**
     * Asserts deletion and/or disapproval permission for this news item.
     *
     * @@param acl The ACL to check for permission.
     */
    public void assertDeletePermission(AccessControlList acl)
        throws ACLException, Exception
    {
        if (getProposedBy() == acl.getUser().getUserId())
        {
            acl.assertPermission("Project News - View", getProject());
        } else {
            acl.assertPermission("Project News - Delete", getProject());
        }
    }

    /**
     * Returns true if the given ACL can see this news item.
d217 1
a217 1
        return (acl.hasPermission(requiredViewPermission(acl), getProject()));
d223 1
a223 1
    public String requiredViewPermission(AccessControlList acl)
d226 2
a227 2
        String permission = "Project News - View";
        if ((!isApproved() || !isPosted() || isRemoved()) && (getProposedBy() != acl.getUser().getUserId()))
d229 2
a234 1

@


1.1.2.10
log
@add method needs project passed to it
@
text
@d95 4
a98 4
    public static NewsItem add(AccessControlList acl, Project project,
                               Date eventDate, String headline,
                               String body, Date publishDate,
                               Date removeDate, int priority)
a101 2
        news.setProjectId(project.getProjectId());
        news.assertAddPermission(acl);
d108 1
@


1.1.2.11
log
@Added new imports necessitated by Torque.
@
text
@a16 1
import org.tigris.helm.om.base.*;
@


1.1.2.12
log
@setting proposedBy in the add method
@
text
@a54 16
    /**
     * Gets user that proposed the news item.
     */
    public User getProposedByUser()
        throws Exception
    {
        User proposedByUser = null;
        int proposedById = getProposedBy();
        if (proposedById == 0)
        { 
            proposedByUser = UserPeer.getAnonymousUser();
        } else {
            proposedByUser = UserPeer.retrieveByPK(getProposedBy());
        }
        return proposedByUser;
    }
a106 5
        news.setProposedBy(acl.getUser().getUserId());
        int approvedBy = (acl.hasPermission(APPROVE_PERM, project) ?
                          news.getProposedBy() : 
                          UserPeer.getNobodyUser().getUserId());
        news.setApprovedBy(approvedBy);
@


1.1.2.13
log
@cleanup
@
text
@d61 9
a69 3
        return (getProposedBy() == 0) ?
                UserPeer.getAnonymousUser() :
                UserPeer.retrieveByPK(getProposedBy());
@


1.1.2.14
log
@minor cleanup
@
text
@d126 1
a126 1
        news.save();
@


1.1.2.15
log
@cleanup
@
text
@d164 1
a164 1
        if (acl != null) 
d166 1
a166 3
            acl.assertPermission(APPROVE_PERM, getProject());
            setApprovedBy(acl.getUser().getUserId());
            save();
d169 4
@


1.1.2.16
log
@* Simplified implementation of getProposedByUser().
* Added getApprovedByUser() : User.
* Simplified implementation of isPosted().
* Noted poor implementation of assertAddPermission() in add().
* Corrected JavaDoc and removed unused local variable on edit().
* Added JavaDoc to disapprove().
* Renamed deleteNC() to delete() and added JavaDoc.
* Moved checks for a null ACL into the assert*Permission() methods.
@
text
@d61 1
a61 1
        return (getProposedBy() <= 0) ?
a66 11
     * Gets user that approved the news item.
     */
    public User getApprovedByUser()
        throws Exception
    {
        return (getApprovedBy() <= 0) ?
                UserPeer.getAnonymousUser() :
                UserPeer.retrieveByPK(getApprovedBy());
    }

    /**
d72 7
a78 1
        return (getPublishDate().compareTo(new Date()) <= 0);
a111 1
        // FIXME: ACL assertion must be done in this order to prevent error.
a114 1

d138 2
a139 1
     * @@exception ACLException Insufficient permission.
d141 3
a143 3
    public void edit(AccessControlList acl, Date eventDate, String headline,
                     String body, Date publishDate,  Date removeDate,
                     String priority)
d146 1
a174 5
     *
     * @@param acl       The ACL to check for permission.
     * @@param rationale The reason for disapproving this news item.
     *
     * @@exception ACLException Insufficient permission.
d179 5
d195 3
a197 2
        emailer.sendEmail(getProposedByUser().getEmail(), subject, body);
        delete();
a199 7
    /**
     * Deletes this news item.
     *
     * @@param acl The ACL to check for permission.
     *
     * @@exception ACLException Insufficient permission.
     */
d203 4
d208 1
a208 1
        delete();
d211 1
a211 4
    /**
     * Performs the delete with no ACL checking.
     */
    private void delete()
d214 3
a216 1
        NewsItemPeer.doDelete(this);
a226 2
     *
     * @@exception ACLException Insufficient permission.
d231 1
a231 6
        if (acl == null) {
            throw new Exception("No ACL provided");
        }
        else {
            acl.assertPermission("Project News - Suggest", getProject());
        }
d235 1
a235 2
     * Asserts edit permission for this news item.  The proposer of this news
     * has lesser ACL requirements.
a237 2
     *
     * @@exception ACLException Insufficient permission.
d242 2
a243 4
        if (acl == null) {
            throw new Exception("No ACL provided");
        }
        else if (getProposedBy() == acl.getUser().getUserId()) {
d245 1
a245 2
        }
        else {
a253 2
     *
     * @@exception ACLException Insufficient permission.
d258 2
a259 4
        if (acl == null) {
            throw new Exception("No ACL provided");
        }
        else if (getProposedBy() == acl.getUser().getUserId()) {
d261 1
a261 2
        }
        else {
d282 1
a282 2
        if ( (!isApproved() || !isPosted() || isRemoved())
             && (getProposedBy() != acl.getUser().getUserId()) )
d288 2
@


1.1.2.17
log
@Inline getID().
@
text
@d44 1
a44 1
    public final String getID()
@


