How to add to the object of SelectManyCheckbox selected values? - jsf

I have an object of SelectManyCheckbox. For this object I set available values in the next way:
SelectManyCheckbox checkbox = new SelectManyCheckbox();
List<Double> selected = new ArrayList<Double>();//this will be used for setting selected values
List<SelectItem> items = new ArrayList<SelectItem>();//this will be used as available values
for (SomeObject av : listOfObjects)
{
SelectItem item = new SelectItem(av.getNumericValue(),
av.getValue());
items.add(item);
if (av.getDefault())
{
selected.add(av.getNumericValue());
}
}
UISelectItems uiItems = new UISelectItems();
uiItems.setValue(items);
checkbox.getChildren().add(uiItems);
checkbox.setSelectedValues(new Double[selected.size()]);
But this way of setting of selected values does not work. Maybe someone know there is the source of the problem?

Your Double[] is empty there, try converting the list to the array like this:
checkbox.setSelectedValues(selected.toArray(new Double[selected.size()]));

Related

Adding multiple objects to ArrayList that match a selected string

What I am trying to do: Fill the namesArray with all the objects that match a certain criteria.
The Activity object is made of four strings "category, name, note, time"
The getCategory(activityArray) returns a string which is the selected category, I'm trying to sort the objects where their category matches the selected category into the new namesArray
So this namesArray will end up containing only Activity objects with selected category.
This is my code:
public static void viewActivity(ArrayList<Activity> activityArray) {
String categ = getCategory(activityArray);
String holder;
ArrayList<Activity> namesArray = new ArrayList<Activity>();
for(Activity obj : activityArray) {
holder = obj.getCategory();
if(holder == categ) {
System.out.println(obj.getName());
namesArray.add(obj);
}
}
}
When I run the debugger, first iteration works, and the first object where it's category matches the selected category is added to the namesArray. But then the if statement just seems to stop working, the holder String does not change and stays the same.
So how do I get my method to add every matching object to the namesArray instead of only the first one?
Thanks
Solved by changing the
if(holder == categ) {
to
if(holder.equals(categ)) {

Value does not fall within the expected range - Exception for SharePoint Lookup Field

I am trying to copy data from one list to other list (both lists are on different sites) along with lookup columns. But, I am getting an error for lookup field as:
Value does not fall within the expected range
Code works and data gets copied for other non-lookup fields. I tried every possible way including increasing List View Lookup Threshold and all possible ways of code but still error persists at ExecuteQuery().
Below is my code for lookup field:
if (field is FieldLookup && field.InternalName == "Country")
{
var CountryLookup = (item.FieldValues["Country"] as FieldLookupValue).LookupValue.ToString();
var CountryLookupId = (item.FieldValues["Country"] as FieldLookupValue).LookupId.ToString();
FieldLookupValue flvRDS = new FieldLookupValue();
flvRDS.LookupId = int.Parse(CountryLookupId);
itemToCreate["Country"] = flvRDS;
itemToCreate.Update();
destContext.ExecuteQuery();
}
Help is really appreciated.
I assume item is the new ListItem you're trying to create on your target list.
But you're never in fact reading any value from field here! So basically, you're trying to set your new FieldLookup.LookupId with the item["Country"].LookupId, which should logically be empty at this moment.
Here's a method I use to retrieve a lookup field ListItem from a value, feel free to modify it to fit your need, since I don't know how you want to retrieve it (SPList is an alias for Microsoft.SharePoint.Client.List).
private ListItem GetLookupItem(FieldLookup lookupField, string lookupValue)
{
string mappingField = lookupField.LookupField;
Microsoft.SharePoint.Client.List lookupList = Context.Web.Lists.GetById(new Guid(lookupField.LookupList));
Context.Load(lookupList);
Context.ExecuteQuery();
ListItemCollection libListItems = lookupList.GetItems(CamlQuery.CreateAllItemsQuery());
Context.Load(libListItems, items => items.Include(
itemlookup => itemlookup.Id,
itemlookup => itemlookup[mappingField]));
Context.ExecuteQuery();
foreach (ListItem mappedItem in libListItems)
{
object mappedField = mappedItem[mappingField];
if (mappedField != null && mappedField.ToString().Equals(lookupValue))
return mappedItem;
}
return null;
}
Now that you have the corresponding ListItem, you can set your item.LookupId with its Id:
if (field is FieldLookup && field.InternalName == "Country")
{
FieldLookupValue flvRDS = new FieldLookupValue();
flvRDS.LookupId = GetLookupItem(field as FieldLookup, "France").Id; // here, dunno how you get your country's name
itemToCreate["Country"] = flvRDS;
itemToCreate.Update();
destContext.ExecuteQuery();
}
Feel free to add some more previous code if you want an answer more suited for your specific issue.

jxls collection printing order

This is my excel template. Unfortunately, employee.email is ArrayList and can have more than one value.
The result becomes
Actually, aaa1.gmail.com and aaa2.gmail.com belong to AAA and similarly, bbb1.gmail.com and bbb2.gmail.com belong to BBB. The excel output is very misleading in this case.
Is it possible to get as below?
Since I am very new to jxls, any help is really appreciate.
First you need to have a POJO like below to hold the data.
public class Employee {
private int id = 0;
private String name = null;
private List<String> mails = new ArrayList<String>();
// getters & setters
}
Now create data objects that you want to display in your excel file
Employee emp1 = new Employee();
emp1.setId(1);
emp1.setName("AAA");
emp1.getMails().add("aaa1#xyz.com");
emp1.getMails().add("aaa2#xyz.com");
Employee emp2 = new Employee();
emp2.setId(2);
emp2.setName("BBB");
emp2.getMails().add("bbb1#xyz.com");
emp2.getMails().add("bbb2#xyz.com");
List<Employee> employees = new ArrayList<Employee>();
employees.add(emp1);
employees.add(emp2);
then add the data into java.util.Map
Map<String, List<Employee>> beanParams = new HashMap<String, List<Employee>>();
beanParams.put("employees", employees);
then create XLSTransformer object and set the source file, destination file and the map which is holding your data
XLSTransformer former = new XLSTransformer();
former.transformXLS(srcFilePath, beanParams, destFilePath);
you need to use two <jx:forEach> tags to iterate over the list which is in your map and then you can set the values into your result excel file.
Change your excel template file with the following
and the final result will look like

SharePoint 2010 - how to make multi-column a unique value

i'm implementing a sand box solution where is should have more than one column a unique key, i have to use the item adding event receiver but how to get the current adding item field values to know if this item is occurred within the list.
thanks
Create UniqueID column and make it unique.
Create an event receiver as follows:
public override void ItemAdding(SPItemEventProperties properties)
{
string Name = properties.AfterProperties["Name"].ToString();
string Title = properties.AfterProperties["Title"].ToString();
StringBuilder StringBuilder = new StringBuilder(Name);
StringBuilder.Append("-");
StringBuilder.Append(Title);
properties.AfterProperties["UniqueID0"] = StringBuilder.ToString();
base.ItemAdding(properties);
}

What is the correct way to format SPGridView values being displayed?

Problem
As we know, SharePoint saves data in database in plain text. Some fields even have concatenated strings like <id>;#<value> for user fields. Percents are saved as doubles (1.00000000000000 for 100%) and etc.
Ofcourse, I want to display data as they are displayed in lists.
What should I do?
Should I use derived SPBoundField to format values (Which I actually did and it works fine until you want to filter (probably SPBoundField won't format me values because i use ObjectDataSource not list and with reflector I saw if there are SPListItems in datasource, then it formats correctly. Not my case)
alt text http://img199.imageshack.us/img199/2797/ss20090820110331.png
Or must I loop through all the DataTable and format each row accordingly?
What are Your techniques?
Thank you.
Here is how I solved this issue.
<asp:TemplateField HeaderText="Campaign Members">
<ItemTemplate>
<%# RemoveCharacters(Eval("CampaignMembers").ToString())%>
</ItemTemplate>
</asp:TemplateField>
// Make sure declare using System.Text.RegularExpression;
protected string RemoveCharacters(object String)
{
string s1 = String.ToString();
string newString = Regex.Replace(s1, #"#[\d-];", string.Empty);
newString = Regex.Replace(newString, "#", " ");
return newString.ToString();
}
I normaly use ItemTemplates that inherit from ITemplate. With in the ItemTemplate I use the SPFieldxxxValue classes or some custom formating code. This saves looping through the DataTable and the ItemTemplates can be reused.
The ItemTemplates are attached in Column Binding
E.G
// Normal Data Binding
SPBoundField fld = new SPBoundField();
fld.HeaderText = field.DisplayName;
fld.DataField = field.InternalName;
fld.SortExpression = field.InternalName;
grid.Columns.Add(fld);
// ItemTemplate Binding
TemplateField fld = new TemplateField();
fld.HeaderText = field.DisplayName;
fld.ItemTemplate = new CustomItemTemplateClass(field.InternalName);
fld.SortExpression = field.InternalName;
grid.Columns.Add(fld);
An example of a ItemTemplate
public class CustomItemTemplateClass : ITemplate
{
private string FieldName
{ get; set; }
public CustomItemTemplateClass(string fieldName, string formatString)
{
FieldName = fieldName;
}
#region ITemplate Members
public void InstantiateIn(Control container)
{
Literal lit = new Literal();
lit.DataBinding += new EventHandler(lit_DataBinding);
container.Controls.Add(lit);
}
#endregion
void lit_DataBinding(object sender, EventArgs e)
{
Literal lit = (Literal)sender;
SPGridViewRow container = (SPGridViewRow)lit.NamingContainer;
string fieldValue = ((DataRowView)container.DataItem)[FieldName].ToString();
//Prosses Filed value here
SPFieldLookupValue lookupValue = new SPFieldLookupValue(fieldValue);
//Display new value
lit.Text = lookupValue.LookupValue;
}
}
Here are a few options. I don't know the output of all of them (would be a good blog post) but one of them should do what you want:
SPListItem.GetFormattedValue()
SPField.GetFieldValue()
SPField.GetFieldValueAsHtml()
SPField.GetFieldValueAsText()
It may also be handy to know that if you ever want to make use of the raw values then have a look at the SPField*XYZ*Value classes. For example the form <id>;#<value> you mention is represented by the class SPFieldUserValue. You can pass the raw text to its constructor and extract the ID, value, and most usefully User very easily.
I would suggest either to format the values before binding them to the spgridview. Linq and an anonymous type is preffered or to call a code behind function on the field that needs the formatting upon binding.
DataField='<%# FormatUserField(Eval("UserFieldName")) %>'
or...maybe a templated field?
After all, i did have not know any other solution to loop through DataTable rows and format them accordingly.
If your SPGridView's data source is list, try out SPBoundField.

Resources