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.View;
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 View for Stapler.
037     */
038    public final class ViewWeb extends ObjectWeb<View> {
039        private final View subject;
040    
041        public ViewWeb(final View c) {
042            if (c == null) {
043                throw new IllegalStateException(Messages.error_1010());
044            }
045    
046            this.subject = c;
047        }
048    
049        protected View 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        @Exported
067        public Date getCreated() {
068            return subject.getCreated();
069        }
070    
071        @Exported
072        public String getCreatedBy() {
073            return subject.getCreatedBy();
074        }
075    
076        @Exported
077        public Date getLastModified() {
078            return subject.getLastModified();
079        }
080    
081        @Exported
082        public String getLastModifiedBy() {
083            return subject.getLastModifiedBy();
084        }
085    
086        @Exported(visibility = 1)
087        public CodelineCollectionWeb getCodelines() {
088            Collection<Codeline> allCodelines = this.subject.getCodelines();
089    
090            //ArrayUtils?XXX
091            if (((allCodelines != null) && !allCodelines.isEmpty())) {
092                return new CodelineCollectionWeb(allCodelines, Messages._AllCodelines());
093            } else {
094                return null;
095            }
096        }
097    
098        public CodelineCollectionWeb getActiveCodelines() {
099            Collection<Codeline> codelines = this.subject.getActiveCodelines();
100    
101            if (codelines != null) {
102                return new CodelineCollectionWeb(codelines, Messages._ActiveCodelines());
103            } else {
104                return null;
105            }
106        }
107    
108        @RolesAllowed(RoleNames.USER)
109        public void doDelete(final StaplerRequest request, final StaplerResponse response) throws Exception {
110            subject.delete();
111            response.sendRedirect(request.getContextPath());
112        }
113    
114        //XXX prevent updating id?
115        @RolesAllowed(RoleNames.USER)
116        public void doUpdate(final StaplerRequest request, final StaplerResponse response) throws Exception {
117            request.bindParameters(subject, "view.");
118    
119            subject.update();
120            gotoMyIndex(request, response);
121        }
122    }