View Javadoc

1   /*
2    * ----------------------------------------------------------------------
3    * Copyright (C) 2009 Enrique Lara (k957@68k.org)
4    *
5    * TinLizard is free software; you can redistribute it and/or
6    * modify it under the terms of the GNU Lesser General Public License
7    * as published by the Free Software Foundation; either version 3.0
8    * of the License, or (at your option) any later version.
9    *
10   * TinLizard is distributed in the hope that it will be useful,
11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13   * GNU Lesser General Public License for more details.
14   *
15   * You should have received a copy of the GNU Lesser General Public License
16   * along with TinLizard. If not, see http://www.gnu.org/licenses/.
17   * ----------------------------------------------------------------------
18   */
19  package tinlizard.web;
20  
21  import tinlizard.annotation.security.RolesAllowed;
22  
23  import tinlizard.model.Codeline;
24  import tinlizard.model.Policy;
25  import tinlizard.model.Project;
26  import tinlizard.model.User;
27  import tinlizard.model.View;
28  
29  import tinlizard.util.Messages;
30  
31  import java.util.Collection;
32  import java.util.Date;
33  
34  import org.apache.log4j.Logger;
35  
36  import org.kohsuke.stapler.StaplerRequest;
37  import org.kohsuke.stapler.StaplerResponse;
38  import org.kohsuke.stapler.export.Exported;
39  
40  /***
41   * Decorate Project for Stapler.
42   */
43  public final class ProjectWeb extends ObjectWeb<Project> {
44      private static final Logger LOG = Logger.getLogger(ProjectWeb.class);
45      private final Project subject;
46  
47      public ProjectWeb(final Project p) {
48          if (p == null) {
49              throw new IllegalStateException(Messages.error_1012());
50          }
51  
52          this.subject = p;
53      }
54  
55      protected Class<Project> getManagedClass() {
56          return Project.class;
57      }
58  
59      protected Project getSubject() {
60          return this.subject;
61      }
62  
63      public Integer getId() {
64          return this.subject.getId();
65      }
66  
67      public String getName() {
68          return this.subject.getName();
69      }
70  
71      @Exported
72      public String getDescription() {
73          return this.subject.getDescription();
74      }
75  
76      @Exported
77      public Date getCreated() {
78          return subject.getCreated();
79      }
80  
81      @Exported
82      public String getCreatedBy() {
83          return subject.getCreatedBy();
84      }
85  
86      @Exported
87      public Date getLastModified() {
88          return subject.getLastModified();
89      }
90  
91      @Exported
92      public String getLastModifiedBy() {
93          return subject.getLastModifiedBy();
94      }
95  
96      @Exported
97      public String getScmConnection() {
98          Codeline codeline = subject.findMainLine();
99  
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 }