Hi i have a parent entity say A which has list of child entities say List<B> children.
I need the order of child entities to be maintained since its important for my application.
The way i have done it is using:
https://developers.google.com/appengine/docs/java/datastore/jdo/relationships#Owned_One_to_Many_Relationships
How Ordered Collections Maintain Their Order.
#Persistent
#Element(dependent = "true")
#Order(extensions = #Extension(vendorName="datanucleus", key="list-ordering", value="index ASC"))
private List objects;
Now i add to list using:
newObj.setIndex(0);
for (int i = 0; i < objList.size(); i++) {
objList.get(i).setIndex(i + 1);
}
objList.add(newObj);
Move using:
if (direction.equalsIgnoreCase("up")) {
objList.get(index).setIndex(index - 1);
objList.get(index - 1).setIndex(index);
}
else if (direction.equalsIgnoreCase("down")) {
objList.get(index).setIndex(index + 1);
objList.get(index + 1).setIndex(index);
}
And delete using:
for (int i = index + 1; i < objList.size(); i++) {
objList.get(i).setIndex(i - 1);
}
objList.remove(index);
Is this the right way to do it? Add & Move seem to work. But Delete behaves weirdly. Random objs get deleted and the list is in completely inconsistent state!
GAE: 1.7.2
DataNucleus Enhancer (version 3.1.0.m2)
Remove at an index only makes real sense for an indexed list (i.e the standard JDO List) and you're not using that. When you call that with DataNucleus and an RDBMS an exception is thrown. Obviously GAE didn't get around to such niceties, but then logic would suggest it. Removing something from an ordered list really ought to call remove(Object).
Moving objects around using setting of this index column may work ... at the next time they are read in; the only thing that the "ordering" clause does is order things at the point they are read in.
try to call objList.remove() before adjusting all the indexes.
Related
This feels wrong to me - given a prefix in GCS and knowing my "folders" are consistently named with a long value (e.g. a date in unix time) I want to get the first listing if i was to sort them in descending order. Right now, I only see how to iterate through them all and sort the list:
ListOptions.Builder b = new ListOptions.Builder();
b.setRecursive(false);
b.setPrefix(path);
ListResult result = null;
result = gcsService.list(appIdentity.getDefaultGcsBucketName(), ListOptions.DEFAULT);
List<Long> names = new ArrayList<>();
while (result.hasNext()){
ListItem l = result.next();
String name = l.getName();
logger.info("get top folder" + name);
names.add(Long.valueOf(name));
}
Collections.sort(names);
long topDay = names.get(0);
Maybe a list option i don't see?
If the numbers have the same length, you are looking for the last element on the last page of the results. There is no parameter which reverses result sorting, unfortunately.
If the numbers are not the same length, that's rough. The best way to solve that would probably be to iterate through the options and keep track of the best one you've seen yet, although sorting through them all afterwards also works.
I am looking to get data from the table on http://www.sportinglife.com/greyhounds/abc-guide using jSoup. I would like to put this data into some kind of table within my java program that I can then use in my code.
I'm not too sure how to do this. I have been playing around with jSoup and currently am able to get each cell from the table to print out using a while loop - but obviously can't use this always as the number of cells in the table will change.
Document doc = Jsoup.connect("http://www.sportinglife.com/greyhounds/abc-guide").get();
int n = 0;
while (n < 100){
Element tableHeader = doc.select("td").get(n);
for( Element element : tableHeader.children() )
{
// Here you can do something with each element
System.out.println(element.text());
}
n++;
}
Any idea of how I could do this?
There are just a few things you have to implement to achieve your goal. Take a look on this Groovy script - https://gist.github.com/wololock/568b9cc402ea661de546 Now lets explain what we have here
List<Element> rows = document.select('table[id=ABC Guide] > tbody > tr')
Here we're specifying that we are interested in every row tr that is immediate child of tbody which is immediate child of table with id ABC Guide. In return you receive a list of Element objects that describes those tr rows.
Map<String, String> data = new HashMap<>()
We will store our result in a simple hash map for further evaluation e.g. putting those scraped data into the database.
for (Element row : rows) {
String dog = row.select('td:eq(0)').text()
String race = row.select('td:eq(1)').text()
data.put(dog, race)
}
Now we iterate over every Element and we select content as a text from the first cell: String dog = row.select('td:eq(0)').text() and we repeat this step to retrieve the content as a text from the second cell: String race = row.select('td:eq(1)').text(). Then we just simply put those data into the hash map. That's all.
I hope this example with provided description will help you with developing your application.
EDIT:
Java code sample - https://gist.github.com/wololock/8ccbc6bbec56ef57fc9e
I have this case where I am filtering list into multiple lists based on criteria.
for(SomeObj someObj : someObjs) {
if(StringUtils.equalsIgnoreCase(someObj.getIndicator(), "Y")) {
beansWithY.add(someObj);
} else if(StringUtils.equalsIgnoreCase(someObj.getIndicator(), "N")) {
beansWithN.add(someObj);
} else {
beansWithIndNotValid.add(someObj);
}
}
This looks simple enough, but, I am wondering if this is possible using Lambdaj.
I came across grouping and it can be used like the following, but, it doesn't seem to cover the default scenario.
Group<SomeObj> group = group(listOfSomeObjs, by(on(SomeObj.class).getIndicator()));
After this the result will be the following:
Y group
N group
null group
a group for each and every invalid indicator ( like A, B, C...)
I am wondering if this can be made to work like the for loop mentioned above, if it is Y - go to a list/group, N - go to another list/group, everything else to one group.
You can't achieve this using "group" function. The group method you added in the question is the right way to approach what you need but you won't be able to cover the default case.
If you want to use LambdaJ to do what you want you have to combine filter (or select). Here you have the code for filter:
List<SomeObj> yList = filter(having(on(SomeObj.class).getIndicator(), equalTo("Y")), listOfSomeObjs);
List<SomeObj> nList = filter(having(on(SomeObj.class).getIndicator(), equalTo("N")), listOfSomeObjs);
List<SomeObj> dList = filter(having(on(SomeObj.class).getIndicator(), not(equalTo("Y"))).and(having(on(SomeObj.class).getIndicator(), not(equalTo("N")))), listOfSomeObjs);
Map<String, List<SomeObj>> map = new HashMap<>();
map.put("Y", yList);
map.put("N", nList);
map.put("D", dList);
Hope to help.
Say I have a csv file where one of the columns is unix timestamp. It is not sorted on that, but following those in order could be a useful relationship. When I want that, I could use ORDER BY, but adding relationship pointers should be faster right, and make use of the NOSQL? Do I have to sort this and add the relationship as I ingest, or can I do it after a query?
After I run the first query and get a subset back:
result = engine.execute(START...WHERE...ORDER BY time)
Can I then go through results adding this relationship like:
prev.createRelationshipTo(next, PRECEDES);
I tried two different ways using foreach or iterator and both had runtime errors casting a string to a Node:
for (Map<String, Object>row : result) {
String n = (String) row.values().iterator().next();
System.out.println(n);
}
Iterator<Node> nodes = result.columnAs("n.chosen");
if (nodes.hasNext()) {
Node prev = nodes.next();
while (nodes.hasNext()) {
Node n = nodes.next();
prev.createRelationshipTo(n, null);
prev = n;
}
}
Also, there is the edge case of two rows having the same timestamp. I don't care about the order that is chosen, but I want it to not break the relation chain.
Hi i am getting List object that contains pojo class objects of the table. in my case i have to show the table data in reverse order. mean that, for ex
i am adding some rows to particular table in database when i am added recently, the data is storing at last row in table(in database). here i have to show whole content of the table in my jsp page in reverse order, mean that what i inserted recently have to display first row in my jsp page.
here my code was like,
List lst = tabledate.getAllData();//return List<Table> Object
Iterator it = lst.iterator();
MyTable mt = new MyTable();//pojo class
while(it.hasNext())
{
mt=(MyTable)it.next();
//getting data from getters.
System.out.println(mt.getxxx());
System.out.println(mt.getxxx());
System.out.println(mt.getxxx());
System.out.println(mt.getxxx());
}
Use a ListIterator to iterate through the list using hasPrevious() and previous():
ListIterator it = lst.listIterator(lst.size());
while(it.hasPrevious()) {
System.out.println(it.previous());
}
You cannot use an iterator in this case. You will need to use index based access:
int size = lst.size();
for (int i=size - 1; i >= 0; i --)
{
MyTable mt = (MyTable)lst.get(i);
....
}
Btw: there is no need to create a new MyTable() before the loop. This is an instance that will be thrown away immediately and serves no purpose.