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.Dependency;
25  import tinlizard.model.Policy;
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.commons.lang.StringUtils;
35  
36  import org.kohsuke.stapler.StaplerRequest;
37  import org.kohsuke.stapler.StaplerResponse;
38  import org.kohsuke.stapler.export.Exported;
39  
40  /***
41   * Decorate Codeline for Stapler.
42   */
43  public final class CodelineWeb extends ObjectWeb<Codeline> {
44      private Codeline subject;
45  
46      public CodelineWeb(final Codeline pb) {
47          if (pb == null) {
48              throw new IllegalStateException(Messages.error_1011());
49          }
50  
51          this.subject = pb;
52      }
53  
54      protected Codeline getSubject() {
55          return this.subject;
56      }
57  
58      public Integer getId() {
59          return this.subject.getId();
60      }
61  
62      public String getName() {
63          return this.subject.getName();
64      }
65  
66      @Exported
67      public String getScmConnection() {
68          return this.subject.getScmConnection();
69      }
70  
71      @Exported
72      public String getGroupId() {
73          return this.subject.getGroupId();
74      }
75  
76      @Exported
77      public String getArtifactId() {
78          return this.subject.getArtifactId();
79      }
80  
81      @Exported
82      public String getVersion() {
83          return this.subject.getVersion();
84      }
85  
86      @Exported
87      public ProjectWeb getProject() {
88          return new ProjectWeb(this.subject.getProject());
89      }
90  
91      @Exported
92      public UserWeb getOwner() {
93          return new UserWeb(subject.getOwner());
94      }
95  
96      @Exported
97      public PolicyWeb getPolicy() {
98          return new PolicyWeb(subject.getPolicy());
99      }
100 
101     public boolean isMainLine() {
102         return this.subject.isMainLine();
103     }
104 
105     @Exported
106     public Boolean getMainLine() {
107         return this.subject.getMainLine();
108     }
109 
110     @Exported
111     public String getDescription() {
112         return subject.getDescription();
113     }
114 
115     @Exported
116     public Date getCreated() {
117         return subject.getCreated();
118     }
119 
120     @Exported
121     public String getCreatedBy() {
122         return subject.getCreatedBy();
123     }
124 
125     @Exported
126     public Date getLastModified() {
127         return subject.getLastModified();
128     }
129 
130     @Exported
131     public String getLastModifiedBy() {
132         return subject.getLastModifiedBy();
133     }
134 
135     @Exported(visibility = 1)
136     public ViewWeb getView() {
137         return new ViewWeb(this.subject.getView());
138     }
139 
140     @Exported(visibility = 1)
141     public CodelineCollectionWeb getConsumers() {
142         Collection<Codeline> allCodelines = this.subject.findAllConsumers(true);
143 
144         if (allCodelines != null) {
145             return new CodelineCollectionWeb(allCodelines, Messages._Consumers());
146         } else {
147             return null;
148         }
149     }
150 
151     @Exported(name = "dependencys")
152     public DependencyCollectionWeb getDependencies() {
153         Collection<Dependency> dependencies = subject.findAllDependencies();
154 
155         if (dependencies != null) {
156             return new DependencyCollectionWeb(dependencies, Messages._Dependencies());
157         } else {
158             return null;
159         }
160     }
161 
162     public DependencyWeb getDependency(final String name) {
163         if (StringUtils.isNotBlank(name)) {
164             Collection<Dependency> dependencies = subject.findAllDependencies();
165 
166             if (dependencies != null) {
167                 for (Dependency dependency : dependencies) {
168                     if (name.equals(dependency.getName())) {
169                         return new DependencyWeb(dependency);
170                     }
171                 }
172             }
173         }
174 
175         return null;
176     }
177 
178     @RolesAllowed(RoleNames.USER)
179     public void doDelete(final StaplerRequest request, final StaplerResponse response) throws Exception {
180         String projectName = subject.getProject().getName();
181         subject.delete();
182         response.sendRedirect(request.getContextPath() + "/project/" + projectName);
183     }
184 
185     //XXX prevent updating id?
186     @RolesAllowed(RoleNames.USER)
187     public void doUpdate(final StaplerRequest request, final StaplerResponse response) throws Exception {
188         request.bindParameters(subject, "codeline.");
189 
190         View view = new View();
191         request.bindParameters(view, "view.");
192 
193         if (StringUtils.isNotBlank(view.getName())) {
194             view = View.findByName(view.getName());
195 
196             if (view != null) {
197                 subject.setView(view);
198             }
199         }
200 
201         User user = new User();
202         request.bindParameters(user, "owner.");
203 
204         if (StringUtils.isNotBlank(user.getName())) {
205             user = User.getUserByName(user.getName(), true);
206 
207             if (user != null) {
208                 subject.setOwner(user);
209             }
210         }
211 
212         Policy policy = new Policy();
213         request.bindParameters(policy, "policy.");
214 
215         if (StringUtils.isNotBlank(policy.getName())) {
216             policy = Policy.findByName(policy.getName());
217 
218             if (policy != null) {
219                 subject.setPolicy(policy);
220             }
221         }
222 
223         subject.update();
224         gotoMyIndex(request, response);
225     }
226 
227     @RolesAllowed(RoleNames.USER)
228     public void doRefresh(final StaplerRequest request, final StaplerResponse response) throws Exception {
229         subject.refresh();
230         gotoMyIndex(request, response);
231     }
232 }