001/*
002 * JDrupes Builder
003 * Copyright (C) 2026 Michael N. Lipp
004 * 
005 * This program is free software: you can redistribute it and/or modify
006 * it under the terms of the GNU Affero General Public License as
007 * published by the Free Software Foundation, either version 3 of the
008 * License, or (at your option) any later version.
009 *
010 * This program 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 Affero General Public License for more details.
014 *
015 * You should have received a copy of the GNU Affero General Public License
016 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
017 */
018
019package org.jdrupes.builder.bnd;
020
021import java.nio.file.Path;
022import java.util.Optional;
023import org.jdrupes.builder.api.Project;
024import org.jdrupes.builder.api.ResourceType;
025import org.jdrupes.builder.core.ResourceObject;
026
027/// Default implementation of a baseline result.
028///
029public class DefaultBndBaselineEvaluation extends ResourceObject
030        implements BndBaselineEvaluation {
031
032    private final Project project;
033    private final Path bundlePath;
034    private boolean missing;
035    private boolean mismatch;
036    private boolean faulty;
037    private String reason;
038    private Path reportLocation;
039
040    /// Initializes a new default bnd baseline result.
041    ///
042    /// @param type the type
043    /// @param project the project
044    /// @param bundlePath the bundle path
045    ///
046    protected DefaultBndBaselineEvaluation(
047            ResourceType<?> type, Project project, Path bundlePath) {
048        super(type);
049        this.project = project;
050        this.bundlePath = bundlePath;
051    }
052
053    @Override
054    public DefaultBndBaselineEvaluation name(String name) {
055        super.name(name);
056        return this;
057    }
058
059    @Override
060    public Project project() {
061        return project;
062    }
063
064    @Override
065    public Path bundlePath() {
066        return bundlePath;
067    }
068
069    @Override
070    public boolean mismatch() {
071        return mismatch;
072    }
073
074    @Override
075    public boolean isFaulty() {
076        return faulty;
077    }
078
079    @Override
080    public DefaultBndBaselineEvaluation setFaulty() {
081        faulty = true;
082        return this;
083    }
084
085    /// Sets the reason for a mismatch.
086    ///
087    /// @param reason the reason
088    /// @return the default bnd baseline evaluation
089    ///
090    public DefaultBndBaselineEvaluation withReason(String reason) {
091        this.reason = reason;
092        return this;
093    }
094
095    @Override
096    public Optional<String> reason() {
097        return Optional.ofNullable(reason);
098    }
099
100    @Override
101    public Path reportLocation() {
102        return reportLocation;
103    }
104
105    /// Sets the report location.
106    ///
107    /// @param reportLocation the report location
108    /// @return the default bnd baseline evaluation
109    ///
110    public DefaultBndBaselineEvaluation
111            withReportLocation(Path reportLocation) {
112        this.reportLocation = reportLocation;
113        return this;
114    }
115
116    /// Sets the baseline artifact missing flag. Also sets the faulty flag
117    /// for consistency.
118    ///
119    /// @return the default bnd baseline evaluation
120    ///
121    public DefaultBndBaselineEvaluation withBaselineArtifactMissing() {
122        missing = true;
123        faulty = true;
124        return this;
125    }
126
127    @Override
128    public boolean baselineArtifactMissing() {
129        return missing;
130    }
131
132    @Override
133    public String toString() {
134        if (missing) {
135            return String.format("BndBaselineEvaluation:%s impossible due"
136                + " to missing baseline artifact", name().orElse("(unknown)"),
137                project());
138        }
139        if (mismatch) {
140            return String.format("BndBaselineEvaluation:%s failed: %s, see %s",
141                name().get(), reason().orElse("(unknown)"),
142                project().rootProject().relativize(reportLocation()));
143        }
144        return String.format("BndBaselineEvaluation:%s succeeded, see %s",
145            name().get(), project().rootProject().relativize(reportLocation()));
146    }
147
148}