Merge "Omaha #4834 fix creating new python files in localization perspective" into omaha_16.2.2

Former-commit-id: a762710e651937513a51b0f61ffb82d0d8c5dac7
This commit is contained in:
Nate Jensen 2016-01-19 16:54:10 -06:00 committed by Gerrit Code Review
commit 14a9e61d44
18 changed files with 169 additions and 1002 deletions

View file

@ -17,16 +17,13 @@
* See the AWIPS II Master Rights File ("Master Rights File.pdf") for
* further licensing information.
**/
/**
*
*/
package com.raytheon.viz.gfe.core.script;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.Properties;
import java.util.regex.Pattern;
import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
@ -40,8 +37,8 @@ import com.raytheon.uf.common.localization.LocalizationContext.LocalizationLevel
import com.raytheon.uf.common.localization.LocalizationContext.LocalizationType;
import com.raytheon.uf.common.localization.LocalizationFile;
import com.raytheon.uf.common.localization.PathManagerFactory;
import com.raytheon.uf.common.localization.SaveableOutputStream;
import com.raytheon.uf.common.python.PythonFileFilter;
import com.raytheon.uf.common.util.FileUtil;
import com.raytheon.uf.viz.core.localization.LocalizationManager;
import com.raytheon.viz.gfe.Activator;
import com.raytheon.viz.gfe.GFEException;
@ -56,83 +53,31 @@ import com.raytheon.viz.gfe.smarttool.SmartToolConstants;
*
* Date Ticket# Engineer Description
* ------------ ---------- ----------- --------------------------
* ??? wldougher Initial creation
* Sep 09, 2013 #2033 dgilling Use new templates directory.
* Jan 18, 2016 4834 njensen Fix creating files, deleted dead code
*
* </pre>
*
* @author wldougher
*
*/
public abstract class AbstractScriptUtil implements IScriptUtil {
public abstract class AbstractScriptUtil implements PythonFileTemplate {
protected IPathManager pathManager;
/**
* Constructor.
*/
public AbstractScriptUtil() {
protected AbstractScriptUtil() {
pathManager = PathManagerFactory.getPathManager();
}
/**
* @see com.raytheon.viz.gfe.core.script.IScriptUtil#copy(java.lang.String,
* java.lang.String,
* com.raytheon.uf.common.localization.LocalizationContext.
* LocalizationLevel,
* com.raytheon.viz.gfe.core.script.IScriptUtil.Overwrite)
*/
@Override
public LocalizationFile copy(String source, String dest,
LocalizationLevel toLevel, Overwrite mode) throws GFEException {
LocalizationFile src = find(source, null);
LocalizationFile dst = null;
String scriptType = getScriptType();
if (src == null || !src.exists()) {
throw new GFEException(String.format("%s %s does not exist.",
scriptType, source));
} else {
LocalizationLevel srcLevel = src.getContext()
.getLocalizationLevel();
if (source.equals(dest) && srcLevel.equals(toLevel)) {
String msg = String.format(
"Attempt to copy %s %s at %s level to itself.",
scriptType, source, srcLevel);
throw new GFEException(msg);
} else {
IPathManager pathManager = PathManagerFactory.getPathManager();
LocalizationContext ctx = pathManager.getContext(
LocalizationType.CAVE_STATIC, toLevel);
String destPath = getScriptTypePathPrefix() + File.separator
+ scripted(dest);
dst = pathManager.getLocalizationFile(ctx, destPath);
if (mode == Overwrite.SAFE && dst.exists()) {
dst = null;
} else {
try {
FileUtil.copyFile(src.getFile(), dst.getFile());
dst.save();
} catch (Exception e) {
throw new GFEException(String.format(
"Error copying %s %s to %s.", scriptType,
source, dest), e);
}
}
}
}
return dst;
}
/**
* @see com.raytheon.viz.gfe.core.script.IScriptUtil#createNew(java.lang.
* String,
* com.raytheon.uf.common.localization.LocalizationContext.LocalizationLevel
* , com.raytheon.viz.gfe.core.script.IScriptUtil.Overwrite)
*/
@Override
public LocalizationFile createNew(String script, LocalizationLevel level,
Overwrite mode) throws GFEException {
String scriptFName;
scriptFName = scripted(script);
scriptFName = normalize(script);
String path = getScriptTypePathPrefix() + File.separator + scriptFName;
LocalizationContext context = pathManager.getContext(
LocalizationType.CAVE_STATIC, level);
@ -146,19 +91,6 @@ public abstract class AbstractScriptUtil implements IScriptUtil {
throw new GFEException(
"Attempt to create existing file in SAFE mode");
} else {
File outFile = locFile.getFile();
File parentDir = outFile.getParentFile();
if (!parentDir.exists()) {
parentDir.mkdirs();
}
FileWriter fileWriter = null;
try {
fileWriter = new FileWriter(outFile);
} catch (IOException e) {
throw new GFEException("Error creating file writer for "
+ getScriptType() + " " + script, e);
}
// Set up the velocity context and template
initVelocity();
VelocityContext vctx = new VelocityContext();
@ -168,98 +100,28 @@ public abstract class AbstractScriptUtil implements IScriptUtil {
Template template = getVelocityTemplate();
// Generate the local file.
boolean throwing = false;
try {
template.merge(vctx, fileWriter);
try (SaveableOutputStream sos = locFile.openOutputStream();
OutputStreamWriter writer = new OutputStreamWriter(sos)) {
template.merge(vctx, writer);
writer.close();
sos.save();
// refresh the reference in memory
locFile = pathManager.getLocalizationFile(context, path);
} catch (Exception e) {
throwing = true;
throw new GFEException("Error in velocity merge for "
+ getScriptType() + " " + script, e);
} finally {
try {
fileWriter.close();
} catch (IOException e) {
if (!throwing) {
throw new GFEException("Error closing file writer for "
+ getScriptType() + " " + script, e);
}
}
}
// Propagate local file to the server.
try {
locFile.save();
} catch (Exception e) {
throw new GFEException("Error saving " + getScriptType() + " "
+ script, e);
throw new GFEException(
"Error creating new file " + scriptFName, e);
}
}
return locFile;
}
/**
* @see com.raytheon.viz.gfe.core.script.IScriptUtil#delete(java.lang.String,
* com
* .raytheon.uf.common.localization.LocalizationContext.LocalizationLevel)
*/
@Override
public void delete(String name, LocalizationLevel level)
throws GFEException {
String srcPath = scripted(name);
String pycPath = srcPath.replaceAll(Pattern.quote(".py") + "$", ".pyc");
String pyoPath = srcPath.replaceAll(Pattern.quote(".py") + "$", ".pyo");
LocalizationLevel fileLevel = LocalizationLevel.BASE;
LocalizationLevel pycLevel = LocalizationLevel.BASE;
LocalizationLevel pyoLevel = LocalizationLevel.BASE;
LocalizationFile file = find(srcPath, level);
LocalizationFile pycFile = find(pycPath, level);
LocalizationFile pyoFile = find(pyoPath, level);
// If level was null we may have source and bytecode files at multiple
// levels. Find the highest localization level.
LocalizationLevel highest = LocalizationLevel.BASE;
if (file != null) {
fileLevel = file.getContext().getLocalizationLevel();
highest = fileLevel;
}
if (pycFile != null) {
pycLevel = pycFile.getContext().getLocalizationLevel();
highest = pycLevel.compareTo(highest) > 0 ? pycLevel : highest;
}
if (pyoFile != null) {
pyoLevel = pyoFile.getContext().getLocalizationLevel();
highest = pyoLevel.compareTo(highest) > 0 ? pyoLevel : highest;
}
// Don't delete files less localized than highest.
if (fileLevel.compareTo(highest) < 0) {
file = null;
}
if (pycLevel.compareTo(highest) < 0) {
pycFile = null;
}
if (pyoLevel.compareTo(highest) < 0) {
pyoFile = null;
}
innerDelete(file, srcPath, fileLevel);
innerDelete(pycFile, pycPath, pycLevel);
innerDelete(pyoFile, pyoPath, pyoLevel);
}
/**
* @see com.raytheon.viz.gfe.core.script.IScriptUtil#find(java.lang.String,
* com
* .raytheon.uf.common.localization.LocalizationContext.LocalizationLevel)
*/
@Override
public LocalizationFile find(String name, LocalizationLevel level)
throws GFEException {
LocalizationFile result = null;
String path = getScriptTypePathPrefix() + File.separator + name;
path = scripted(path);
path = normalize(path);
if (level == null) {
result = pathManager.getStaticLocalizationFile(path);
} else {
@ -273,21 +135,6 @@ public abstract class AbstractScriptUtil implements IScriptUtil {
return result;
}
/**
* @see com.raytheon.viz.gfe.core.script.IScriptUtil#getScriptType()
*/
@Override
abstract public String getScriptType();
/*
* (non-Javadoc)
*
* @see
* com.raytheon.viz.gfe.core.script.IScriptUtil#getScriptTypePathPrefix()
*/
@Override
abstract public String getScriptTypePathPrefix();
/**
* Get the velocity template. This mostly serves to convert the many
* exception types thrown by Velocity.getTemplate() to a GFEException.
@ -341,54 +188,6 @@ public abstract class AbstractScriptUtil implements IScriptUtil {
}
}
/**
* Do the actual deletion of a localization file. If file is null, no action
* is taken. Otherwise, file is deleted locally, and on the server, if it
* exists there.
*
* @param file
* The file to delete. May be null.
* @param name
* The name of file, for error reporting.
* @param level
* The localization level of file
* @throws GFEException
*/
protected void innerDelete(LocalizationFile file, String name,
LocalizationLevel level) throws GFEException {
if (file == null) {
; // ignore deletion of non-existent file
} else if (file.isAvailableOnServer()) {
try {
file.delete();
} catch (Exception e) {
String msg = String.format("Error deleting %s %s at level %s",
getScriptType(), name, level);
throw new GFEException(msg, e);
}
} else if (file.exists()) {
File jFile = file.getFile();
jFile.delete();
}
}
/**
* @see com.raytheon.viz.gfe.core.script.IScriptUtil#rename(java.lang.String,
* java.lang.String,
* com.raytheon.uf.common.localization.LocalizationContext.
* LocalizationLevel,
* com.raytheon.viz.gfe.core.script.IScriptUtil.Overwrite)
*/
@Override
public LocalizationFile rename(String source, String dest,
LocalizationLevel toLevel, Overwrite mode) throws GFEException {
LocalizationFile dst;
LocalizationFile src = find(source, null);
dst = copy(source, dest, toLevel, mode);
delete(source, src.getContext().getLocalizationLevel());
return dst;
}
/**
* Add ".py" to name if it doesn't end with ".py", ".pyc", or ".pyo" and
* return the result.
@ -398,7 +197,7 @@ public abstract class AbstractScriptUtil implements IScriptUtil {
* @return the decorated name.
*/
@Override
public String scripted(String name) {
public String normalize(String name) {
String result = null;
if (name.endsWith(PythonFileFilter.EXTENSION) || name.endsWith(".pyo")
|| name.endsWith(".pyc")) {

View file

@ -38,7 +38,7 @@ public class CopyInputValidator implements IInputValidator {
protected String scriptf;
protected IScriptUtil util;
protected PythonFileTemplate util;
private static final Pattern VALID_NAME = Pattern
.compile("^[\\p{Alpha}_]+\\w*");
@ -51,10 +51,10 @@ public class CopyInputValidator implements IInputValidator {
* @param util
* The script utility object that will do the work.
*/
public CopyInputValidator(String script, IScriptUtil util) {
public CopyInputValidator(String script, PythonFileTemplate util) {
this.script = script;
this.util = util;
this.scriptf = util.scripted(script);
this.scriptf = util.normalize(script);
}
/**
@ -72,7 +72,7 @@ public class CopyInputValidator implements IInputValidator {
if (!VALID_NAME.matcher(newText.trim()).matches()) {
return "Only letters, digits, and underscores are allowed.";
}
String newfname = util.scripted(newText);
String newfname = util.normalize(newText);
if (scriptf.equals(newfname)) {
return script + " cannot be copied to itself.";
}

View file

@ -22,47 +22,40 @@
*/
package com.raytheon.viz.gfe.core.script;
import com.raytheon.uf.common.localization.LocalizationFile;
import com.raytheon.uf.common.localization.LocalizationContext.LocalizationLevel;
import com.raytheon.uf.common.localization.LocalizationFile;
import com.raytheon.viz.gfe.GFEException;
/**
* An interface describing several common actions on user-defined scripts.
* An interface for generating new python files based on a template.
*
* <pre>
*
* SOFTWARE HISTORY
*
* Date Ticket# Engineer Description
* ------------ ---------- ----------- --------------------------
* ??? wldougher Initial creation
* Jan 19, 2016 4834 njensen Rename, refactor, clean up
*
* </pre>
*
* @author wldougher
*
*/
public interface IScriptUtil {
public interface PythonFileTemplate {
public enum Overwrite {
OVERWRITE, SAFE
};
/**
* Copy script to the new file toScript at localization level toLevel. If
* source and dest are the equal, toLevel must be different from the
* localization level of source. If dest already exists and mode is SAFE,
* dest will not be overwritten and the returned localization file will be
* null.
*
* @param source
* the simple name of the source script
* @param dest
* the simple name of the destination script
* @param mode
* Whether processing should proceed if dest already exists.
* @return A reference to toScript, which may be null if the copy fails.
* @throws GFEException
* if script and toScript have the same name and level or if the
* copy cannot complete due to file or network errors.
*/
public LocalizationFile copy(String source, String dest,
LocalizationLevel toLevel, Overwrite mode) throws GFEException;
}
/**
* Create a new script at the designated localization level. Implementations
* may initialize the contents of the new script however they desire.
*
* @param script
* @param level
* @param mode
* @return A reference to the newly created script, or null if the script
* could not be created.
*/
@ -70,29 +63,7 @@ public interface IScriptUtil {
Overwrite mode) throws GFEException;
/**
* Delete the script with the designated name at the designated localization
* level.
*
* @param name
* The simple name of the script.
* @param level
* the localization level at which the script should be deleted.
* @throws GFEException
* if underlying file or network operations fail.
*/
public void delete(String name, LocalizationLevel level)
throws GFEException;
/**
* The type of file to pass to PythonUtil.openPythonFile() when editing
* scripts of this type.
*
* @return the file type string
*/
public String getFileType();
/**
* The type of script this utility handles. Implementors should return this
* The type of script this utility handles. Implementers should return this
* in capitalized case, i.e., "Procedure", for use in error messages.
*
* @return the script type string
@ -116,21 +87,6 @@ public interface IScriptUtil {
public LocalizationFile find(String name, LocalizationLevel level)
throws GFEException;
/**
* Rename source to dest. This should be equivalent to calling copy()
* followed by delete().
*
* @param source
* the simple name of the source script
* @param dest
* the simple name of the destination script
* @param mode
* Whether processing should proceed if dest already exists.
* @return A reference to the renamed file.
*/
public LocalizationFile rename(String source, String dest,
LocalizationLevel toLevel, Overwrite mode) throws GFEException;
/**
* Convert a simple name to the name of a script file. For example,
* "Extrapolate" might become "Extrapolate.py".
@ -139,14 +95,14 @@ public interface IScriptUtil {
* The undecorated name
* @return The name of the script file derived from name.
*/
public String scripted(String name);
public String normalize(String name);
/**
* Get the path prefix used in building localization filenames. For example,
* the path prefix for Procedures is "gfe/userPython/procedures", or
* GfePyIncludeUtil.PROCEDURES.
*
* @return The path prefix
*/
public abstract String getScriptTypePathPrefix();
/**
* Get the path prefix used in building localization filenames. For example,
* the path prefix for Procedures is "gfe/userPython/procedures", or
* GfePyIncludeUtil.PROCEDURES.
*
* @return The path prefix
*/
public abstract String getScriptTypePathPrefix();
}

View file

@ -23,8 +23,8 @@
package com.raytheon.viz.gfe.core.script.action;
import org.eclipse.jface.action.Action;
import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.dialogs.IInputValidator;
import org.eclipse.jface.window.Window;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
@ -36,37 +36,43 @@ import com.raytheon.uf.common.status.UFStatus.Priority;
import com.raytheon.viz.gfe.GFEException;
import com.raytheon.viz.gfe.PythonUtil;
import com.raytheon.viz.gfe.core.script.ExistMode;
import com.raytheon.viz.gfe.core.script.IScriptUtil;
import com.raytheon.viz.gfe.core.script.IScriptUtil.Overwrite;
import com.raytheon.viz.gfe.core.script.NewInputValidator;
import com.raytheon.viz.gfe.core.script.PythonFileTemplate;
import com.raytheon.viz.gfe.core.script.PythonFileTemplate.Overwrite;
import com.raytheon.viz.gfe.dialogs.ScriptNameInputDialog;
/**
* An Action for creating a new instance of a script.
*
* <pre>
*
* SOFTWARE HISTORY
*
* Date Ticket# Engineer Description
* ------------ ---------- ----------- --------------------------
* ??? wldougher Initial creation
* Jan 19, 2016 4834 njensen Cleaned up warnings
*
* </pre>
*
* @author wldougher
*
*/
public class NewAction extends Action {
private static final transient IUFStatusHandler statusHandler = UFStatus
.getHandler(NewAction.class);
protected IScriptUtil util;
protected PythonFileTemplate util;
/**
* @param selected
* @param util
*/
public NewAction(IScriptUtil util) {
public NewAction(PythonFileTemplate util) {
super("New...");
this.util = util;
}
/**
*
*
* @see org.eclipse.jface.action.Action#run()
*/
@Override
public void run() {
Shell parent = Display.getCurrent().getActiveShell();
@ -76,7 +82,7 @@ public class NewAction extends Action {
+ type, "Name", "My" + type, validator, util);
dialog.setMode(ExistMode.ERR_EXISTS);
int rtnCode = dialog.open();
if (rtnCode == Dialog.OK) {
if (rtnCode == Window.OK) {
String script = dialog.getValue().trim();
try {
// since createNew() will only return if a LocalizationFile is

View file

@ -23,7 +23,7 @@ import com.raytheon.uf.common.dataplugin.gfe.python.GfePyIncludeUtil;
import com.raytheon.viz.gfe.core.script.AbstractScriptUtil;
/**
* TODO Add Description
* An implementation of PythonFileTemplate for making new Configs.
*
* <pre>
*
@ -31,7 +31,8 @@ import com.raytheon.viz.gfe.core.script.AbstractScriptUtil;
*
* Date Ticket# Engineer Description
* ------------ ---------- ----------- --------------------------
* Oct 8, 2010 mpduff Initial creation
* Oct 08, 2010 mpduff Initial creation
* Jan 19, 2016 4834 njensen Removed dead method
*
* </pre>
*
@ -40,6 +41,7 @@ import com.raytheon.viz.gfe.core.script.AbstractScriptUtil;
*/
public class ConfigUtil extends AbstractScriptUtil {
private static final String TEMPLATE_FILENAME = "config.vm";
private static final String scriptType = "Config";
@ -51,39 +53,19 @@ public class ConfigUtil extends AbstractScriptUtil {
super();
}
/**
* @see com.raytheon.viz.gfe.core.script.IScriptUtil#getScriptType()
*/
@Override
public String getScriptType() {
return scriptType;
}
/**
* @see com.raytheon.viz.gfe.core.script.AbstractScriptUtil#getScriptTypePathPrefix
* ()
*/
@Override
public String getScriptTypePathPrefix() {
return GfePyIncludeUtil.CONFIG;
}
/**
* @see com.raytheon.viz.gfe.core.script.AbstractScriptUtil#getVelocityTemplateName
* ()
*/
@Override
protected String getVelocityTemplateName() {
return TEMPLATE_FILENAME;
}
/*
* (non-Javadoc)
*
* @see com.raytheon.viz.gfe.core.script.IScriptUtil#getFileType()
*/
@Override
public String getFileType() {
return "CONFIG";
}
}

View file

@ -38,8 +38,8 @@ import com.raytheon.uf.common.status.UFStatus;
import com.raytheon.uf.common.status.UFStatus.Priority;
import com.raytheon.viz.gfe.GFEException;
import com.raytheon.viz.gfe.PythonUtil;
import com.raytheon.viz.gfe.core.script.IScriptUtil;
import com.raytheon.viz.gfe.core.script.IScriptUtil.Overwrite;
import com.raytheon.viz.gfe.core.script.PythonFileTemplate;
import com.raytheon.viz.gfe.core.script.PythonFileTemplate.Overwrite;
import com.raytheon.viz.gfe.textproduct.TextProductTableUtil;
import com.raytheon.viz.gfe.textproduct.TextProductUtil;
import com.raytheon.viz.ui.dialogs.CaveJFACEDialog;
@ -126,7 +126,7 @@ public class NewTextProductDialog extends CaveJFACEDialog {
return;
}
IScriptUtil util = null;
PythonFileTemplate util = null;
if (isSmart) {
util = new TextProductUtil();
} else {

View file

@ -39,7 +39,8 @@ import org.eclipse.ui.PlatformUI;
import com.raytheon.uf.common.dataplugin.gfe.db.objects.ParmID;
import com.raytheon.uf.common.localization.LocalizationFile;
import com.raytheon.viz.gfe.core.DataManager;
import com.raytheon.viz.gfe.core.DataManagerUIFactory;
import com.raytheon.viz.gfe.core.IParmManager;
import com.raytheon.viz.gfe.core.parm.Parm;
import com.raytheon.viz.gfe.smarttool.SmartToolEdit;
import com.raytheon.viz.ui.dialogs.CaveJFACEDialog;
@ -53,6 +54,7 @@ import com.raytheon.viz.ui.dialogs.CaveJFACEDialog;
* ------------ ---------- ----------- --------------------------
* Feb 20, 2008 njensen Initial creation
* Nov 12, 2012 1298 rferrel Changes for non-blocking dialog.
* Jan 19, 2016 4834 njensen Cleaned up warnings
*
* </pre>
*
@ -77,13 +79,6 @@ public class NewToolDialog extends CaveJFACEDialog {
setShellStyle(SWT.DIALOG_TRIM | SWT.APPLICATION_MODAL);
}
/*
* (non-Javadoc)
*
* @see
* org.eclipse.jface.window.Window#configureShell(org.eclipse.swt.widgets
* .Shell)
*/
@Override
protected void configureShell(Shell shell) {
super.configureShell(shell);
@ -92,11 +87,6 @@ public class NewToolDialog extends CaveJFACEDialog {
}
}
/*
* (non-Javadoc)
*
* @see org.eclipse.jface.dialogs.Dialog#buttonPressed(int)
*/
@Override
protected void buttonPressed(int buttonId) {
if (buttonId == IDialogConstants.OK_ID) {
@ -124,30 +114,14 @@ public class NewToolDialog extends CaveJFACEDialog {
}
}
/*
* (non-Javadoc)
*
* @see
* org.eclipse.jface.dialogs.Dialog#createButtonsForButtonBar(org.eclipse
* .swt.widgets.Composite)
*/
@Override
protected void createButtonsForButtonBar(Composite parent) {
createButton(parent, IDialogConstants.OK_ID, IDialogConstants.OK_LABEL,
true);
createButton(parent, IDialogConstants.CANCEL_ID,
IDialogConstants.CANCEL_LABEL, false);
}
/*
* (non-Javadoc)
*
* @see
* org.eclipse.jface.dialogs.Dialog#createDialogArea(org.eclipse.swt.widgets
* .Composite)
*/
@Override
protected Control createDialogArea(final Composite parent) {
Composite composite = (Composite) super.createDialogArea(parent);
@ -180,8 +154,8 @@ public class NewToolDialog extends CaveJFACEDialog {
// select the active parm if not null otherwise select the first parm in
// the list
Parm parm = DataManager.getCurrentInstance().getSpatialDisplayManager()
.getActivatedParm();
Parm parm = DataManagerUIFactory.getCurrentInstance()
.getSpatialDisplayManager().getActivatedParm();
if (parm != null) {
parms.setSelection(new String[] { parm.getParmID().getParmName() });
} else {
@ -195,12 +169,9 @@ public class NewToolDialog extends CaveJFACEDialog {
public String[] getParms() {
IWorkbenchWindow window = PlatformUI.getWorkbench()
.getActiveWorkbenchWindow();
ParmID[] parms = DataManager
.getInstance(window)
.getParmManager()
.getAvailableParms(
DataManager.getInstance(window).getParmManager()
.getMutableDatabase());
IParmManager pm = DataManagerUIFactory.getInstance(window)
.getParmManager();
ParmID[] parms = pm.getAvailableParms(pm.getMutableDatabase());
java.util.List<String> parmNames = new ArrayList<String>(
parms.length + 2);
for (ParmID p : parms) {

View file

@ -30,11 +30,9 @@ import com.raytheon.uf.common.localization.LocalizationFile;
import com.raytheon.uf.common.status.IUFStatusHandler;
import com.raytheon.uf.common.status.UFStatus;
import com.raytheon.uf.common.status.UFStatus.Priority;
import com.raytheon.uf.viz.core.Activator;
import com.raytheon.viz.gfe.GFEException;
import com.raytheon.viz.gfe.constants.StatusConstants;
import com.raytheon.viz.gfe.core.script.ExistMode;
import com.raytheon.viz.gfe.core.script.IScriptUtil;
import com.raytheon.viz.gfe.core.script.PythonFileTemplate;
/**
* A dialog for the user to input the name of a script.
@ -43,9 +41,10 @@ import com.raytheon.viz.gfe.core.script.IScriptUtil;
*
*/
public class ScriptNameInputDialog extends InputDialog {
private static final transient IUFStatusHandler statusHandler = UFStatus.getHandler(ScriptNameInputDialog.class);
private static final transient IUFStatusHandler statusHandler = UFStatus
.getHandler(ScriptNameInputDialog.class);
protected IScriptUtil util;
protected PythonFileTemplate util;
protected ExistMode mode;
@ -63,10 +62,11 @@ public class ScriptNameInputDialog extends InputDialog {
* @param validator
* The validator to apply to the input field. This is called for
* each character typed.
* @param util
*/
public ScriptNameInputDialog(Shell parentShell, String dialogTitle,
String dialogMessage, String initialValue,
IInputValidator validator, IScriptUtil util) {
IInputValidator validator, PythonFileTemplate util) {
super(parentShell, dialogTitle, dialogMessage, initialValue, validator);
this.util = util;
mode = ExistMode.NONE;
@ -89,8 +89,7 @@ public class ScriptNameInputDialog extends InputDialog {
super.okPressed();
}
} catch (GFEException e) {
statusHandler.handle(Priority.PROBLEM,
"Error validating name", e);
statusHandler.handle(Priority.PROBLEM, "Error validating name", e);
}
}

View file

@ -23,7 +23,7 @@ import org.eclipse.jface.action.IMenuManager;
import com.raytheon.uf.viz.localization.adapter.LocalizationPerspectiveAdapter;
import com.raytheon.uf.viz.localization.filetreeview.FileTreeEntryData;
import com.raytheon.viz.gfe.core.script.IScriptUtil;
import com.raytheon.viz.gfe.core.script.PythonFileTemplate;
import com.raytheon.viz.gfe.core.script.action.NewAction;
import com.raytheon.viz.gfe.procedures.util.ProcedureUtil;
@ -55,7 +55,7 @@ public class ProceduresAdapter extends LocalizationPerspectiveAdapter {
@Override
public boolean addContextMenuItems(IMenuManager menuMgr,
FileTreeEntryData[] selectedData) {
IScriptUtil util = new ProcedureUtil();
PythonFileTemplate util = new ProcedureUtil();
NewAction newAction = new NewAction(util);
menuMgr.add(newAction);

View file

@ -26,7 +26,18 @@ import com.raytheon.uf.common.dataplugin.gfe.python.GfePyIncludeUtil;
import com.raytheon.viz.gfe.core.script.AbstractScriptUtil;
/**
* A concrete implementation of IScriptUtil for working with Procedure scripts.
* An implementation of PythonFileTemplate for making new Procedures.
*
* <pre>
*
* SOFTWARE HISTORY
*
* Date Ticket# Engineer Description
* ------------ ---------- ----------- --------------------------
* ??? wldougher Initial creation
* Jan 19, 2016 4834 njensen Removed dead method
*
* </pre>
*
* @author wldougher
*
@ -44,39 +55,19 @@ public class ProcedureUtil extends AbstractScriptUtil {
super();
}
/**
* @see com.raytheon.viz.gfe.core.script.IScriptUtil#getScriptType()
*/
@Override
public String getScriptType() {
return scriptType;
}
/**
* @see com.raytheon.viz.gfe.core.script.AbstractScriptUtil#getScriptTypePathPrefix
* ()
*/
@Override
public String getScriptTypePathPrefix() {
return GfePyIncludeUtil.PROCEDURES;
}
/**
* @see com.raytheon.viz.gfe.core.script.AbstractScriptUtil#getVelocityTemplateName
* ()
*/
@Override
protected String getVelocityTemplateName() {
return TEMPLATE_FILENAME;
}
/*
* (non-Javadoc)
*
* @see com.raytheon.viz.gfe.core.script.IScriptUtil#getFileType()
*/
@Override
public String getFileType() {
return "PROCEDURE";
}
}

View file

@ -20,8 +20,8 @@
package com.raytheon.viz.gfe.smarttool;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.Properties;
import org.apache.velocity.Template;
@ -40,6 +40,7 @@ import com.raytheon.uf.common.localization.LocalizationContext.LocalizationLevel
import com.raytheon.uf.common.localization.LocalizationContext.LocalizationType;
import com.raytheon.uf.common.localization.LocalizationFile;
import com.raytheon.uf.common.localization.PathManagerFactory;
import com.raytheon.uf.common.localization.SaveableOutputStream;
import com.raytheon.uf.common.python.PythonFileFilter;
import com.raytheon.uf.common.status.IUFStatusHandler;
import com.raytheon.uf.common.status.UFStatus;
@ -53,11 +54,12 @@ import com.raytheon.viz.gfe.PythonUtil;
*
* <pre>
* SOFTWARE HISTORY
* Date Ticket# Engineer Description
* ------------ ---------- ----------- --------------------------
* Mar 20, 2008 njensen Initial creation
* Sep 25, 2008 1562 askripsky Moved methods out to
* Date Ticket# Engineer Description
* ------------ ---------- ----------- --------------------------
* Mar 20, 2008 njensen Initial creation
* Sep 25, 2008 1562 askripsky Moved methods out to
* Sep 09, 2013 #2033 dgilling Use new templates directory.
* Jan 18, 2016 4834 njensen Fix creating new files, deleted dead code
*
* </pre>
*
@ -66,6 +68,15 @@ import com.raytheon.viz.gfe.PythonUtil;
*/
public class SmartToolEdit {
/*
* TODO Functionality should be consolidated to an extension of
* AbstractScriptUtil to follow the same pattern as when creating other new
* python files for GFE. Currently AbstractScriptUtil does not support extra
* args to velocity like the parmToEdit so it will need to be further
* improved before that is possible.
*/
private static final transient IUFStatusHandler statusHandler = UFStatus
.getHandler(SmartToolEdit.class);
@ -102,23 +113,22 @@ public class SmartToolEdit {
IPathManager pathMgr = PathManagerFactory.getPathManager();
LocalizationContext cx = pathMgr.getContext(
LocalizationType.CAVE_STATIC, LocalizationLevel.USER);
localizationFile = PathManagerFactory.getPathManager()
.getLocalizationFile(
cx,
GfePyIncludeUtil.SMART_TOOLS + File.separator
+ toolFilename);
if (localizationFile != null) {
File file = localizationFile.getFile();
if (!file.getParentFile().exists()) {
file.getParentFile().mkdirs();
String filename = GfePyIncludeUtil.SMART_TOOLS
+ IPathManager.SEPARATOR + toolFilename;
localizationFile = pathMgr.getLocalizationFile(cx, filename);
if (!localizationFile.isProtected()) {
try (SaveableOutputStream sos = localizationFile
.openOutputStream();
OutputStreamWriter writer = new OutputStreamWriter(
sos)) {
template.merge(context, writer);
writer.close();
sos.save();
// refresh reference in memory
localizationFile = pathMgr.getLocalizationFile(cx,
filename);
PythonUtil.openPythonFile(localizationFile);
}
FileWriter fw = new FileWriter(localizationFile.getFile());
template.merge(context, fw);
fw.close();
localizationFile.save();
PythonUtil.openPythonFile(localizationFile);
} else {
// Display Protected file message
Shell shell = PlatformUI.getWorkbench()
@ -140,66 +150,4 @@ public class SmartToolEdit {
return localizationFile;
}
public static LocalizationFile initializeNewUtility(String utilityName) {
LocalizationFile localizationFile = null;
String toolFilename = new String(utilityName);
if (!toolFilename.endsWith(PythonFileFilter.EXTENSION)) {
toolFilename += PythonFileFilter.EXTENSION;
}
try {
File templateFile = new File(FileLocator.resolve(
FileLocator.find(Activator.getDefault().getBundle(),
new Path(SmartToolConstants.TEMPLATES_DIR), null))
.getPath());
try {
Properties p = new Properties();
p.setProperty("file.resource.loader.path",
templateFile.getPath());
Velocity.init(p);
VelocityContext context = new VelocityContext();
context.put("itemName", utilityName);
String author = LocalizationManager.getInstance()
.getCurrentUser();
context.put("author", author);
Template template = null;
template = Velocity.getTemplate("utility.vm");
IPathManager pathMgr = PathManagerFactory.getPathManager();
LocalizationContext cx = pathMgr.getContext(
LocalizationType.CAVE_STATIC, LocalizationLevel.USER);
localizationFile = PathManagerFactory.getPathManager()
.getLocalizationFile(
cx,
GfePyIncludeUtil.UTILITIES + File.separator
+ toolFilename);
File file = localizationFile.getFile();
if (!file.getParentFile().exists()) {
file.getParentFile().mkdirs();
}
FileWriter fw = new FileWriter(localizationFile.getFile());
template.merge(context, fw);
fw.close();
localizationFile.save();
PythonUtil.openPythonFile(localizationFile);
} catch (Exception e) {
statusHandler.handle(Priority.PROBLEM,
"Error creating new utility", e);
}
} catch (IOException e) {
statusHandler.error(
"Error retrieving templates directory for new utility.", e);
}
return localizationFile;
}
}

View file

@ -23,7 +23,7 @@ import com.raytheon.uf.common.dataplugin.gfe.python.GfePyIncludeUtil;
import com.raytheon.viz.gfe.core.script.AbstractScriptUtil;
/**
* TODO Add Description
* An implementation of PythonFileTemplate for making new Utilities.
*
* <pre>
*
@ -31,7 +31,8 @@ import com.raytheon.viz.gfe.core.script.AbstractScriptUtil;
*
* Date Ticket# Engineer Description
* ------------ ---------- ----------- --------------------------
* May 3, 2011 dgilling Initial creation
* May 03, 2011 dgilling Initial creation
* Jan 19, 2016 4834 njensen Removed dead method
*
* </pre>
*
@ -52,45 +53,16 @@ public class UtilitiesScriptUtil extends AbstractScriptUtil {
super();
}
/*
* (non-Javadoc)
*
* @see com.raytheon.viz.gfe.core.script.IScriptUtil#getFileType()
*/
@Override
public String getFileType() {
return SCRIPT_TYPE.toUpperCase();
}
/*
* (non-Javadoc)
*
* @see com.raytheon.viz.gfe.core.script.AbstractScriptUtil#getScriptType()
*/
@Override
public String getScriptType() {
return SCRIPT_TYPE;
}
/*
* (non-Javadoc)
*
* @see
* com.raytheon.viz.gfe.core.script.AbstractScriptUtil#getScriptTypePathPrefix
* ()
*/
@Override
public String getScriptTypePathPrefix() {
return GfePyIncludeUtil.UTILITIES;
}
/*
* (non-Javadoc)
*
* @see
* com.raytheon.viz.gfe.core.script.AbstractScriptUtil#getVelocityTemplateName
* ()
*/
@Override
protected String getVelocityTemplateName() {
return TEMPLATE_FILENAME;

View file

@ -26,7 +26,18 @@ import com.raytheon.uf.common.dataplugin.gfe.python.GfePyIncludeUtil;
import com.raytheon.viz.gfe.core.script.AbstractScriptUtil;
/**
* A concrete implementation of IScriptUtil for working with Procedure scripts.
* An implementation of PythonFileTemplate for making new Text Products.
*
* <pre>
*
* SOFTWARE HISTORY
*
* Date Ticket# Engineer Description
* ------------ ---------- ----------- --------------------------
* ??? wldougher Initial creation
* Jan 19, 2016 4834 njensen Removed dead method
*
* </pre>
*
* @author wldougher
*
@ -37,9 +48,6 @@ public class TextProductUtil extends AbstractScriptUtil {
private static final String SCRIPT_TYPE = "Text Product";
/**
* @see com.raytheon.viz.gfe.core.script.IScriptUtil#getScriptType()
*/
@Override
public String getScriptType() {
return SCRIPT_TYPE;
@ -49,32 +57,14 @@ public class TextProductUtil extends AbstractScriptUtil {
return SCRIPT_TYPE;
}
/**
* @see com.raytheon.viz.gfe.core.script.AbstractScriptUtil#getScriptTypePathPrefix
* ()
*/
@Override
public String getScriptTypePathPrefix() {
return GfePyIncludeUtil.TEXT_PRODUCTS;
}
/**
* @see com.raytheon.viz.gfe.core.script.AbstractScriptUtil#getVelocityTemplateName
* ()
*/
@Override
protected String getVelocityTemplateName() {
return TEMPLATE_FILENAME;
}
/*
* (non-Javadoc)
*
* @see com.raytheon.viz.gfe.core.script.IScriptUtil#getFileType()
*/
@Override
public String getFileType() {
return "TEXT_PRODUCT";
}
}

View file

@ -23,7 +23,7 @@ import org.eclipse.jface.action.IMenuManager;
import com.raytheon.uf.viz.localization.adapter.LocalizationPerspectiveAdapter;
import com.raytheon.uf.viz.localization.filetreeview.FileTreeEntryData;
import com.raytheon.viz.gfe.core.script.IScriptUtil;
import com.raytheon.viz.gfe.core.script.PythonFileTemplate;
import com.raytheon.viz.gfe.core.script.action.NewAction;
/**
@ -53,7 +53,7 @@ public class TextUtilityAdapter extends LocalizationPerspectiveAdapter {
@Override
public boolean addContextMenuItems(IMenuManager menuMgr,
FileTreeEntryData[] selectedData) {
IScriptUtil util = new TextUtilityUtil();
PythonFileTemplate util = new TextUtilityUtil();
NewAction newAction = new NewAction(util);
menuMgr.add(newAction);

View file

@ -22,13 +22,23 @@
*/
package com.raytheon.viz.gfe.textproduct;
import java.io.File;
import com.raytheon.uf.common.dataplugin.gfe.python.GfePyIncludeUtil;
import com.raytheon.uf.common.localization.IPathManager;
import com.raytheon.viz.gfe.core.script.AbstractScriptUtil;
/**
* A concrete implementation of IScriptUtil for working with Procedure scripts.
* An implementation of PythonFileTemplate for making new Text Utilities.
*
* <pre>
*
* SOFTWARE HISTORY
*
* Date Ticket# Engineer Description
* ------------ ---------- ----------- --------------------------
* ??? wldougher Initial creation
* Jan 19, 2016 4834 njensen Removed dead method
*
* </pre>
*
* @author wldougher
*
@ -39,44 +49,22 @@ public class TextUtilityUtil extends AbstractScriptUtil {
private static final String SCRIPT_TYPE = "Utility";
private static final String FILETYPE = "TEXT_PRODUCT";
private static final String PATH_PREFIX = GfePyIncludeUtil.TEXT_UTILITIES
+ File.separator + "regular";
+ IPathManager.SEPARATOR + "regular";
/**
* @see com.raytheon.viz.gfe.core.script.IScriptUtil#getScriptType()
*/
@Override
public String getScriptType() {
return SCRIPT_TYPE;
}
/**
* @see com.raytheon.viz.gfe.core.script.AbstractScriptUtil#getScriptTypePathPrefix
* ()
*/
@Override
public String getScriptTypePathPrefix() {
return PATH_PREFIX;
}
/**
* @see com.raytheon.viz.gfe.core.script.AbstractScriptUtil#getVelocityTemplateName
* ()
*/
@Override
protected String getVelocityTemplateName() {
return TEMPLATE_FILENAME;
}
/*
* (non-Javadoc)
*
* @see com.raytheon.viz.gfe.core.script.IScriptUtil#getFileType()
*/
@Override
public String getFileType() {
return FILETYPE;
}
}

View file

@ -1,93 +0,0 @@
/**
* This software was developed and / or modified by Raytheon Company,
* pursuant to Contract DG133W-05-CQ-1067 with the US Government.
*
* U.S. EXPORT CONTROLLED TECHNICAL DATA
* This software product contains export-restricted data whose
* export/transfer/disclosure is restricted by U.S. law. Dissemination
* to non-U.S. persons whether in the United States or abroad requires
* an export license or other authorization.
*
* Contractor Name: Raytheon Company
* Contractor Address: 6825 Pine Street, Suite 340
* Mail Stop B8
* Omaha, NE 68106
* 402.291.0100
*
* See the AWIPS II Master Rights File ("Master Rights File.pdf") for
* further licensing information.
**/
package com.raytheon.viz.gfe.textproduct.action;
import org.eclipse.jface.action.Action;
import com.raytheon.uf.common.localization.LocalizationContext.LocalizationLevel;
import com.raytheon.uf.common.localization.LocalizationContext.LocalizationType;
import com.raytheon.uf.common.status.IUFStatusHandler;
import com.raytheon.uf.common.status.UFStatus;
import com.raytheon.uf.common.status.UFStatus.Priority;
import com.raytheon.viz.gfe.Activator;
import com.raytheon.viz.gfe.GFEException;
import com.raytheon.viz.gfe.constants.StatusConstants;
import com.raytheon.viz.gfe.core.script.IScriptUtil;
import com.raytheon.viz.gfe.ui.AccessMgr;
/**
* Action to delete a text product.
*
* <pre>
*
* SOFTWARE HISTORY
* Date Ticket# Engineer Description
* ------------ ---------- ----------- --------------------------
* Jul 30, 2010 dgilling Initial creation
*
* </pre>
*
* @author dgilling
* @version 1.0
*/
public class TextProductDeleteAction extends Action {
private static final transient IUFStatusHandler statusHandler = UFStatus.getHandler(TextProductDeleteAction.class);
protected String scriptName;
protected IScriptUtil util;
/**
* Constructor
*
* @param scriptName
* The simple name of the script to be deleted.
* @param util
* The utility object that will actually delete the script
*/
public TextProductDeleteAction(String scriptName, IScriptUtil util) {
super("Delete");
this.scriptName = scriptName;
this.util = util;
}
/**
* Delete the procedure this action is tied to.
*
* @see org.eclipse.jface.action.Action#run()
*/
@Override
public void run() {
if (AccessMgr.verifyDelete(util.scripted(scriptName),
LocalizationType.CAVE_STATIC, false)) {
try {
// Delete the script
util.delete(scriptName, LocalizationLevel.USER);
statusHandler.handle(Priority.VERBOSE,
"USER " + util.getScriptType() + " " + scriptName
+ " deleted.");
} catch (GFEException e) {
statusHandler.handle(Priority.PROBLEM,
"Error deleting USER " + util.getScriptType() + " "
+ scriptName, e);
}
}
}
}

View file

@ -1,181 +0,0 @@
/**
* This software was developed and / or modified by Raytheon Company,
* pursuant to Contract DG133W-05-CQ-1067 with the US Government.
*
* U.S. EXPORT CONTROLLED TECHNICAL DATA
* This software product contains export-restricted data whose
* export/transfer/disclosure is restricted by U.S. law. Dissemination
* to non-U.S. persons whether in the United States or abroad requires
* an export license or other authorization.
*
* Contractor Name: Raytheon Company
* Contractor Address: 6825 Pine Street, Suite 340
* Mail Stop B8
* Omaha, NE 68106
* 402.291.0100
*
* See the AWIPS II Master Rights File ("Master Rights File.pdf") for
* further licensing information.
**/
package com.raytheon.viz.gfe.textproduct.action;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.eclipse.jface.action.Action;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.swt.widgets.Display;
import com.raytheon.uf.common.dataplugin.gfe.python.GfePyIncludeUtil;
import com.raytheon.uf.common.localization.LocalizationContext.LocalizationLevel;
import com.raytheon.uf.common.localization.LocalizationFile;
import com.raytheon.uf.common.status.IUFStatusHandler;
import com.raytheon.uf.common.status.UFStatus;
import com.raytheon.uf.common.status.UFStatus.Priority;
import com.raytheon.viz.gfe.Activator;
import com.raytheon.viz.gfe.GFEException;
import com.raytheon.viz.gfe.constants.StatusConstants;
import com.raytheon.viz.gfe.core.script.IScriptUtil;
import com.raytheon.viz.gfe.core.script.IScriptUtil.Overwrite;
/**
* An action to upgrade a text product to the SITE localization level.
*
* <pre>
*
* SOFTWARE HISTORY
* Date Ticket# Engineer Description
* ------------ ---------- ----------- --------------------------
* Jul 29, 2010 dgilling Initial creation
*
* </pre>
*
* @author dgilling
* @version 1.0
*/
public class TextProductSiteAction extends Action {
private static final transient IUFStatusHandler statusHandler = UFStatus.getHandler(TextProductSiteAction.class);
private static final Pattern OVR_PATTERN = Pattern.compile(
"(.+?)_?overrides", Pattern.CASE_INSENSITIVE);
String script;
IScriptUtil util;
/**
* @param script
* The simple name of the script, i.e., "OAX_APD"
* @param util
* The script utility instance that provides low-level
* script-manipulation methods
*/
public TextProductSiteAction(String script, IScriptUtil util) {
super("Make SITE");
this.script = script;
this.util = util;
}
/**
* @see org.eclipse.jface.action.Action#run()
*/
@Override
public void run() {
String scriptType = util.getScriptType();
LocalizationFile source = null;
try {
source = util.find(script, null);
} catch (GFEException e) {
String message = String.format(
"An error occurred while finding '%s'", script);
statusHandler.handle(Priority.PROBLEM, message, e);
return;
}
// Make sure the source exists
if (source == null) {
String message = String.format("%s '%s' does not exist",
util.getScriptType(), script);
MessageDialog.openError(Display.getCurrent().getActiveShell(),
"Does Not Exist", message);
statusHandler.handle(Priority.PROBLEM, message);
return;
}
LocalizationLevel srcLevel = source.getContext().getLocalizationLevel();
String dest = script;
Matcher ovrMatch = OVR_PATTERN.matcher(script);
boolean isOverride = ovrMatch.matches();
if (isOverride) {
// strip "overrides" or "_overrides" from destination name
dest = ovrMatch.group(1);
}
if (LocalizationLevel.BASE == srcLevel) {
// source is BASE, we can't downgrade it
String message = String
.format("%s '%s' is at BASE level and cannot be moved to SITE level.",
scriptType, script);
MessageDialog.openInformation(
Display.getCurrent().getActiveShell(), "Cannot Move Base",
message);
return;
}
if (LocalizationLevel.SITE == srcLevel && !isOverride) {
// nothing to do
String message = String.format("%s '%s' is already at SITE level.",
scriptType, script);
MessageDialog.openInformation(
Display.getCurrent().getActiveShell(), message, message);
return;
}
LocalizationFile destlf = null;
try {
destlf = util.find(dest, LocalizationLevel.SITE);
} catch (GFEException e1) {
String message = String.format("Error during search for %s '%s'",
scriptType, dest);
statusHandler.handle(Priority.PROBLEM, message);
return;
}
if (destlf != null
&& destlf.getFile().getPath()
.contains(GfePyIncludeUtil.TEXT_PRODUCTS)) {
// destination already exists. Confirm overwrite.
String message = String
.format("%s '%s' already exists at SITE level!\n"
+ "Confirm that you want to overwrite it from %s at %s level:",
scriptType, dest, script, srcLevel.toString());
boolean confirmed = MessageDialog.openConfirm(Display.getCurrent()
.getActiveShell(), "Confirm Overwrite", message);
if (confirmed) {
try {
destlf.delete();
} catch (Exception e) {
String errMessage = String.format("Error deleting %s '%s'",
scriptType, dest);
statusHandler.handle(Priority.PROBLEM, errMessage, e);
}
} else {
return;
}
}
try {
util.copy(script, dest, LocalizationLevel.SITE, Overwrite.SAFE);
source.delete();
String message = String.format("%s '%s' set as SITE", scriptType,
dest);
statusHandler.handle(Priority.VERBOSE, message);
} catch (Exception e) {
String message = String.format("Error moving %s to SITE", script);
statusHandler.handle(Priority.PROBLEM, message, e);
}
}
}

View file

@ -1,161 +0,0 @@
package com.raytheon.viz.gfe.textproduct.action;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.eclipse.jface.action.Action;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.swt.widgets.Display;
import com.raytheon.uf.common.localization.LocalizationContext.LocalizationLevel;
import com.raytheon.uf.common.localization.LocalizationFile;
import com.raytheon.uf.common.status.IUFStatusHandler;
import com.raytheon.uf.common.status.UFStatus;
import com.raytheon.uf.common.status.UFStatus.Priority;
import com.raytheon.viz.gfe.Activator;
import com.raytheon.viz.gfe.GFEException;
import com.raytheon.viz.gfe.constants.StatusConstants;
import com.raytheon.viz.gfe.core.script.IScriptUtil;
import com.raytheon.viz.gfe.core.script.IScriptUtil.Overwrite;
/**
* An action to upgrade a a text product and make it the SITE override script.
*
* <pre>
*
* SOFTWARE HISTORY
* Date Ticket# Engineer Description
* ------------ ---------- ----------- --------------------------
* Jul 30, 2010 dgilling Initial creation
*
* </pre>
*
* @author dgilling
* @version 1.0
*/
public class TextProductSiteOverrideAction extends Action {
private static final transient IUFStatusHandler statusHandler = UFStatus.getHandler(TextProductSiteOverrideAction.class);
private static final Pattern OVR_PATTERN = Pattern.compile(
"(.+?)_?overrides", Pattern.CASE_INSENSITIVE);
String script;
IScriptUtil util;
public TextProductSiteOverrideAction(String script, IScriptUtil util) {
super("Make SITE Override");
this.script = script;
this.util = util;
}
/**
* @see org.eclipse.jface.action.Action#run()
*/
@Override
public void run() {
String scriptType = util.getScriptType();
LocalizationFile source = null;
try {
source = util.find(script, null);
} catch (GFEException e) {
String message = String.format(
"An error occurred while finding '%s'", script);
statusHandler.handle(Priority.PROBLEM, message, e);
return;
}
// Make sure the source exists
if (source == null) {
String message = String.format("%s '%s' does not exist",
util.getScriptType(), script);
MessageDialog.openError(Display.getCurrent().getActiveShell(),
"Does Not Exist", message);
statusHandler.handle(Priority.PROBLEM, message);
return;
}
LocalizationLevel srcLevel = source.getContext().getLocalizationLevel();
String dest = script;
Matcher ovrMatch = OVR_PATTERN.matcher(script);
boolean isOverride = ovrMatch.matches();
if (isOverride) {
// strip "override" or "_override" from destination name
dest = ovrMatch.group(1);
}
if (srcLevel.isSystemLevel()) {
// source is BASE, we can't downgrade it
String message = String.format(
"%s '%s' cannot be moved to SITE level.", scriptType,
script);
MessageDialog.openInformation(
Display.getCurrent().getActiveShell(), "Cannot Move Base",
message);
return;
}
if (LocalizationLevel.SITE == srcLevel) {
// nothing to do, in two flavors:
String message = null;
if (isOverride) {
message = String.format(
"%s '%s' is already the site override.", scriptType,
script);
} else {
message = String.format("'%s' is the SITE %s.\n"
+ "It cannot be made the SITE override.", script,
scriptType);
}
MessageDialog.openInformation(
Display.getCurrent().getActiveShell(), message, message);
return;
}
dest = dest + "_OVERRIDE";
LocalizationFile destlf = null;
try {
destlf = util.find(dest, LocalizationLevel.SITE);
} catch (GFEException e1) {
String message = String.format("Error during search for %s '%s'",
scriptType, dest);
statusHandler.handle(Priority.PROBLEM, message);
return;
}
if (destlf != null) {
// destination already exists. Confirm overwrite.
String message = String
.format("%s '%s' already exists at SITE level!\n"
+ "Confirm that you want to overwrite it from %s at %s level:",
scriptType, dest, script, srcLevel.toString());
boolean confirmed = MessageDialog.openConfirm(Display.getCurrent()
.getActiveShell(), "Confirm Overwrite", message);
if (confirmed) {
try {
destlf.delete();
} catch (Exception e) {
String errMessage = String.format("Error deleting %s '%s'",
scriptType, dest);
statusHandler.handle(Priority.PROBLEM, errMessage, e);
}
} else {
return;
}
}
try {
util.copy(script, dest, LocalizationLevel.SITE, Overwrite.SAFE);
source.delete();
String message = String.format("%s '%s' set as SITE", scriptType,
dest);
statusHandler.handle(Priority.VERBOSE, message);
} catch (Exception e) {
String message = String.format("Error ", script);
statusHandler.handle(Priority.PROBLEM, message, e);
}
}
}