001/*
002 * This file is part of McIDAS-V
003 *
004 * Copyright 2007-2024
005 * Space Science and Engineering Center (SSEC)
006 * University of Wisconsin - Madison
007 * 1225 W. Dayton Street, Madison, WI 53706, USA
008 * https://www.ssec.wisc.edu/mcidas/
009 * 
010 * All Rights Reserved
011 * 
012 * McIDAS-V is built on Unidata's IDV and SSEC's VisAD libraries, and
013 * some McIDAS-V source code is based on IDV and VisAD source code.  
014 * 
015 * McIDAS-V is free software; you can redistribute it and/or modify
016 * it under the terms of the GNU Lesser Public License as published by
017 * the Free Software Foundation; either version 3 of the License, or
018 * (at your option) any later version.
019 * 
020 * McIDAS-V is distributed in the hope that it will be useful,
021 * but WITHOUT ANY WARRANTY; without even the implied warranty of
022 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
023 * GNU Lesser Public License for more details.
024 * 
025 * You should have received a copy of the GNU Lesser Public License
026 * along with this program.  If not, see https://www.gnu.org/licenses/.
027 */
028package edu.wisc.ssec.mcidasv.supportform;
029
030import static java.util.Objects.requireNonNull;
031
032import java.util.Properties;
033
034import edu.wisc.ssec.mcidasv.startupmanager.StartupManager;
035import ucar.unidata.util.LogUtil;
036
037import edu.wisc.ssec.mcidasv.Constants;
038import edu.wisc.ssec.mcidasv.McIDASV;
039import edu.wisc.ssec.mcidasv.util.SystemState;
040
041public class McvStateCollector implements StateCollector {
042    
043    /** Reference used to query McIDAS-V's application state. */
044    private final McIDASV mcv;
045    
046    /** Name of the attachment used for the system state bundle. */
047    private static final String BUNDLE = "bundle" + Constants.SUFFIX_MCV;
048    
049    /** Name of the attachment used for system properties. */
050    private static final String EXTRA = "mcv.properties";
051    
052    /**
053     * Builds a state collector that knows how to query McIDAS-V for specific
054     * information.
055     * 
056     * @param mcv The McIDAS-V reference that we'll interrogate.
057     */
058    public McvStateCollector(final McIDASV mcv) {
059        this.mcv = requireNonNull(mcv);
060    }
061    
062    /**
063     * What should the name of the bundled version of McIDAS-V's current state 
064     * be named?
065     * 
066     * @return Filename to use as an email attachment. Note that this file is
067     * created specifically for the support request and will not exist otherwise.
068     */
069    public String getBundleAttachmentName() {
070        return BUNDLE;
071    }
072    
073    /**
074     * What should the set of McIDAS-V system properties be named?
075     * 
076     * @return Filename to use as an email attachment. Again, this file does
077     * not actually exist outside of the support request.
078     */
079    public String getExtraAttachmentName() {
080        return EXTRA;
081    }
082    
083    /**
084     * Return full path to McV log file
085     */
086    public String getLogPath() {
087        return mcv.getUserFile("mcidasv.log");
088    }
089    
090    /**
091     * Return full path to user's startup preferences file.
092     */
093    public String getPrefsPath() {
094        return StartupManager.getInstance().getPlatform().getUserPrefs();
095    }
096    
097    /**
098     * Get the path to where the user's {@code RESOLV.SRV} file <b>should</b>
099     * be located.
100     *
101     * @return Path to {@code RESOLV.SRV}. Note: <b>the file may not exist!</b>
102     */
103    public String getResolvSrvPath() {
104        return mcv.getUserFile("RESOLV.SRV");
105    }
106    
107    /**
108     * Builds the McIDAS-V system properties and returns the results as a 
109     * nicely formatted {@code String}.
110     * 
111     * @return The McIDAS-V system properties in the following format: 
112     * {@code KEY=VALUE\n}. This is so we kinda-sorta conform to the standard
113     * {@link Properties} file format.
114     */
115    public String getContentsAsString() {
116        return SystemState.getStateAsString(mcv, true);
117    }
118    
119    /**
120     * The results of {@link #getContentsAsString()} as an array of {@code byte}s.
121     * This makes for a particularly easy way to attach to a {@code HTTP POST}.
122     */
123    public byte[] getContents() {
124        return getContentsAsString().getBytes();
125    }
126    
127    /**
128     * Whether or not this {@link StateCollector} allows for attaching current
129     * McIDAS-V state as a bundle.
130     */
131    public boolean canBundleState() {
132        return true;
133    }
134    
135    /**
136     * Current McIDAS-V state as an XML bundle named by 
137     * {@link #getBundleAttachmentName()}.
138     */
139    public byte[] getBundledState() {
140        String data = "";
141        try {
142            data = mcv.getPersistenceManager().getBundleXml(true);
143        } catch (Exception e) {
144            LogUtil.logException("Error saving state for support request", e);
145        }
146        return data.getBytes();
147    }
148    
149    public String toString() {
150        return String.format("[McvStateCollector@%x: canBundleState=%s, bundle=%s, extra=%s]", hashCode(), canBundleState(), getBundleAttachmentName(), getExtraAttachmentName());
151    }
152}