001 /* 002 * Sonar, open source software quality management tool. 003 * Copyright (C) 2009 SonarSource SA 004 * mailto:contact AT sonarsource DOT com 005 * 006 * Sonar is free software; you can redistribute it and/or 007 * modify it under the terms of the GNU Lesser General Public 008 * License as published by the Free Software Foundation; either 009 * version 3 of the License, or (at your option) any later version. 010 * 011 * Sonar is distributed in the hope that it will be useful, 012 * but WITHOUT ANY WARRANTY; without even the implied warranty of 013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 014 * Lesser General Public License for more details. 015 * 016 * You should have received a copy of the GNU Lesser General Public 017 * License along with Sonar; if not, write to the Free Software 018 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 019 */ 020 package org.sonar.batch; 021 022 import org.sonar.api.batch.DecoratorContext; 023 import org.sonar.api.batch.Event; 024 import org.sonar.api.database.DatabaseSession; 025 import org.sonar.api.design.Dependency; 026 import org.sonar.api.measures.Measure; 027 import org.sonar.api.measures.MeasuresFilter; 028 import org.sonar.api.measures.MeasuresFilters; 029 import org.sonar.api.measures.Metric; 030 import org.sonar.api.resources.Project; 031 import org.sonar.api.resources.Resource; 032 import org.sonar.api.rules.Violation; 033 import org.sonar.batch.indexer.Bucket; 034 import org.sonar.batch.indexer.DefaultSonarIndex; 035 036 import java.util.*; 037 038 public class DefaultDecoratorContext implements DecoratorContext { 039 040 private DatabaseSession session; 041 private DefaultSonarIndex index; 042 private Resource resource; 043 private boolean readOnly = false; 044 045 private List<DecoratorContext> childrenContexts; 046 private List<Violation> violations; 047 private ViolationsDao violationsDao; 048 049 public DefaultDecoratorContext(Resource resource, 050 DefaultSonarIndex index, 051 List<DecoratorContext> childrenContexts, 052 DatabaseSession session, 053 ViolationsDao violationsDao) { 054 this.index = index; 055 this.session = session; 056 this.resource = resource; 057 this.childrenContexts = childrenContexts; 058 this.violationsDao = violationsDao; 059 } 060 061 public DefaultDecoratorContext setReadOnly(boolean b) { 062 readOnly = b; 063 violations = null; 064 childrenContexts = null; 065 return this; 066 } 067 068 public Project getProject() { 069 return index.getProject(); 070 } 071 072 public List<DecoratorContext> getChildren() { 073 checkReadOnly("getModules"); 074 return childrenContexts; 075 } 076 077 private void checkReadOnly(String methodName) { 078 if (readOnly) { 079 throw new IllegalStateException("Method DecoratorContext." + methodName + "() can not be executed on children."); 080 } 081 } 082 083 public <M> M getMeasures(MeasuresFilter<M> filter) { 084 return index.getMeasures(resource, filter); 085 } 086 087 public Measure getMeasure(Metric metric) { 088 return index.getMeasures(resource, MeasuresFilters.metric(metric)); 089 } 090 091 public Collection<Measure> getChildrenMeasures(MeasuresFilter filter) { 092 List<Measure> result = new ArrayList<Measure>(); 093 for (DecoratorContext childContext : childrenContexts) { 094 Object childResult = childContext.getMeasures(filter); 095 if (childResult instanceof Collection) { 096 result.addAll((Collection) childResult); 097 } else { 098 result.add((Measure) childResult); 099 } 100 } 101 return result; 102 } 103 104 public Collection<Measure> getChildrenMeasures(Metric metric) { 105 return getChildrenMeasures(MeasuresFilters.metric(metric)); 106 } 107 108 public Resource getResource() { 109 return resource; 110 } 111 112 public DecoratorContext saveMeasure(Measure measure) { 113 checkReadOnly("saveMeasure"); 114 index.saveMeasure(resource, measure); 115 return this; 116 } 117 118 public DecoratorContext saveMeasure(Metric metric, Double value) { 119 checkReadOnly("saveMeasure"); 120 index.saveMeasure(resource, new Measure(metric, value)); 121 return this; 122 } 123 124 125 public List<Violation> getViolations() { 126 if (violations == null) { 127 Bucket bucket = index.getBucket(resource); 128 if (bucket != null && bucket.getSnapshotId()!=null) { 129 violations = violationsDao.getViolations(resource, bucket.getSnapshotId()); 130 } 131 } 132 return violations; 133 } 134 135 public Dependency saveDependency(Dependency dependency) { 136 checkReadOnly("saveDependency"); 137 return index.saveDependency(dependency); 138 } 139 140 public Set<Dependency> getDependencies() { 141 return index.getDependencies(); 142 } 143 144 public Collection<Dependency> getIncomingDependencies() { 145 return index.getIncomingEdges(resource); 146 } 147 148 public Collection<Dependency> getOutgoingDependencies() { 149 return index.getOutgoingEdges(resource); 150 } 151 152 protected DatabaseSession getSession() { 153 return session; 154 } 155 156 public List<Event> getEvents() { 157 return index.getEvents(resource); 158 } 159 160 public Event createEvent(String name, String description, String category, Date date) { 161 return index.createEvent(resource, name, description, category, date); 162 } 163 164 public void deleteEvent(Event event) { 165 index.deleteEvent(event); 166 } 167 168 public DefaultDecoratorContext saveViolation(Violation violation) { 169 if (violation.getResource()==null) { 170 violation.setResource(resource); 171 } 172 index.addViolation(violation); 173 return this; 174 } 175 }