X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;f=bundles%2Forg.simantics.issues.common%2Fsrc%2Forg%2Fsimantics%2Fissues%2Fcommon%2FMaxIssueSeveritySingle.java;h=e8ed917aad52182f701f1060fb5a289ce49f1325;hb=refs%2Fchanges%2F78%2F278%2F3;hp=008848250ad87f587eb4cd7eeb7f26ca0d4015a4;hpb=969bd23cab98a79ca9101af33334000879fb60c5;p=simantics%2Fplatform.git diff --git a/bundles/org.simantics.issues.common/src/org/simantics/issues/common/MaxIssueSeveritySingle.java b/bundles/org.simantics.issues.common/src/org/simantics/issues/common/MaxIssueSeveritySingle.java index 008848250..e8ed917aa 100644 --- a/bundles/org.simantics.issues.common/src/org/simantics/issues/common/MaxIssueSeveritySingle.java +++ b/bundles/org.simantics.issues.common/src/org/simantics/issues/common/MaxIssueSeveritySingle.java @@ -1,183 +1,183 @@ -/******************************************************************************* - * Copyright (c) 2007, 2011 Association for Decentralized Information Management in - * Industry THTH ry. - * All rights reserved. This program and the accompanying materials - * are made available under the terms of the Eclipse Public License v1.0 - * which accompanies this distribution, and is available at - * http://www.eclipse.org/legal/epl-v10.html - * - * Contributors: - * VTT Technical Research Centre of Finland - initial API and implementation - *******************************************************************************/ -package org.simantics.issues.common; - -import java.util.concurrent.atomic.AtomicReference; - -import org.simantics.db.AsyncReadGraph; -import org.simantics.db.Resource; -import org.simantics.db.common.request.ResourceAsyncRead; -import org.simantics.db.procedure.AsyncMultiProcedure; -import org.simantics.db.procedure.AsyncProcedure; -import org.simantics.issues.Severity; -import org.simantics.issues.ontology.IssueResource; - -/** - * @author Tuukka Lehtonen - */ -public class MaxIssueSeveritySingle extends ResourceAsyncRead{ - - public MaxIssueSeveritySingle(Resource resource) { - super(resource); - } - -// @Override -// public Severity perform(ReadGraph graph) throws DatabaseException { -// //System.out.println("severityFor: " + NameUtils.getSafeName(graph, resource)); -// IssueResource ISSUE = IssueResource.getInstance(graph); -// Severity maxSeverity = null; -// Collection issues = graph.getObjects(resource, ISSUE.IsIssueContextOf); -// for (Resource issue : issues) { -// boolean resolved = graph.hasStatement(issue, ISSUE.Resolved); -// if (resolved) -// continue; -// -// Resource severity = graph.getPossibleObject(issue, ISSUE.HasSeverity); -// if (severity != null) -// maxSeverity = Severity.moreSevere(maxSeverity, toSeverity(ISSUE, severity)); -// } -// -// //System.out.println("severityFor: " + NameUtils.getSafeName(graph, resource) + " : " + maxSeverity); -// return maxSeverity; -// } - - @Override - public void perform(AsyncReadGraph graph, final AsyncProcedure procedure) { - final IssueResource ISSUE = graph.getService(IssueResource.class); - //System.out.println(getClass().getSimpleName() + ": " + resource); - - graph.forEachObject(resource, ISSUE.Issue_HasContext_Inverse, new AsyncMultiProcedure() { - AtomicReference maxSeverity = new AtomicReference(); - @Override - public void execute(AsyncReadGraph graph, final Resource issue) { - - /* - * Compare severity of each related issue and find the maximum severity. - * The issues that are not resolved and have active issue source manager - * are taken into account. - */ - acceptIfNotResolved(graph, procedure, ISSUE, issue, maxSeverity); - } - @Override - public void finished(AsyncReadGraph graph) { - procedure.execute(graph, maxSeverity.get()); - } - @Override - public void exception(AsyncReadGraph graph, Throwable throwable) { - procedure.exception(graph, throwable); - } - }); - } - - /** - * Accept an issue for maximum severity comparison, if the issue has not been resolved - * - * @param graph AsyncReadGraph - * @param procedure AsyncProcedure - * @param issue Issue resource - * @param maxSeverity Current maximum severity - */ - private void acceptIfNotResolved(AsyncReadGraph graph, final AsyncProcedure procedure, final IssueResource ISSUE, final Resource issue, final AtomicReference maxSeverity) { - - graph.forHasStatement(issue, ISSUE.Resolved, new AsyncProcedure() { - @Override - public void execute(AsyncReadGraph graph, Boolean resolved) { - if (resolved) - return; - - acceptIfSourceIsActive(graph, procedure, ISSUE, issue, maxSeverity); - - } - @Override - public void exception(AsyncReadGraph graph, Throwable throwable) { - procedure.exception(graph, throwable); - } - }); - } - - /** - * Accept an issue for maximum severity comparison if the issue source is active - * @param graph AsyncReadGraph - * @param procedure AsyncProcedure - * @param issue Issue resource - * @param maxSeverity Current maximum severity - */ - private void acceptIfSourceIsActive(AsyncReadGraph graph, final AsyncProcedure procedure, final IssueResource ISSUE, final Resource issue, final AtomicReference maxSeverity) { - graph.forPossibleObject(issue, ISSUE.IssueSource_Manages_Inverse, new AsyncProcedure() { - @Override - public void execute(AsyncReadGraph graph, Resource issueSource) { - if (issueSource != null) { - graph.forPossibleRelatedValue(issueSource, ISSUE.IssueSource_active, new AsyncProcedure() { - @Override - public void execute(AsyncReadGraph graph, Boolean active) { - if (!Boolean.FALSE.equals(active)) { - compareSeverity(graph, procedure, ISSUE, issue, maxSeverity); - } - } - @Override - public void exception(AsyncReadGraph graph, Throwable throwable) { - procedure.exception(graph, throwable); - } - }); - } else { - // Not managed by an issue source => user issue - compareSeverity(graph, procedure, ISSUE, issue, maxSeverity); - } - } - @Override - public void exception(AsyncReadGraph graph, Throwable throwable) { - procedure.exception(graph, throwable); - } - }); - } - - /** - * Compare issue's severity for current maximum severity. Update the maximum severity - * if issue is more severe. - * @param graph AsyncReadGraph - * @param procedure AsyncProcedure - * @param issue Issue resource - * @param maxSeverity Current maximum severity - */ - private void compareSeverity(AsyncReadGraph graph, final AsyncProcedure procedure, final IssueResource ISSUE, final Resource issue, final AtomicReference maxSeverity) { - graph.forPossibleObject(issue, ISSUE.Issue_HasSeverity, new AsyncProcedure() { - @Override - public void execute(AsyncReadGraph graph, Resource severity) { - if (severity != null) { - synchronized (maxSeverity) { - maxSeverity.set(Severity.moreSevere(maxSeverity.get(), toSeverity(ISSUE, severity))); - } - } - } - @Override - public void exception(AsyncReadGraph graph, Throwable throwable) { - procedure.exception(graph, throwable); - } - }); - } - - - private static Severity toSeverity(IssueResource ISSUE, Resource severity) { - if (ISSUE.Severity_Fatal.equals(severity)) - return Severity.FATAL; - if (ISSUE.Severity_Error.equals(severity)) - return Severity.ERROR; - if (ISSUE.Severity_Warning.equals(severity)) - return Severity.WARNING; - if (ISSUE.Severity_Info.equals(severity)) - return Severity.INFO; - if (ISSUE.Severity_Note.equals(severity)) - return Severity.NOTE; - return null; - } - -} +/******************************************************************************* + * Copyright (c) 2007, 2011 Association for Decentralized Information Management in + * Industry THTH ry. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * VTT Technical Research Centre of Finland - initial API and implementation + *******************************************************************************/ +package org.simantics.issues.common; + +import java.util.concurrent.atomic.AtomicReference; + +import org.simantics.db.AsyncReadGraph; +import org.simantics.db.Resource; +import org.simantics.db.common.request.ResourceAsyncRead; +import org.simantics.db.procedure.AsyncMultiProcedure; +import org.simantics.db.procedure.AsyncProcedure; +import org.simantics.issues.Severity; +import org.simantics.issues.ontology.IssueResource; + +/** + * @author Tuukka Lehtonen + */ +public class MaxIssueSeveritySingle extends ResourceAsyncRead{ + + public MaxIssueSeveritySingle(Resource resource) { + super(resource); + } + +// @Override +// public Severity perform(ReadGraph graph) throws DatabaseException { +// //System.out.println("severityFor: " + NameUtils.getSafeName(graph, resource)); +// IssueResource ISSUE = IssueResource.getInstance(graph); +// Severity maxSeverity = null; +// Collection issues = graph.getObjects(resource, ISSUE.IsIssueContextOf); +// for (Resource issue : issues) { +// boolean resolved = graph.hasStatement(issue, ISSUE.Resolved); +// if (resolved) +// continue; +// +// Resource severity = graph.getPossibleObject(issue, ISSUE.HasSeverity); +// if (severity != null) +// maxSeverity = Severity.moreSevere(maxSeverity, toSeverity(ISSUE, severity)); +// } +// +// //System.out.println("severityFor: " + NameUtils.getSafeName(graph, resource) + " : " + maxSeverity); +// return maxSeverity; +// } + + @Override + public void perform(AsyncReadGraph graph, final AsyncProcedure procedure) { + final IssueResource ISSUE = graph.getService(IssueResource.class); + //System.out.println(getClass().getSimpleName() + ": " + resource); + + graph.forEachObject(resource, ISSUE.Issue_HasContext_Inverse, new AsyncMultiProcedure() { + AtomicReference maxSeverity = new AtomicReference(); + @Override + public void execute(AsyncReadGraph graph, final Resource issue) { + + /* + * Compare severity of each related issue and find the maximum severity. + * The issues that are not resolved and have active issue source manager + * are taken into account. + */ + acceptIfNotResolved(graph, procedure, ISSUE, issue, maxSeverity); + } + @Override + public void finished(AsyncReadGraph graph) { + procedure.execute(graph, maxSeverity.get()); + } + @Override + public void exception(AsyncReadGraph graph, Throwable throwable) { + procedure.exception(graph, throwable); + } + }); + } + + /** + * Accept an issue for maximum severity comparison, if the issue has not been resolved + * + * @param graph AsyncReadGraph + * @param procedure AsyncProcedure + * @param issue Issue resource + * @param maxSeverity Current maximum severity + */ + private void acceptIfNotResolved(AsyncReadGraph graph, final AsyncProcedure procedure, final IssueResource ISSUE, final Resource issue, final AtomicReference maxSeverity) { + + graph.forHasStatement(issue, ISSUE.Resolved, new AsyncProcedure() { + @Override + public void execute(AsyncReadGraph graph, Boolean resolved) { + if (resolved) + return; + + acceptIfSourceIsActive(graph, procedure, ISSUE, issue, maxSeverity); + + } + @Override + public void exception(AsyncReadGraph graph, Throwable throwable) { + procedure.exception(graph, throwable); + } + }); + } + + /** + * Accept an issue for maximum severity comparison if the issue source is active + * @param graph AsyncReadGraph + * @param procedure AsyncProcedure + * @param issue Issue resource + * @param maxSeverity Current maximum severity + */ + private void acceptIfSourceIsActive(AsyncReadGraph graph, final AsyncProcedure procedure, final IssueResource ISSUE, final Resource issue, final AtomicReference maxSeverity) { + graph.forPossibleObject(issue, ISSUE.IssueSource_Manages_Inverse, new AsyncProcedure() { + @Override + public void execute(AsyncReadGraph graph, Resource issueSource) { + if (issueSource != null) { + graph.forPossibleRelatedValue(issueSource, ISSUE.IssueSource_active, new AsyncProcedure() { + @Override + public void execute(AsyncReadGraph graph, Boolean active) { + if (!Boolean.FALSE.equals(active)) { + compareSeverity(graph, procedure, ISSUE, issue, maxSeverity); + } + } + @Override + public void exception(AsyncReadGraph graph, Throwable throwable) { + procedure.exception(graph, throwable); + } + }); + } else { + // Not managed by an issue source => user issue + compareSeverity(graph, procedure, ISSUE, issue, maxSeverity); + } + } + @Override + public void exception(AsyncReadGraph graph, Throwable throwable) { + procedure.exception(graph, throwable); + } + }); + } + + /** + * Compare issue's severity for current maximum severity. Update the maximum severity + * if issue is more severe. + * @param graph AsyncReadGraph + * @param procedure AsyncProcedure + * @param issue Issue resource + * @param maxSeverity Current maximum severity + */ + private void compareSeverity(AsyncReadGraph graph, final AsyncProcedure procedure, final IssueResource ISSUE, final Resource issue, final AtomicReference maxSeverity) { + graph.forPossibleObject(issue, ISSUE.Issue_HasSeverity, new AsyncProcedure() { + @Override + public void execute(AsyncReadGraph graph, Resource severity) { + if (severity != null) { + synchronized (maxSeverity) { + maxSeverity.set(Severity.moreSevere(maxSeverity.get(), toSeverity(ISSUE, severity))); + } + } + } + @Override + public void exception(AsyncReadGraph graph, Throwable throwable) { + procedure.exception(graph, throwable); + } + }); + } + + + private static Severity toSeverity(IssueResource ISSUE, Resource severity) { + if (ISSUE.Severity_Fatal.equals(severity)) + return Severity.FATAL; + if (ISSUE.Severity_Error.equals(severity)) + return Severity.ERROR; + if (ISSUE.Severity_Warning.equals(severity)) + return Severity.WARNING; + if (ISSUE.Severity_Info.equals(severity)) + return Severity.INFO; + if (ISSUE.Severity_Note.equals(severity)) + return Severity.NOTE; + return null; + } + +}