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    import tinlizard.model.Project;
026    import tinlizard.model.User;
027    import tinlizard.model.View;
028    
029    import tinlizard.util.Messages;
030    
031    import java.util.Collection;
032    import java.util.Date;
033    
034    import org.apache.log4j.Logger;
035    
036    import org.kohsuke.stapler.StaplerRequest;
037    import org.kohsuke.stapler.StaplerResponse;
038    import org.kohsuke.stapler.export.Exported;
039    
040    /**
041     * Decorate Project for Stapler.
042     */
043    public final class ProjectWeb extends ObjectWeb<Project> {
044        private static final Logger LOG = Logger.getLogger(ProjectWeb.class);
045        private final Project subject;
046    
047        public ProjectWeb(final Project p) {
048            if (p == null) {
049                throw new IllegalStateException(Messages.error_1012());
050            }
051    
052            this.subject = p;
053        }
054    
055        protected Class<Project> getManagedClass() {
056            return Project.class;
057        }
058    
059        protected Project getSubject() {
060            return this.subject;
061        }
062    
063        public Integer getId() {
064            return this.subject.getId();
065        }
066    
067        public String getName() {
068            return this.subject.getName();
069        }
070    
071        @Exported
072        public String getDescription() {
073            return this.subject.getDescription();
074        }
075    
076        @Exported
077        public Date getCreated() {
078            return subject.getCreated();
079        }
080    
081        @Exported
082        public String getCreatedBy() {
083            return subject.getCreatedBy();
084        }
085    
086        @Exported
087        public Date getLastModified() {
088            return subject.getLastModified();
089        }
090    
091        @Exported
092        public String getLastModifiedBy() {
093            return subject.getLastModifiedBy();
094        }
095    
096        @Exported
097        public String getScmConnection() {
098            Codeline codeline = subject.findMainLine();
099    
100            if (codeline != null) {
101                return codeline.getScmConnection();
102            } else {
103                return null;
104            }
105        }
106    
107        @Exported(visibility = 1)
108        public CodelineCollectionWeb getCodelines() {
109            Collection<Codeline> codelines = this.subject.getCodelines();
110    
111            if (codelines != null) {
112                return new CodelineCollectionWeb(codelines, Messages._AllCodelines());
113            } else {
114                return null;
115            }
116        }
117    
118        public CodelineCollectionWeb getActiveCodelines() {
119            Collection<Codeline> codelines = this.subject.getActiveCodelines();
120    
121            if (codelines != null) {
122                return new CodelineCollectionWeb(codelines, Messages._ActiveCodelines());
123            } else {
124                return null;
125            }
126        }
127    
128        public CodelineWeb getMainline() {
129            Codeline codeline = subject.findMainLine();
130    
131            if (codeline != null) {
132                return new CodelineWeb(codeline);
133            } else {
134                return null;
135            }
136        }
137    
138        public CodelineWeb getCodeline(final String name) {
139            if (LOG.isDebugEnabled()) {
140                LOG.debug(name);
141            }
142    
143            Codeline codeline = subject.getCodeline(name);
144    
145            if (codeline != null) {
146                return new CodelineWeb(codeline);
147            } else {
148                return null;
149            }
150        }
151    
152        @RolesAllowed(RoleNames.USER)
153        public void doDelete(final StaplerRequest request, final StaplerResponse response) throws Exception {
154            subject.delete();
155            response.sendRedirect(request.getContextPath());
156        }
157    
158        //XXX prevent updating id?
159        @RolesAllowed(RoleNames.USER)
160        public void doUpdate(final StaplerRequest request, final StaplerResponse response) throws Exception {
161            request.bindParameters(subject, "project.");
162            subject.update();
163            gotoMyIndex(request, response);
164        }
165    
166        @RolesAllowed(RoleNames.USER)
167        public void doAddCodeline(final StaplerRequest request, final StaplerResponse response) throws Exception {
168            Codeline f = new Codeline();
169            request.bindParameters(f, "codeline.");
170    
171            View view = new View();
172            request.bindParameters(view, "view.");
173            f.setView(View.findByName(view.getName()));
174    
175            User user = new User();
176            request.bindParameters(user, "owner.");
177            user = User.getUserByName(user.getName(), true);
178            f.setOwner(user);
179    
180            String policyName = request.getParameter("policy.name");
181            f.setPolicy(Policy.findByName(policyName));
182    
183            subject.addCodeline(f);
184    
185            response.sendRedirect(request.getContextPath() + "/project/" + subject.getName() + "/codeline/" + f.getName());
186        }
187    }