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.Dependency;
025    import tinlizard.model.Policy;
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.commons.lang.StringUtils;
035    
036    import org.kohsuke.stapler.StaplerRequest;
037    import org.kohsuke.stapler.StaplerResponse;
038    import org.kohsuke.stapler.export.Exported;
039    
040    /**
041     * Decorate Codeline for Stapler.
042     */
043    public final class CodelineWeb extends ObjectWeb<Codeline> {
044        private Codeline subject;
045    
046        public CodelineWeb(final Codeline pb) {
047            if (pb == null) {
048                throw new IllegalStateException(Messages.error_1011());
049            }
050    
051            this.subject = pb;
052        }
053    
054        protected Codeline getSubject() {
055            return this.subject;
056        }
057    
058        public Integer getId() {
059            return this.subject.getId();
060        }
061    
062        public String getName() {
063            return this.subject.getName();
064        }
065    
066        @Exported
067        public String getScmConnection() {
068            return this.subject.getScmConnection();
069        }
070    
071        @Exported
072        public String getGroupId() {
073            return this.subject.getGroupId();
074        }
075    
076        @Exported
077        public String getArtifactId() {
078            return this.subject.getArtifactId();
079        }
080    
081        @Exported
082        public String getVersion() {
083            return this.subject.getVersion();
084        }
085    
086        @Exported
087        public ProjectWeb getProject() {
088            return new ProjectWeb(this.subject.getProject());
089        }
090    
091        @Exported
092        public UserWeb getOwner() {
093            return new UserWeb(subject.getOwner());
094        }
095    
096        @Exported
097        public PolicyWeb getPolicy() {
098            return new PolicyWeb(subject.getPolicy());
099        }
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    }