You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
185 lines
6.5 KiB
185 lines
6.5 KiB
package com.fr.plugin.file.submit.oss.script; |
|
|
|
import com.fr.base.BaseFormula; |
|
import com.fr.stable.ParameterProvider; |
|
import com.fanruan.api.cal.ParameterKit; |
|
import com.fr.base.ResultFormula; |
|
import com.fanruan.api.util.GeneralKit; |
|
import com.fanruan.api.xml.XmlKit; |
|
import com.fr.js.Hyperlink; |
|
import com.fanruan.api.err.JSONException; |
|
import com.fr.json.JSONObject; |
|
import com.fanruan.api.json.JSONKit; |
|
import com.fanruan.api.log.LogKit; |
|
import com.fr.plugin.file.submit.oss.fun.OssVariableValue; |
|
import com.fr.script.Calculator; |
|
import com.fanruan.api.util.ArrayKit; |
|
import com.fanruan.api.util.AssistKit; |
|
import com.fanruan.api.util.StringKit; |
|
import com.fr.stable.web.Repository; |
|
import com.fr.stable.xml.XMLPrintWriter; |
|
import com.fr.stable.xml.XMLableReader; |
|
import com.fanruan.api.session.SessionKit; |
|
import org.jetbrains.annotations.NotNull; |
|
|
|
import java.util.HashMap; |
|
import java.util.Map; |
|
|
|
public class OssDownloadHyperlink extends Hyperlink { |
|
|
|
private String bucket; |
|
private OssVariableValue directory; |
|
private OssVariableValue fileName; |
|
|
|
public String getBucket() { |
|
return bucket; |
|
} |
|
|
|
public void setBucket(String bucket) { |
|
this.bucket = bucket; |
|
} |
|
|
|
public OssVariableValue getDirectory() { |
|
return directory; |
|
} |
|
|
|
public void setDirectory(OssVariableValue directory) { |
|
this.directory = directory; |
|
modifyParameters(directory, "directory"); |
|
} |
|
|
|
public OssVariableValue getFileName() { |
|
return fileName; |
|
} |
|
|
|
public void setFileName(OssVariableValue fileName) { |
|
this.fileName = fileName; |
|
modifyParameters(fileName, "fileName"); |
|
} |
|
|
|
private void modifyParameters(OssVariableValue variableValue, String parameterName) { |
|
if (variableValue == null) { |
|
return; |
|
} |
|
ParameterProvider[] providers = getParameters(); |
|
if (ArrayKit.isEmpty(providers)) { |
|
providers = ArrayKit.add(providers, ParameterKit.newParameter(parameterName, variableValue.getValue())); |
|
} else { |
|
Map<String, ParameterProvider> map = new HashMap<String, ParameterProvider>(); |
|
for (ParameterProvider provider : providers) { |
|
map.put(provider.getName(), provider); |
|
} |
|
if (map.containsKey(parameterName)) { |
|
map.get(parameterName).setValue(variableValue.getValue()); |
|
} else { |
|
providers = ArrayKit.add(providers, ParameterKit.newParameter(parameterName, variableValue.getValue())); |
|
} |
|
} |
|
setParameters(providers); |
|
} |
|
|
|
|
|
@Override |
|
protected String actionJS(Repository repository) throws JSONException{ |
|
JSONObject data = JSONKit.create(); |
|
try { |
|
data.put("bucket", bucket); |
|
if (directory != null) { |
|
data.put("directory", getResultValue(repository, "directory", directory)); |
|
} |
|
if (fileName != null) { |
|
data.put("fileName", getResultValue(repository, "fileName", fileName)); |
|
} |
|
} catch (JSONException e) { |
|
LogKit.error(e.getMessage(), e); |
|
} |
|
return "FR.doHyperlinkByOssDownload(" + data.toString() + ");"; |
|
} |
|
|
|
@NotNull |
|
private String getResultValue(Repository repository, String parameterName, OssVariableValue variableValue){ |
|
Object result = null; |
|
ParameterProvider[] providers = getParameters(); |
|
for (ParameterProvider provider : providers) { |
|
if (AssistKit.equals(parameterName, provider.getName())) { |
|
Object value = provider.getValue(); |
|
if (value instanceof BaseFormula) { |
|
result = ((BaseFormula) value).getResult(); |
|
if (result instanceof ResultFormula) { |
|
result = ((ResultFormula)result).getResult(); |
|
} |
|
break; |
|
} else { |
|
result = value; |
|
} |
|
} |
|
} |
|
if (result == null) { |
|
Calculator calculator = newCalculator(repository); |
|
result = variableValue.getResult(calculator); |
|
} |
|
return GeneralKit.objectToString(result); |
|
} |
|
|
|
|
|
private Calculator newCalculator(Repository repository) { |
|
Calculator calculator = Calculator.createCalculator(); |
|
if (repository != null) { |
|
calculator.pushNameSpace(ParameterKit.createParameterMapNameSpace(repository.getReportParameterMap())); |
|
calculator.pushNameSpace(SessionKit.asNameSpace(repository.getSessionID())); |
|
} |
|
if (!paraMap.isEmpty()) { |
|
calculator.pushNameSpace(ParameterKit.createParameterMapNameSpace(new HashMap(this.paraMap))); |
|
} |
|
return calculator; |
|
} |
|
@Override |
|
public void readXML(final XMLableReader reader) { |
|
super.readXML(reader); |
|
if (reader.isChildNode()) { |
|
String tagName = reader.getTagName(); |
|
if ("Bucket".equals(tagName)) { |
|
this.bucket = reader.getElementValue(); |
|
} else if ("Directory".equals(tagName)) { |
|
this.setDirectory((OssVariableValue)XmlKit.readXMLable(reader)); |
|
} else if ("FileName".equals(tagName)) { |
|
this.setFileName((OssVariableValue) XmlKit.readXMLable(reader)); |
|
} |
|
} |
|
} |
|
@Override |
|
public void writeXML(final XMLPrintWriter writer) { |
|
writer.startTAG("JavaScript").attr("class", this.getClass().getName()); |
|
super.writeXML(writer); |
|
if (StringKit.isNotEmpty(bucket)) { |
|
writer.startTAG("Bucket").textNode(bucket).end(); |
|
} |
|
if (directory != null) { |
|
XmlKit.writeXMLable(writer, directory, "Directory"); |
|
} |
|
if (fileName != null) { |
|
XmlKit.writeXMLable(writer, fileName, "FileName"); |
|
} |
|
writer.end(); |
|
} |
|
|
|
@Override |
|
public boolean equals(Object o) { |
|
return o instanceof OssDownloadHyperlink |
|
&& AssistKit.equals(((OssDownloadHyperlink) o).bucket, bucket) |
|
&& AssistKit.equals(((OssDownloadHyperlink) o).directory, directory) |
|
&& AssistKit.equals(((OssDownloadHyperlink) o).fileName, fileName); |
|
} |
|
|
|
@Override |
|
public Object clone() throws CloneNotSupportedException { |
|
OssDownloadHyperlink cloned = (OssDownloadHyperlink) super.clone(); |
|
if (directory != null) { |
|
cloned.directory = (OssVariableValue) directory.clone(); |
|
} |
|
if (fileName != null) { |
|
cloned.fileName = (OssVariableValue) fileName.clone(); |
|
} |
|
return cloned; |
|
} |
|
}
|
|
|