[mapguide-users] Layer Filter
sairam
ramsai1973 at GMAIL.COM
Tue Feb 17 00:46:55 EST 2009
I have managed code for Layer Filter task.
I passing a filter condition as parameter for the selected layer and I get
the filtered layer on the map.
But the problem I persist is the layer definition xml is getting permanently
altered even though I make a copy to session repository before altering the
layer definition and use the session layer definition for altering it with
the filter condition.
I couldn't trace out where I have went wrong in my code.
Given below is the code:
public String SetFilter(MgResourceService resourceService,
MgResourceIdentifier ResID, String sNodeToFind, String QueryString)
{
String sNewFilter = "";
// Load the mapguide resource into an xml DOM document
XmlDocument doc = loadResourceXML(resourceService, ResID);
XmlElement baseRootNode = doc.DocumentElement;
//Select all nodes
bool bChanged = false;
XmlNodeList nodeList = baseRootNode.SelectNodes(sNodeToFind);
//Update if it exists
if (nodeList.Count != 0)
{
for (int i = 0; i < nodeList.Count; i++)
{
XmlNode node = nodeList.Item(i);
String sOldString = node.FirstChild.Value;
node.FirstChild.Value = sOldString.Replace(sOldString,
QueryString);
sNewFilter = node.FirstChild.Value;
bChanged = true;
}
}
//Create a New Filter Node if it does not exists
if (nodeList.Count == 0)
{
XmlNode FilterNode = doc.CreateNode(XmlNodeType.Element,
"Filter", string.Empty);
FilterNode.InnerText = QueryString;
XmlElement rootNode = doc.DocumentElement;
XmlNodeList nodelist1 =
doc.DocumentElement.GetElementsByTagName("LegendLabel");
for (int i = 0; i < nodelist1.Count; i++)
{
XmlNode node = nodelist1.Item(i);
XmlNode FinalNode = node.ParentNode;
FinalNode.InsertAfter(FilterNode, node);
bChanged = true;
sNewFilter = FilterNode.InnerText;
}
}
if (bChanged)
{
MgByteSource byteSrc = ByteSourceFromXMLDoc(doc);
resourceService.SetResource(ResID, byteSrc.GetReader(), null);
}
return sNewFilter;
}
public XmlDocument loadResourceXML(MgResourceService resourceService,
MgResourceIdentifier ResID)
{
// Get the content of the resource
string xmlDef =
resourceService.GetResourceContent(ResID).ToString();
byte[] byteDef = new byte[xmlDef.Length];
int iByteCount = Encoding.UTF8.GetBytes(xmlDef, 0, xmlDef.Length,
byteDef, 0);
// Load the content into an XMLDOM object
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.PreserveWhitespace = true;
xmlDoc.LoadXml(xmlDef);
return xmlDoc;
//throw new System.NotImplementedException();
}
public MgByteSource ByteSourceFromXMLDoc(XmlDocument xmlDoc)
{
//Save the amended DOM object to a memory stream
MemoryStream XmlStream = new MemoryStream();
xmlDoc.Save(XmlStream);
//Now get the memory stream into a byte array that can be read into
an MgByteSource
byte[] byteNewDef = XmlStream.ToArray();
String sNewDef = new String(Encoding.UTF8.GetChars(byteNewDef));
byte[] byteNewDef2 = new byte[byteNewDef.Length - 1];
int iNewByteCount = Encoding.UTF8.GetBytes(sNewDef, 1,
sNewDef.Length - 1, byteNewDef2, 0);
MgByteSource byteSource = new MgByteSource(byteNewDef2,
byteNewDef2.Length);
byteSource.SetMimeType(MgMimeType.Xml);
return byteSource;
}
public void SetResourceValue(MgResourceService resourceService,
MgResourceIdentifier ResID, String sNodeToFind, String sNewValue)
{
// Load the mapguide resource into an xml DOM document
XmlDocument doc = loadResourceXML(resourceService, ResID);
XmlElement baseRootNode = doc.DocumentElement;
//baseRootNode.SelectSingleNode(sNodeToFind).FirstChild.Value =
sNewValue;
//Alternative to above, change all nodes
bool bChanged = false;
XmlNodeList nodeList = baseRootNode.SelectNodes(sNodeToFind);
for (int i = 0; i < nodeList.Count; i++)
{
XmlNode node = nodeList.Item(i);
String sOldString = node.FirstChild.Value;
//swap the session layer resource id for the original resource id
in the runtime map
node.FirstChild.Value = sOldString.Replace(sOldString,
sNewValue);
bChanged = true;
}
// create an MgByteSource from the XML DOM document and commit the
changes
if (bChanged)
{
MgByteSource byteSrc = ByteSourceFromXMLDoc(doc);
resourceService.SetResource(ResID, byteSrc.GetReader(), null);
}
}
public bool UpdateFiltersRes(MgResourceIdentifier mapResID, string
QueryString, MgResourceService res, String sessID, MgLayer LayerName)
{
bool bUpdated = false;
string sFilterLayer = LayerName.Name;
String sNewFilter;
XmlDocument docMap = loadResourceXML(res, mapResID);
XmlElement rootNode = docMap.DocumentElement;
XmlNode node = rootNode.SelectSingleNode("//MapLayer[Name='" +
sFilterLayer + "']");
if (node != null)
{
// Get the resource identifier for each layer
XmlNode resourceID = node.SelectSingleNode("ResourceId");
String sLayerDef = resourceID.FirstChild.Value;
MgResourceIdentifier layerResID = new
MgResourceIdentifier(sLayerDef);
// Create a copy in the Session repository and update the filter
MgResourceIdentifier sessionLayerResID =
CreateSessionResource(res, layerResID, sessID);
////Calling SetFilter Function here with Session
Copy(sessionLayerResID)
sNewFilter = SetFilter(res, sessionLayerResID, "//Filter",
QueryString);
if (sNewFilter != "" && sNewFilter != null)
{
bUpdated = true;
////////////////////// save or Alter the map with the
amended session layer definition
String sXpath = String.Concat("//MapLayer/ResourceId[. =
\"", layerResID.ToString(), "\"]");
SetResourceValue(res, mapResID, sXpath,
sessionLayerResID.ToString());
}
}
return bUpdated;
}
Please help me out on this regards.
Many Thanks,
Ram
--
View this message in context: http://n2.nabble.com/Layer-Filter-tp2339204p2339204.html
Sent from the MapGuide Users mailing list archive at Nabble.com.
More information about the mapguide-users
mailing list