001    /*
002     * ----------------------------------------------------------------------
003     * Copyright (C) 2009 Enrique Lara (k957@68k.org)
004     *
005     * TinLizard is free software; you can redistribute it and/or
006     * modify it under the terms of the GNU Lesser General Public License
007     * as published by the Free Software Foundation; either version 3.0
008     * of the License, or (at your option) any later version.
009     *
010     * TinLizard is distributed in the hope that it will be useful,
011     * but WITHOUT ANY WARRANTY; without even the implied warranty of
012     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
013     * GNU Lesser General Public License for more details.
014     *
015     * You should have received a copy of the GNU Lesser General Public License
016     * along with TinLizard. If not, see http://www.gnu.org/licenses/.
017     * ----------------------------------------------------------------------
018     */
019    package tinlizard.web;
020    
021    import tinlizard.annotation.security.RolesAllowed;
022    
023    import tinlizard.model.Codeline;
024    import tinlizard.model.Policy;
025    
026    import tinlizard.util.Messages;
027    
028    import java.util.Collection;
029    import java.util.Date;
030    
031    import org.kohsuke.stapler.StaplerRequest;
032    import org.kohsuke.stapler.StaplerResponse;
033    import org.kohsuke.stapler.export.Exported;
034    
035    /**
036     * Decorate Policy for Stapler.
037     */
038    public final class PolicyWeb extends ObjectWeb<Policy> {
039        private final Policy subject;
040    
041        public PolicyWeb(final Policy c) {
042            if (c == null) {
043                throw new IllegalStateException(Messages.error_1010());
044            }
045    
046            this.subject = c;
047        }
048    
049        protected Policy getSubject() {
050            return this.subject;
051        }
052    
053        public Integer getId() {
054            return subject.getId();
055        }
056    
057        public String getName() {
058            return subject.getName();
059        }
060    
061        @Exported
062        public String getDescription() {
063            return subject.getDescription();
064        }
065    
066        public boolean isActive() {
067            return this.subject.isActive();
068        }
069    
070        @Exported
071        public Boolean getActive() {
072            return this.subject.getActive();
073        }
074    
075        @Exported
076        public Date getCreated() {
077            return subject.getCreated();
078        }
079    
080        @Exported
081        public String getCreatedBy() {
082            return subject.getCreatedBy();
083        }
084    
085        @Exported
086        public Date getLastModified() {
087            return subject.getLastModified();
088        }
089    
090        @Exported
091        public String getLastModifiedBy() {
092            return subject.getLastModifiedBy();
093        }
094    
095        @Exported(visibility = 1)
096        public CodelineCollectionWeb getCodelines() {
097            Collection<Codeline> allCodelines = this.subject.getCodelines();
098    
099            //ArrayUtils?XXX
100            if (((allCodelines != null) && !allCodelines.isEmpty())) {
101                return new CodelineCollectionWeb(allCodelines, Messages._Codelines());
102            } else {
103                return null;
104            }
105        }
106    
107        @RolesAllowed(RoleNames.ADMIN)
108        public void doDelete(final StaplerRequest request, final StaplerResponse response) throws Exception {
109            //XXX add check for matching codelines/views
110            subject.delete();
111            response.sendRedirect(request.getContextPath());
112        }
113    
114        //XXX prevent updating id?
115        @RolesAllowed(RoleNames.ADMIN)
116        public void doUpdate(final StaplerRequest request, final StaplerResponse response) throws Exception {
117            request.bindParameters(subject, "policy.");
118            subject.update();
119            gotoMyIndex(request, response);
120        }
121    }