📄 xmla_soap.java
字号:
AxisInfoLoop: while (itAxisInfo.hasNext()) {
SOAPElement eAxisInfo = (SOAPElement) itAxisInfo.next();
name = envelope.createName("name");
String axisName = eAxisInfo.getAttributeValue(name);
int axisOrdinal;
if (axisName.equals("SlicerAxis"))
axisOrdinal = -1;
else
axisOrdinal = iOrdinal++;
handler.handleAxisInfo(axisName, axisOrdinal);
// retrieve the hierarchies by <HierarchyInfo>
name = envelope.createName("HierarchyInfo", "", MDD_URI);
Iterator itHierInfo = eAxisInfo.getChildElements(name);
int hierNumber = 0;
HierInfoLoop: while (itHierInfo.hasNext()) {
SOAPElement eHierInfo = (SOAPElement) itHierInfo.next();
name = envelope.createName("name");
String hierName = eHierInfo.getAttributeValue(name);
handler.handleHierInfo(hierName, axisOrdinal, hierNumber++);
} // HierInfoLoop
} // AxisInfoLoop
// for each axis, get the positions (tuples)
name = envelope.createName("Axes", "", MDD_URI);
SOAPElement eAxes = selectSingleNode(eRoot, name);
if (eAxes == null)
throw new OlapException("Excecute result has no Axes element");
name = envelope.createName("Axis", "", MDD_URI);
Iterator itAxis = eAxes.getChildElements(name);
AxisLoop: for (iOrdinal = 0; itAxis.hasNext();) {
SOAPElement eAxis = (SOAPElement) itAxis.next();
name = envelope.createName("name");
String axisName = eAxis.getAttributeValue(name);
int axisOrdinal;
if (axisName.equals("SlicerAxis"))
axisOrdinal = -1;
else
axisOrdinal = iOrdinal++;
handler.handleAxis(axisName, axisOrdinal);
name = envelope.createName("Tuples", "", MDD_URI);
SOAPElement eTuples = selectSingleNode(eAxis, name);
if (eTuples == null)
continue AxisLoop; // what else?
name = envelope.createName("Tuple", "", MDD_URI);
Iterator itTuple = eTuples.getChildElements(name);
// loop over tuples
int positionOrdinal = 0;
TupleLoop: while (itTuple.hasNext()) {
SOAPElement eTuple = (SOAPElement) itTuple.next();
handler.handleTuple(axisOrdinal, positionOrdinal);
// loop over members
//XMLA_Member[] posMembers = new XMLA_Member[axis.getNHier()];
int index = 0;
name = envelope.createName("Member", "", MDD_URI);
Iterator itMember = eTuple.getChildElements(name);
MemberLoop: while (itMember.hasNext()) {
SOAPElement eMem = (SOAPElement) itMember.next();
// loop over children nodes
String uName = null;
String caption = null;
String levUname = null;
String displayInfo = null;
Iterator it = eMem.getChildElements();
Map otherProps = new HashMap();
InnerLoop: while (it.hasNext()) {
Node n = (Node) it.next();
if (!(n instanceof SOAPElement))
continue InnerLoop;
SOAPElement el = (SOAPElement) n;
String enam = el.getElementName().getLocalName();
if (enam.equals("UName"))
uName = el.getValue();
else if (enam.equals("Caption"))
caption = el.getValue();
else if (enam.equals("LName"))
levUname = el.getValue();
else if (enam.equals("DisplayInfo"))
displayInfo = el.getValue();
else
otherProps.put(enam, el.getValue());
}
handler.handleMember(uName, caption, levUname, displayInfo, otherProps, axisOrdinal,
positionOrdinal, index);
++index;
} //MemberLoop
++positionOrdinal;
} // TupleLoop
} // AxisLoop
// loop over cells in result set
name = envelope.createName("CellData", "", MDD_URI);
SOAPElement eCellData = selectSingleNode(eRoot, name);
handler.handleCellData(); // start cell loop
name = envelope.createName("Cell", "", MDD_URI);
Iterator itSoapCell = eCellData.getChildElements(name);
CellLoop: while (itSoapCell.hasNext()) {
SOAPElement eCell = (SOAPElement) itSoapCell.next();
name = envelope.createName("CellOrdinal", "", "");
String cellOrdinal = eCell.getAttributeValue(name);
int ordinal = Integer.parseInt(cellOrdinal);
name = envelope.createName("Value", "", MDD_URI);
SOAPElement eValue = selectSingleNode(eCell, name);
Object value = null;
if (eValue != null) {
name = envelope.createName("type", "xsi", XSI_URI);
String type = eValue.getAttributeValue(name);
/*
if ( type == null) {
// probably Error
String eCode = "unknown";
String eDescription = "unknown";
name = envelope.createName("Error", "", MDD_URI);
SOAPElement eError = selectSingleNode(eValue, name);
if (eError != null) {
name = envelope.createName("ErrorCode", "", MDD_URI);
SOAPElement eErrorCode = selectSingleNode(eError, name);
if (eErrorCode != null) {
eCode = eErrorCode.getValue();
}
name = envelope.createName("Description", "", MDD_URI);
SOAPElement eErrorDesc = selectSingleNode(eError, name);
if (eErrorDesc != null) {
eDescription = eErrorDesc.getValue();
}
}
throw new OlapException("Error reading Cell: Error Code = " + eCode +
" Description = " + eDescription);
}
*/
if ("xsd:int".equals(type)) {
// value = new Integer(eValue.getValue()); //EGO crash too long
value = new Long(eValue.getValue());
} else if ("xsd:double".equals(type)) {
value = new Double(eValue.getValue());
} else if("xsd:decimal".equals(type)) {
value=new Double(eValue.getValue());
} else {
value = eValue.getValue();
}
}
name = envelope.createName("FmtValue", "", MDD_URI);
SOAPElement eFmtValue = selectSingleNode(eCell, name);
String fmtValue;
if (eFmtValue != null)
fmtValue = eFmtValue.getValue();
else
fmtValue = "";
name = envelope.createName("FontSize", "", MDD_URI);
SOAPElement eFontSize = selectSingleNode(eCell, name);
String fontSize = null;
if (eFontSize != null)
fontSize = eFontSize.getValue();
handler.handleCell(ordinal, value, fmtValue, fontSize);
} // CellLoop
} catch (SOAPException se) {
throw new OlapException(se);
} finally {
if (connection != null)
try {
connection.close();
} catch (SOAPException e) {
// log and ignore
logger.error("?", e);
}
}
}
// dsf
public void executeDrillQuery(String query, String catalog, QueryResultHandler handler)
throws OlapException {
SOAPConnection connection = null;
SOAPMessage reply = null;
try {
connection = scf.createConnection();
SOAPMessage msg = mf.createMessage();
MimeHeaders mh = msg.getMimeHeaders();
mh.setHeader("SOAPAction", "\"urn:schemas-microsoft-com:xml-analysis:Execute\"");
SOAPPart soapPart = msg.getSOAPPart();
SOAPEnvelope envelope = soapPart.getEnvelope();
SOAPBody body = envelope.getBody();
Name nEx = envelope.createName("Execute", "", XMLA_URI);
SOAPElement eEx = body.addChildElement(nEx);
// add the parameters
// COMMAND parameter
// <Command>
// <Statement>select [Measures].members on Columns from Sales</Statement>
// </Command>
Name nCom = envelope.createName("Command");
SOAPElement eCommand = eEx.addChildElement(nCom);
Name nSta = envelope.createName("Statement");
SOAPElement eStatement = eCommand.addChildElement(nSta);
eStatement.addTextNode(query);
Map paraList = new HashMap();
paraList.put("DataSourceInfo", dataSource);
paraList.put("Catalog", catalog);
//dsf : Note the use of tabular format instead of multidimensional. This is crucial
// otherwise the drillthrough will fail
paraList.put("Format", "Tabular");
addParameterList(envelope, eEx, "Properties", "PropertyList", paraList);
msg.saveChanges();
reply = connection.call(msg, url);
if (logger.isDebugEnabled()) {
logger.debug("Reply from Execute");
//reply.getSOAPPart().getContent().
logSoapMsg(reply);
}
// error check
errorCheck(reply);
// process the reply
SOAPElement eRoot = findDrillExecRoot(reply);
// first the column headers
// Note the use of Hashmap for the column names. This is because
// the reponse message can return variable number of data columns depending on whether
// the column data value is null. The hash map is used to store the column name and the
// column position so that when we do the rendering we can map the data to its appropriate column header.
Name name = envelope.createName("row", "", ROWS_URI);
SOAPElement columnHeader = selectSingleNode(eRoot, name);
if (columnHeader == null)
throw new OlapException("Excecute result has no rows element");
Map colNames = new HashMap();
Iterator columnHeaderIt = columnHeader.getChildElements();
int colIdx = 0;
RowHeadLoop : while (columnHeaderIt.hasNext()) {
Object columnHeaderObj = columnHeaderIt.next();
if (columnHeaderObj instanceof SOAPElement) {
String colName = ((SOAPElement)columnHeaderObj).getElementName().getLocalName();
colNames.put(colName,new Integer(colIdx));
colIdx++;
}
}
// handler.setDrillHeader(colNames);
// extract the data for each row
ArrayList drillRows = new ArrayList();
name = envelope.createName("row", "", ROWS_URI);
Iterator rowIt = eRoot.getChildElements(name);
while (rowIt.hasNext()) {
SOAPElement rowElement = (SOAPElement) rowIt.next();
// process the column in each row
Iterator rowDataIt = rowElement.getChildElements();
javax.xml.soap.Text child = (javax.xml.soap.Text)rowDataIt.next();
SOAPElement columnElement = child.getParentElement();
Iterator columnIterator = columnElement.getChildElements();
Map dataRow = new HashMap();
while (columnIterator.hasNext()) {
Object colObject = columnIterator.next();
if (colObject instanceof SOAPElement) {
String colName = ((SOAPElement)colObject).getElementName().getLocalName();
if (!colNames.containsKey(colName)) {
colNames.put(colName,new Integer(colIdx));
colIdx++;
}
String colValue = ((SOAPElement)colObject).getValue();
dataRow.put(colName, colValue );
}
}
drillRows.add(dataRow);
}
handler.setDrillHeader(colNames);
handler.setDrillRows(drillRows);
} catch (SOAPException se) {
throw new OlapException(se);
} finally {
if (connection != null)
try {
connection.close();
} catch (SOAPException e) {
// log and ignore
logger.error("?", e);
}
}
}
/**
* @return provider (Microsoft/SAP/Mondrian)
*/
public int getProvider() {
return provider;
}
/**
* Get provider id from String
*
* @param dataSourceString
* @return provider id from OlapDiscoverer
* @throws OlapException
*/
private int determineProvider(String dataSourceString) throws OlapException {
logger.debug("determineProvider from dataSourceString: " + dataSourceString);
if (dataSourceString == null) {
throw new OlapException("No data source given for determining XML/A OLAP provider");
}
String upperDSString = dataSourceString.toUpperCase();
if (!upperDSString.startsWith("PROVIDER=")) {
throw new OlapException("Malformed data source given for determining XML/A provider");
}
if (upperDSString.startsWith("PROVIDER=SAP")) {
logger.debug("Provider is SAP");
return OlapDiscoverer.PROVIDER_SAP;
} else if (upperDSString.startsWith("PROVIDER=MONDRIAN")) {
logger.debug("Provider is Mondrian");
return OlapDiscoverer.PROVIDER_MONDRIAN;
} else if (upperDSString.startsWith("PROVIDER=MS")) {//not sure if this is needed?
logger.debug("Provider is Microsoft");
return OlapDiscoverer.PROVIDER_MICROSOFT;
} else if (upperDSString.startsWith("PROVIDER=MICROSOFT")) {// return value from MSAS: "Microsoft XML for Analysis"
logger.debug("Provider is Microsoft");
return OlapDiscoverer.PROVIDER_MICROSOFT; //
} else {
logger.error("Error determining provider from: " + dataSourceString);
throw new OlapException("Unexpected data source determining XML/A provider");
}
}
/** Set provider and dataSource from result of discoverDS
*
* @param resMap
* @throws OlapException
*/
private void setProviderAndDataSource(Map resMap) throws OlapException {
if (resMap == null || resMap.size() == 0) {
logger.error("No resource map from Discover Datasource");
throw new OlapException("No resource map from Discover Datasource");
}
String pstr = (String) resMap.get("ProviderName");
//if (provider == 0) {
//logger.debug("Provider was 0");
// Check other providers
if (pstr == null)
throw new OlapException("No ProviderName from Discover Datasource");
provider = determineProvider("Provider=" + pstr);
//}
logger.debug("Provider ID: " + provider);
if (provider == OlapDiscoverer.PROVIDER_SAP) {
String dstr = (String) resMap.get("DataSourceDescription");
if (dstr == null) {
throw new OlapException("No DataSourceDescription from Discover Datasource");
}
dataSource = "Provider=" + pstr + ";Data Source=" + dstr;
} else {
dataSource = (String) resMap.get("DataSourceInfo");
if (dataSource == null) {
throw new OlapException("No DataSourceInfo from Discover Datasource");
}
}
logger.debug("Discover Datasource set: " + dataSource);
}
/**
* Find the Provider type in the DiscoverResponse
*
* @param n
* @return
* @throws OlapException
* @throws SOAPException
*/
private int getProviderFromDiscoverResponse(SOAPEnvelope envelope, SOAPElement e) throws OlapException, SOAPException {
Name name = ((SOAPElement) e).getElementName();
if (!name.getLocalName().equals("DiscoverResponse")) {
throw new OlapException("Not a DiscoverResponse element. Was: " + name.getLocalName());
}
// Look for return/root/row/ProviderName
SOAPElement walker = getDiscoverReturn(envelope, e);
if (walker == null) {
throw new OlapException("Discover result has no DiscoverResponse/return element");
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -