how to get array values of a node property in jcr - jcr

Need help in getting the string[] values of node property??
for example I have a node image which has property "references" of type String[] . I need to get the first value of array.
Thanks

From the Node, you can get the references property. And then call getValues to the reference values. From there, just take the first. Something like
public String getFirstReference(Node node) throws RepositoryException {
Property references = node.getProperty("references");
Value[] values = references.getValues();
return values[0].getString();
}

Property nProp = node.getProperty("references");
Value[] values = propertyNode.getValues();
for (Value v : values) {
System.out.println("Property Name = "+nProp.getName()+" ; Property Value= "+v.getString());
}

Related

Iterating through a hashmap and changing the values using reflection

So I'm coding a gene algorithm and ideally I start off by placing some float variables in a hashmap. These will be the genes that are passed on to different objects.
ie
genes.put("mass", 200);
genes.put("lifespan",0);
genes.put("intelligence",0);
genes.put("dexterity",0);
genes.put("endurance",0);
then later I want hashmap values to be updated when there is a change in the variable. This is the code I have so far;
if(updategenes){
for ( String key : genes.keySet() ) {
System.out.println( key );
try{
String a = key;
Field field = a.getClass().getField(key);
genes.put(key,field);
}catch (NoSuchFieldException e) {
}catch (IllegalAccessException e) {
}}
updategenes = false;
}
only issue is it wont accept field as an int even though thats the only possible type that could ever be in field at the time. Any suggestions?
int strValue = (int) field.get (key);
How to get string value from a Java field via reflection?

Creating a new set from components of an existing set in OPL/CPLEX

I have a set that I have read into my OPL project that looks like this:
S = {<"A","">, <"B","">, <"AB","A">, <"AB","B">, <"C","">, <"ABC","A">,<"ABC","B">, <"ABC","C">, <"ABC","AB">},
where each element <,> is a tuple with two string elements. This set represents parent-child relationships between items of interest.
From this set I need to create a new set:
S' = {<"A",{""}>, <"B",{""}>, <"C",{""}>, <"AB",{"A","B"}>, <"ABC",{"A","B","C","AB"}>},
where each element <,> is a tuple with the first element of each tuple a string and the second element of each tuple a set of strings. My attempt at creating this set is:
tuple child{
string Item;
string Child;
}
{child} Children = ...; //Reads in the set S
tuple dependents{
string Item;
{string} ItemChildren;
}
{dependents} dependentsSet = {<x.Item, y.Child> | x in Children, (y in Children : <x,y> in Children)};
Using the variable names from the above code, the purpose of creating S' is because later in my program I need to create a collection of constraints, one for each Item, and within each constraint I need to index over the ItemChildren. I'm a relative novice with OPL so I know I'm using the syntax incorrectly in the initialization of the dependentsSet variable, but I don't know how to write this statement correctly such that it creates the set I'm looking for.
Can anyone help me understand the statements required to create the set I'm after?
tuple child{
string Item;
string Child;
}
{child} Children
= {<"A","">, <"B","">, <"AB","A">, <"AB","B">, <"C","">, <"ABC","A">,
<"ABC","B">, <"ABC","C">, <"ABC","AB">};
{string} setOfParents={i.Item | i in Children};
{string} setOfChildren={i.Child | i in Children};
tuple dependents{
string Item;
{string} ItemChildren;
}
{string} kids[p in setOfParents]={k | k in setOfChildren : <p,k> in Children};
{dependents} dependentsSet = {<x, kids[x]> | x in setOfParents};
execute
{
writeln(dependentsSet);
}
gives
{<"A" {""}> <"B" {""}> <"AB" {"A" "B"}> <"C" {""}>
<"ABC" {"A" "B" "C" "AB"}>}

SPFieldUserValue : Value does not fall within the expected range

Following piece of code returning following error not sure where is the problem,
Exception Details: System.ArgumentException: Value does not fall within the expected range.
public static SPUser convertStringToSPUser(string struser)
{
SPFieldUserValue userValue = new SPFieldUserValue(SPContext.Current.Web, struser);
SPUser objSPUser = userValue.User;
return objSPUser;
}
My string is something like, "S, Rishi".
In order to create an instance of SPFieldUserValue you should Pass a string with the following format:
[int];#[domain]\[username]
The easiest way to get that value from a list item is:
ListItem[field_GUID].ToString();

Get a value from array based on the value of others arrays (VB.Net)

Supposed that I have two arrays:
Dim RoomName() As String = {(RoomA), (RoomB), (RoomC), (RoomD), (RoomE)}
Dim RoomType() As Integer = {1, 2, 2, 2, 1}
I want to get a value from the "RoomName" array based on a criteria of "RoomType" array. For example, I want to get a "RoomName" with "RoomType = 2", so the algorithm should randomize the index of the array that the "RoomType" is "2", and get a single value range from index "1-3" only.
Is there any possible ways to solve the problem using array, or is there any better ways to do this? Thank you very much for your time :)
Note: Code examples below using C# but hopefully you can read the intent for vb.net
Well, a simpler way would be to have a structure/class that contained both name and type properties e.g.:
public class Room
{
public string Name { get; set; }
public int Type { get; set; }
public Room(string name, int type)
{
Name = name;
Type = type;
}
}
Then given a set of rooms you can find those of a given type using a simple linq expression:
var match = rooms.Where(r => r.Type == 2).Select(r => r.Name).ToList();
Then you can find a random entry from within the set of matching room names (see below)
However assuming you want to stick with the parallel arrays, one way is to find the matching index values from the type array, then find the matching names and then find one of the matching values using a random function.
var matchingTypeIndexes = new List<int>();
int matchingTypeIndex = -1;
do
{
matchingTypeIndex = Array.IndexOf(roomType, 2, matchingTypeIndex + 1);
if (matchingTypeIndex > -1)
{
matchingTypeIndexes.Add(matchingTypeIndex);
}
} while (matchingTypeIndex > -1);
List<string> matchingRoomNames = matchingTypeIndexes.Select(typeIndex => roomName[typeIndex]).ToList();
Then to find a random entry of those that match (from one of the lists generated above):
var posn = new Random().Next(matchingRoomNames.Count);
Console.WriteLine(matchingRoomNames[posn]);

SharePoint 2010 - Custom calculated column

In a document library I need a custom calculated column, because the default Excel formula don't provide the functionality I need.
I created a custom field inheriting from SPFieldText, that I then could customize at will. The question is: how is it possible, from my custom field, to access the content values of the other fields of the document library?
In other world, in the overriden GetValidatedString method, how can I return a value that is dependent upon values from other fields, for the same record? How to implement getFieldValue() , below:
public class MyCustomField : SPFieldText
{
....
public override string GetValidatedString(object value)
{
string value1 = getFieldValue("Column-Name1");
string value2 = getFieldValue("Column-Name2");
return value1 + ", " + value2; // any arbitrary operation on field values
}
}
Thanks!
You should be able to grab other values from the form using the Item property of the FormComponent or the Item property of the ItemContext.
Either of these should work from the FieldControl class:
Code Snippet
if ((this.ControlMode == SPControlMode.New) || (this.ControlMode == SPControlMode.Edit))
{
object obj = this.Item["Name"];
if (obj != null)
string name = obj.ToString();
object obj2 = base.ItemContext.Item["Name"];
if (obj2 != null)
string name2 = obj2.ToString();
}
where "Name" is the internal name of the field that you wish to retrieve.