Xpages : How to get all members from a group in java (using ODA) and get their common name - xpages

I thought I was pretty clever writing this method to load up members of a group. But the values that are returned are not in common name format:
How can I easily grab these values in common name or easily transform them to common name?
private void loadNetworkTeam() {
try {
Session session = Factory.getSession();
Database tmpDB = session.getCurrentDatabase();
Database tmpDB2 = session.getDatabase(tmpDB.getServer(), "names.nsf");
View grpView = tmpDB2.getView("($VIMGroups)");
Vector<Object> nTeam = new Vector<Object>();
nTeam.addElement("NetWorkTeam");
Document grpDoc = grpView.getFirstDocumentByKey("Network Team");
Item itm = grpDoc.getFirstItem("members");
networkTeam.addAll(itm.getValues());
} catch (Exception e) {
XspOpenLogUtil.logError(e);
}
}

Use class Name and its method getCommon() to convert a hierarchical name into a common name.
Example:
Item itm = grpDoc.getFirstItem("members");
List<String> networkTeam = new ArrayList<String>();
for (Object value : itm.getValues()) {
networkTeam.add(session.createName((String) value).getCommon());
}

Related

Is there a way to iterate through the fields in a row of a PXResultSet?

Is it possible to use a foreach loop in a BLC to iterate through the fields of a PXResultSet to get the FieldNames?
Is this doable? I can't seem to find a good way.
Thanks...
The PXResultset records are selected from a view. You can get the field names from the View.
Here's a full example:
public class SOOrderEntry_Extension : PXGraphExtension<SOOrderEntry>
{
public override void Initialize()
{
// Get field list from data view
var dataView = new PXSelect<SOOrder>(Base);
string fieldNames = string.Join(",", GetFieldNames(dataView.View, Base.Caches));
// You don't need result set to get field names
PXResultset<SOOrder> resultSet = dataView.Select();
throw new PXException(fieldNames);
}
public string[] GetFieldNames(PXView view, PXCacheCollection caches)
{
var list = new List<string>();
var set = new HashSet<string>();
foreach (Type t in view.GetItemTypes())
{
if (list.Count == 0)
{
list.AddRange(caches[t].Fields);
set.AddRange(list);
}
else
{
foreach (string field in caches[t].Fields)
{
string s = String.Format("{0}__{1}", t.Name, field);
if (set.Add(s))
{
list.Add(s);
}
}
}
}
return list.ToArray();
}
}
When run, this example will show the fields names used in the data view in Sales Order screen SO301000 as an exception.
Field names are contained in Cache object. If you really need to get field names from PXResultset you need to iterate the cache types in the result set.
Example for first DacType (0) of result set:
public class SOOrderEntry_Extension : PXGraphExtension<SOOrderEntry>
{
public override void Initialize()
{
var dataView = new PXSelect<SOOrder>(Base);
PXResultset<SOOrder> resultSet = dataView.Select();
foreach (PXResult result in resultSet)
{
Type dacType = result.GetItemType(0);
foreach (var field in Base.Caches[dacType].Fields)
PXTrace.WriteInformation(field);
}
}
}

Issue getting index field from hazelcast using predicate

I am trying to match a field that is not a key with a remote hazelcast, the goal here is to create many remote instances and use it to store serialized objects.
what i noticed is that if i do both put and SQL in the same run, the return works, as follow :
my class
public class Provider implements Serializable {
private String resourceId;
private String first;
public String getResourceId() {
return resourceId;
}
public void setResourceId(String resourceId) {
this.resourceId = resourceId;
}
public String getFirst() {
return first;
}
public void setFirst(String first) {
this.first = first;
}
}
code :
/*********** map initlization ************/
Config config = new Config();
config.getNetworkConfig().setPublicAddress(host + ":" + port);
instance = Hazelcast.newHazelcastInstance(config);
map = instance.getMap("providerWithIndex");
String first = "XXXX"
/***** adding item ***************/
Provider provider = new Provider();
provider.setResourceId("11111");
provider.setFirst( first);
map.put(provider);
/********** getting items ************/
EntryObject e = new PredicateBuilder().getEntryObject();
Predicate predicate = e.get( "first" ).equal( first ) ;
Collection<Provider> providers = map.values(predicate);
once i run both put and get in different runs, the result is 0 - so to the same code i get no response.
my only thought is that it only does local fetch and not remote. but i hope i have a bug somewhere.
Your code looks good, apart from the fact that you can't do map.put(provider), you need to pass a key, so like map.put(someKey, provider);
The query looks & works fine.

Display a specific result using foreach in Dictionary C#

Problem that I'm unable to solve is that when I search a contact, if found than I need that contact to be the only output but it's displaying whole record. What should I do?
here is my code
namespace Telephone_Directory
{
class Program
{
public bool info()
{
Console.WriteLine("ENTER THE NAME TO BE SEARCHED");
Dictionary<string, uint> contact = new Dictionary<string, uint>();
contact.Add("usman", 03453648729);
contact.Add("Iqtiqa", 03159825052);
contact.Add("Aamir", 03343315412);
contact.Add("Ghous", 03323142783);
var items = from pair in contact
orderby pair.Value ascending
select pair;
string chk = Console.ReadLine();
if (contact.ContainsKey(chk))
{
foreach (KeyValuePair<string, uint> pair in contact)
{
Console.WriteLine("{0}: {1}", pair.Key, pair.Value);
} return true;
}
else
return false;
}
static void Main(string[] args)
{
Program ob =new Program();
bool a=ob.info();
if (a == true)
{
Console.WriteLine("Your contact is found successfully");
}
else
Console.WriteLine("Not found");
Console.ReadLine();
}
}
}
You are checking if the Contact is available but you are iterating over the whole collection as an output. So you can completely remove the first query (var item = ...) because it does not have any effect because you are not using it anywhere.
I would propose accessing everything directly by the Dictionary Key like this
uint srchNumber;
contact.TryGetValue(chk, out srchNumber);
if (srchNumber != null)
Console.WriteLine("{0}: {1}", chk, srchNumber);
this will get you what you want.
But I would suggest to change the implementation because a Dictionary may be not optimal if you will have multiple items with the same name because it only allows you to have every key once. Instead I would suggest to use something like SortedList> or List<...>. Then you may query all possible results with a LINQ-Statement like
var results = from c in contact select c where c.key == chk
this will get you all Key-Value-Pairs belonging to a certain name.
But it is a matter of what you are trying to achieve.

Sharepoint search fails when using DataKeynames

We have a Sharepoint site which uses search.
We get the following error:
Unable to validate data. at
System.Web.Configuration.MachineKeySection.EncryptOrDecryptData
(Boolean fEncrypt, Byte[] buf, Byte[] modifier, Int32 start, Int32 length,
IVType ivType, Boolean useValidationSymAlgo)
at System.Web.UI.ObjectStateFormatter.Deserialize(String inputString)
After a bit of testing we have found that the error occurs when we use DateKeyNames on a GridView control.
Not sure why there should be any combination between Search, this error and DataKeyNames.
Any ideas?
Update: Here is some code
[Guid("8891224e-e830-4ffa-afbd-f789583d8d14")]
public class TestErrorGridView : System.Web.UI.WebControls.WebParts.WebPart
{
Control ascxToAdd;
public TestErrorGridView()
{
}
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
}
protected override void CreateChildControls()
{
base.CreateChildControls();
Table test = new Table();
TestBindObject t1 = new TestBindObject() { ID = 1, Name = "Test" };
List<TestBindObject> l1 = new List<TestBindObject>();
l1.Add(t1);
SPGridView testGrid = new SPGridView() { AutoGenerateColumns = false};
BoundField header = new BoundField();
header.DataField = "ID";
BoundField name = new BoundField();
name.DataField = "Name";
testGrid.Columns.Add(header);
testGrid.Columns.Add(name);
testGrid.DataSource = l1;
testGrid.DataBind();
// If you comment out this line search works
testGrid.DataKeyNames = new string[] { "ID" };
this.Controls.Add(testGrid);
base.CreateChildControls();
SPGridView testGrid = new SPGridView() { AutoGenerateColumns = false, EnableViewState=false };
testGrid.DataKeyNames = new string[] { "testid" };
this.Controls.Add(testGrid);
}
}
public class TestBindObject
{
public int ID { get; set; }
public string Name { get; set; }
}
UPDATE
This error occurrs on the developer machines, so it is not realated to machine keys being different on different machines in a cluster.
One of the developers has MOSS installed, he does not get the error. The developers who have just WSS installed get the error.
UPDATE 2
Have added datasource to code
I'm going to throw out a guess here since the code where you set the GridView's datasource is missing as well, but here goes...
It probably has something to do with the order in which you are setting the GridView's properties. My guess is that you need to set it in this order:
Create your GridView
Set the GridView's datasource (so that it has data)
Set DataKeyNames
Call GridView.DataBind()
Step 3 and 4 are the steps I am not sure about. You may have to DataBind and then set DataKeyNames.
We eventually solved this by following the example in this link:
http://msdn.microsoft.com/en-us/library/bb466219.aspx
I think that is was binding to a data table rather than a list that made the difference.
The fact that Sharepoint grids do not allow autogenerated columns appears to be one of the factors leading to this problem.

Subsonic - Return only certain columns/properties for display

I have a baseclass that handles returning data from the relevant class/table.
I want to have some way of specifying the columns to display. Maybe have a public columns List that gets assigned with all column we want to display?
This is what I have but its not right.
public void SetupGrid<T>() where T : class, new()
{
var db = new MyApp.MyDB();
IRepository<T> repo = new SubSonicRepository<T>(db);
var s = repo.GetAll();
var x = from c in s select new { c.Columns //that match columns I specify };
}
This seems to do it, however unsure if its best practice:
public virtual void SetupGrid<T>() where T : class, new()
{
MyApp.MyDBdb = new MyApp.MyDB();
IRepository<T> repo = new SubSonicRepository<T>(db);
ITable table = repo.GetTable();
List<string> list = new List<string>();
list.Add("CreatedOn");
list.Add("PageID");
list.Add("CreatedBy");
var s = db.SelectColumns(list.ToArray()).
From(table.Name).
OrderAsc(table.Descriptor.Name).ExecuteReader();
bindingSource1.DataSource = s;
}

Resources