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.model.Codeline;
022    import tinlizard.model.Dependency;
023    
024    import tinlizard.util.Messages;
025    
026    import java.util.Collection;
027    import java.util.Date;
028    
029    import org.codehaus.plexus.util.StringUtils;
030    
031    import org.kohsuke.stapler.StaplerRequest;
032    import org.kohsuke.stapler.StaplerResponse;
033    import org.kohsuke.stapler.export.Exported;
034    
035    /**
036     * Decorate Dependency for Stapler.
037     */
038    public class DependencyWeb extends ObjectWeb<Dependency> {
039        protected final Dependency subject;
040    
041        public DependencyWeb(final Dependency c) {
042            if (c == null) {
043                throw new IllegalStateException(Messages.error_1018());
044            }
045    
046            this.subject = c;
047        }
048    
049        protected final Dependency getSubject() {
050            return this.subject;
051        }
052    
053        public final Integer getId() {
054            return subject.getId();
055        }
056    
057        public String getName() {
058            return getVersion();
059        }
060    
061        @Exported
062        public String getGroupId() {
063            return subject.getGroupId();
064        }
065    
066        @Exported
067        public String getArtifactId() {
068            return subject.getArtifactId();
069        }
070    
071        @Exported
072        public String getVersion() {
073            return subject.getVersion();
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 final CodelineCollectionWeb getCodelines() {
098            Collection<Codeline> codelines = this.subject.findAllDefiningDependency(true);
099    
100            if (codelines != null) {
101                return new CodelineCollectionWeb(codelines, Messages._Codelines());
102            } else {
103                return null;
104            }
105        }
106    
107        @Exported
108        public final CodelineCollectionWeb getCodelinesAnyVersion() {
109            Collection<Codeline> codelines = this.subject.findAllDefiningDependency(false);
110    
111            if (codelines != null) {
112                return new CodelineCollectionWeb(codelines, Messages._Dependency_CodelinesAnyVersion());
113            } else {
114                return null;
115            }
116        }
117    
118        @Exported
119        public final CodelineCollectionWeb getConsumers() {
120            Collection<Codeline> codelines = this.subject.findAllConsumers(true);
121    
122            if (codelines != null) {
123                return new CodelineCollectionWeb(codelines, Messages._Dependency_ConsumersAnyVersion());
124            } else {
125                return null;
126            }
127        }
128    
129        @Exported
130        public final CodelineCollectionWeb getConsumersAnyVersion() {
131            Collection<Codeline> codelines = this.subject.findAllConsumers(false);
132    
133            if (codelines != null) {
134                return new CodelineCollectionWeb(codelines, Messages._Dependency_ConsumersAnyVersion());
135            } else {
136                return null;
137            }
138        }
139    
140        public Object getDynamic(final String token, final StaplerRequest req, final StaplerResponse rsp) {
141            if (StringUtils.isNotBlank(token)) {
142                if (StringUtils.isBlank(subject.getArtifactId())) {
143                    Dependency dependency = new Dependency();
144                    dependency.setGroupId(subject.getGroupId());
145                    dependency.setArtifactId(token);
146    
147                    return new DependencyWeb(dependency);
148                } else if (StringUtils.isBlank(subject.getVersion())) {
149                    Dependency dependency = new Dependency();
150                    dependency.setGroupId(subject.getGroupId());
151                    dependency.setArtifactId(subject.getArtifactId());
152                    dependency.setVersion(token);
153    
154                    return new DependencyWeb(dependency);
155                }
156            }
157    
158            return null;
159        }
160    }