IndexOutOfBoundsException when migrating from Java6 to Java8 - primefaces

We are currently migrating our application from Java6 to Java8.We use primefaces-4.0 and JSF 2.2 version.
Following is the code:
// Iterate over columns.
for (int i = 0; i < dynamicList.get(0).size(); i++) {
// Create <h:column>.
column = new Column();
// Create <h:outputText value="dynamicHeaders[i]"> for <f:facet name="header"> of column.
column.setHeaderText(dynamicHeaders[i]);
//column.setWidth("" + (100 / dynamicHeaders.length));
//dynamicDataTable.getChildren().add(column);
System.out.println("Table after setting values");
System.out.println("List size is:"+dynamicList.size());
System.out.println("First list size is:"+dynamicList.get(0).size());
System.out.println("Elements:"+dynamicList.get(0));
System.out.println(column.getHeaderText());
System.out.println(column.getChildren().get(i)); //Getting IndexOutOfBounds exception in this line
// Create <h:outputText value="#{dynamicItem[" + i + "]}"> for the body of column.
output = new OutputLabel();
output.setValueExpression("value", createValueExpression("#{dynamicItem[" + i + "]}", String.class));
column.getChildren().add(output);
System.out.println("Column information");
System.out.println(output.toString());
System.out.println(output.getValue());
System.out.println(output.getChildren().get(i));
System.out.println(output.getValueExpression("value"));
System.out.println(output.getChildren().size());
System.out.println(output.getFamily());
}
We are generating reports in Excel. When debugging code able to see IndexOutOfBounds Exception(have commented the line). In logs I'm able to print the list values and table is also constructed using primefaces. I'm facing issue only when setting value to the table.This works fine in Java6.
I'm unable to identify where the issue is.Kindly help.

Related

How to programmatically reorder a column in primefaces datatable

I have a datatable with 10 columns which are grouped in 4:4:2 manner. Now the first two groups(of 4) are fixed while the last 2 can be added to any of the groups(single or both at a time) based on a condition.
Is there a way to set indexes for columns so that they can be ordered based on a condition ?
I see that Primefaces reorder using drag n drop gives the kind of result Im looking for except I want the reordering to be programmable and set before the table is displayed.
I had basically the same problem: after allowing the user to rearrange columns, it is easy to save the arrangement into a cookie, but how to restore the order a day later?
As far as I know, there are no primefaces method to this but can be hacked with a little javascript. (I tried with primefaces 6.0)
1) Give all the columns unique styleClasses, like:
styleClass="mystyle col0"
styleClass="mystyle col1"
styleClass="mystyle col2"
...
2) Convert the styleClass strings into valid css selectors, and store the required order of columns as an order of those, like:
".mystyle.col2,.mystyle.col0,.mystyle.col1"
Put it into a cookie, or store and get back as you like.
3) Write a javascript function to rearrange the cells into the required order:
function reorder() {
var o = getCookie('orderString');
var a = o.split(",");
for (var i = a.length - 1; i >= 0; i--) {
$(a[i]).each(function() {
$(this).prependTo($(this).parent())
});
}
}
function getCookie(name) {
var value = "; " + document.cookie;
var parts = value.split("; " + name + "=");
if (parts.length === 2)
return parts.pop().split(";").shift();
}
(Don't use EL expressions to get back the order string from a backing bean, because it evaluates on page render, so won't update if the user rearranges the columns again.)
4) Execute reorder() "every time you need it".
You'll certainly need it on page loads:
$(document).ready(function() {
reorder();
});
but also on paging events, so add something like this:
<p:ajax event="page" oncomplete="reorder();" />
(And perhaps other times I've not met yet.)

Inserting in sql Database while maybe out of range

I have an question. I'm trying to insert some values into an mySql database, but I have a small issue.
for (int i = 0; i < Chauffeurlijst.Count; i++)
{
db.Insert("Insert into route (Voertuigen_ID,Chauffeurs_ID,Planning_ID) VALUES(#voertuigid,#chauffeurid,#planningid", new List<KeyValuePair<string, object>>
{
new KeyValuePair<string, object>("#voertuigid", Voertuigenlijst[i].ID),
new KeyValuePair<string, object>("#chauffeurid", Chauffeurlijst[i].ID),
new KeyValuePair<string, object>("#planningid", planning.ID)
});
}
I have two notable variables: A voertuigenlijst(a list of voertuig) and a chauffeurlijst (a list of chauffeur) But the problem is that it could be that Voertuigenlijst's count is smaller than the count of Chauffeurlijst. It could also be the same. When the chauffeurlijst is smaller the program will ofcourse crash because the last item in the list of voertuigen already has been allocated. What I would like to do is when the voertuigenlijst[i] doesn't exist anymore I want to do i-1. Is there a nice solution for this problem?
You can do this by using another local variable to determine the index for Voertuigenlijst. Basically, you want to check that the index is within your range. If not, then use the last index in your list. For example --
for (int i = 0; i < Chauffeurlijst.Count; i++)
{
var j = i < Voertuigenlijst.Count ? i : Voertuigenlijst.Count - 1;
db.Insert("Insert into route (Voertuigen_ID,Chauffeurs_ID,Planning_ID) VALUES(#voertuigid,#chauffeurid,#planningid",
new List<KeyValuePair<string, object>>
{
new KeyValuePair<string, object>("#voertuigid", Voertuigenlijst[j].ID),
new KeyValuePair<string, object>("#chauffeurid", Chauffeurlijst[i].ID),
new KeyValuePair<string, object>("#planningid", planning.ID)
});
}
This will use the last Voertuig in the list if Chauffeurlijst.Count > Voertuigenlijst.Count. However, this code does not handle the case where Voertuigenlijst.Count > Chauffeurlijst.Count. I'm sure you can figure out a good solution using this example though. You may also want to handle the case where one is empty.

How to generate simple schema from database in meteor

How can we generate a schema from the database in meteor app.
I want to generate multiple schemas from each database entry.
The DB used is Mongo DB.
This schema will be used later to generate a form.
I am using autoform to generate a form.
[1: http://autoform.meteor.com]
I've written a small script that you can run in mongo to reverse engineer an existing (flat) collection.
/*
** SimpleSchema definition generator
**
** This will reverse engineer a flat collection
** only at this point. If you improve this,
** please share: {"email-left": "timstew", "at":"#", "email-right": "gmail.com"}
**
*/
var schemaName = "publisherSchema"; // Name you want to give to your simple schema
var collectionName = "publishers"; // mongodb collection name (not including 'db.')
var sampleID = "54c00f0d2b21500370a2e4c4"; // _id of a good representative document
// OK, that's all the info we need - Let's get started!
var message = eval("db." + collectionName + ".findOne({_id:\"" + sampleID +"\"})");
var count = 0;
// Hack because I can't figure out how to find out how many fields are in
var numKeys = 0;
for(var key in message) {numKeys += 1}
var index = 0;
for (var key in message) {
if (index == 0) {
print(schemaName + " = new SimpleSchema({");
}
print("\t" + key + ": {");
print("\t\ttype: " + toProper(eval("typeof db." + collectionName + ".findOne({_id:\"" + sampleID + "\"})." + key)) + ",");
print("\t\tlabel: \"" + toProper(key) + "\"");
if (index == numKeys-1) {
print("\t\t}");
print("\t})");
} else {
print("\t\t},");
}
index += 1;
}
function toProper(str)
{
return str.replace(/\w\S*/g, function(txt){return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase();});
}
MongoDB is a document database, which doesn't require a schema. All you need to do is declare collections in Meteor
If you want to be strict about the document structure, check out https://github.com/aldeed/meteor-collection2 (meteor add aldeed:collection2)
I've recently explained Meteor collections and schemas here.
If you want to read the schema from a collection, you can simply access it via collectionName.simpleSchema(). For your purpose you can take this schema and convert it into your desired structure (or exclude certain configured fields).

Trouble accessing Array Elements

I'm creating this array.
var GPA_Array:Array=new Array();
var dg:DataGrid= new DataGrid();
gpaBuild();
function gpaBuild()
{
dg.columns=["Num","Course","Grade","Credits"];
GPA_Array.push({Num:"1",Course:"ADS",Grade:"A+",Credits:"4"});
GPA_Array.push({Num:"1",Course:"ADD",Grade:"A+",Credits:"4"});
dg.dataProvider=new DataProvider(GPA_Array);
}
after pushing data in the array ,i need to accees Grade and credits.
I have tried this method,
GPA_Array[0][1],GPA_array[0][2] ,
but it didn't work.
If i try to trace it
trace(GPA_Array[0][1])
it gives me undefined .
also ,when i use trace(GPA_array.toString), it gives me error.
Your push() method appears to be pushing an object into your array, so GPA_Array[0][1] will likely throw an exception. Treating each item in the array as an object and using object notation, you should be able to access it with something like:
Object gpaEntry = GPA_Array[0];
trace("gpaEntry {Num:" + gpaEntry.Num + ",Course:" + gpaEntry.Course + ",Grade:" + gpaEntry.Grade + ",Credits:" + gpaEntry.Credits + "});

Flex requests by URLLoader not being well received on server side

today's question involves URLLoader requests using encrypted strings.
when I encrypt a string I get the following result:
1Kx4dfp5OC7ox0zb0lWzzzlnoPLcoPGE1MrAKOtl3h6SPcFmEdpLnUROSKpPrCl70VHRxrKzhsxHHlb1MRp3++JkvYZ++ghBEG2zbVhyaqQ/0+NDrJ+0cLt3g9THe9POohN6Ufcq9TcnmZVvIFXllg4HrjVNfQrhQCNwxuBgWBf2DRc4eq6hKzEgyLdlllQFc9ssUFlPD3wOBqoI22r+7N82sI3pqsQYBq5VlKHHreqD8Cq0gictnTFS3IqepASGARKyuCIPDCa4zE76VeQV5zgvkFfjDww+C1uZ8PUgjH67DKYqUP9a6euf2v1jUpBrREnm4ZbLAXScDjvrJ11rWYyVXOLZy9nhy9qRBQRvdw+tnBThPTmvxaq+LAusF8IbvDpZgMrZ3buvThnXuSBGXZxaja7fk/FIlm4RSliDTSGySiizFHy7dJePXuV0c9MI6ciOYxmEIg64NnhBZtB8wipUDJWOpoytOD2/sNQBenjZbYN8291msYnbBG+alAOQmEBH5Mn4KyW1VQWE2lBGk9ML+SflND8UXfdHz5Q3psOcMZJxSAURKGq5tjA8KlPPOAdQuVPIcysg2/4lV25QGIdDttQVGrkP+ZHZcHIPTLLD+Vml+PJU/OAJGNPGlf3wawUo+bID0FKur8N6tNyu7Pnoocn7plDi6WSJgUAaYjI4=
I send it in, everything seems fine on Flex's end. But when I go to the serverside (logfiles, not allowed to change server-side code) to check what I'm getting, I end up with this:
1Kx4dfp5OC7ox0zb0lWzzzlnoPLcoPGE1MrAKOtl3h6SPcFmEdpLnUROSKpPrCl70VHRxrKzhsxHHlb1MRp3 JkvYZ ghBEG2zbVhyaqQ/0 NDrJ 0cLt3g9THe9POohN6Ufcq9TcnmZVvIFXllg4HrjVNfQrhQCNwxuBgWBf2DRc4eq6hKzEgyLdlllQFc9ssUFlPD3wOBqoI22r 7N82sI3pqsQYBq5VlKHHreqD8Cq0gictnTFS3IqepASGARKyuCIPDCa4zE76VeQV5zgvkFfjDww C1uZ8PUgjH67DKYqUP9a6euf2v1jUpBrREnm4ZbLAXScDjvrJ11rWYyVXOLZy9nhy9qRBQRvdw tnBThPTmvxaq LAusF8IbvDpZgMrZ3buvThnXuSBGXZxaja7fk/FIlm4RSliDTSGySiizFHy7dJePXuV0c9MI6ciOYxmEIg64NnhBZtB8wipUDJWOpoytOD2/sNQBenjZbYN8291msYnbBG alAOQmEBH5Mn4KyW1VQWE2lBGk9ML SflND8UXfdHz5Q3psOcMZJxSAURKGq5tjA8KlPPOAdQuVPIcysg2/4lV25QGIdDttQVGrkP ZHZcHIPTLLD Vml PJU/OAJGNPGlf3wawUo bID0FKur8N6tNyu7Pnoocn7plDi6WSJgUAaYjI4=
at first glance they're the same, but if you check closely, the + gets replaced by a whitespace...
I've even tried switching the + for %2B but on the server-side it gets read as %2B, it isn't converted to a + (flex doesn't seem to function as a browser in this case).
Any kind of insight and help on this matter would be very appreciated.
The requests are being done as follows:
public function callService(callback:String, request:String):void{
var url:URLRequest = new URLRequest(server);
var requestedString:String = handlePluses(request);
url.useCache = false;
url.contentType = contentType;
url.method = method;
trace("sending: " + requestedString);
url.data += requestedString);
serverURL.addEventListener(IOErrorEvent.IO_ERROR, treatIO);
serverURL.dataFormat = URLLoaderDataFormat.TEXT;
serverURL.addEventListener(Event.COMPLETE, loadData);
serverURL.addEventListener(Event.CONNECT, function():void{trace("connected");});
try{
serverURL.load(url);
}catch(e:ArgumentError){trace("ArgError: " + e.message);}
catch(e:SecurityError){trace("SecError: " + e.message);}
catch(e:TimeoutEvent){trace("===========<Timeout>===========");}
}
we fixed this problem by switching the + character with a subset of escaped characters like \&\#.
this might be a problem to others attempting the same thing and trying to keep to a minimum size.