"Query-loop" to get the appropriate value out of the DB - java

So I will have to explain what I'm looking for and I will use an example very general to this so this could help others with the same problem:
Lets assume I'm working at the police station and I'm creating a DB with a table called "Policestations" and the Police of my country has 3 policestations.
Those stations wan't to be as efficient as possible therefore they will pick those incidents if they fullfill some parameters for our example here those 3 policestations:
policeStation:1010
offender:GuyA
place:GB (iso-code)
delict:murder
policeStation:2020
offender:GuyA
place: * (* = "for not relevant")
delict:murder
policeStation:3030
offender:GuyB
place:*
delict:*
So now, I have a method that looks like that (could someone edit this the code thing doesnt work when I add that into it):
public String responsiblePoliceStation(String offender, String place, String delict) {
Query q = em.createQuery("SELECT a.Policestation FROM PoliceStations a WHERE a.offender = :offender AND a.place = :place AND a.delict = :delict );
q.setParameter("offender", offender);
q.setParameter("place", place);
q.setParameter("delict", delict);
String policestation = (String) q.getSingleResult();
return policestation;
}
so now I can call that method for example with that:
PSPRouteService psp = new PSPRouteService();
String pS = psp.responsiblePoliceStation("GuyA", "GB", "murder");
`
and this will work fine, I will get as response that the policeStation with String 1010 has to "solve" this incident.... but as I intended in the header I want a loop so for example thats assume I have that incident:
offender: GuyA
place: Brazil
delict: murder
then this wouldnt work because I dont know how to implement that, if some parameter doesn't fit in then just compare it with the next row and look if the next Policestation is more tolerant and doesn't care for example whether the incident took place.
So I want kinda a query-loop where I compare the given Parameter with my DB and if something doesnt fit I just skip to the next row compare that and if that doesnt fit I keep going to the next and as you see above the third policeStation is more tolerant they dont care, what happend and where they just take the incident if the offender was GuyB...... so any ideas and tipps how to implement that?

Related

How do I select a specific element from a set with similar XPath paths?

There are 2 drop-down lists. Each has a similar meaning, for example, "Jorge". Lists in different modules. When I need to fill in, for example, a list that is lower in the tree, then the first match is taken along the XPath path, on an undisclosed list.
Not lists, but values in drop-down lists!
There are 2 drop-down lists. Each has a similar meaning, for example, "Jorge". Lists in different modules. When I need to fill in, for example, a list that is lower in the tree, then the first match is taken along the XPath path, on an undisclosed list.
Not lists, but values in drop-down lists!
I wanted to implement it in Java this way:
Example:
if (findElement(By.xpath("(//example//example)")).isDisplayed()) {
findElement(By.xpath("(//example//example)")).click();
}
But in this case, the element is not displayed.
How to implement a search of all values similar to the XPath path in order to get the one that is displayed?
I tried to do something like this: (//example//example)1 (//example//example)[2] (//example//example)[3]
In my case, we have that 1 - the element does not exist [2] - exists, but is not displayed (isDisplayed = false) [3] - exists, is displayed (isDisplayed = true)
iterating through the values in the loop for [n] cannot be implemented, because, for example, the value 1 is not.
Described as difficult as possible :D. Excuse me.
If someone understands my nonsense, please help me. How to implement my requirement?
enter image description here
UPD:
The problem was solved (for me) by substituting the first value into the expression ()"{1}" immediately.
Now I'm interested in why I get an exception after the first iteration:
Method threw 'org.openqa.selenium.ElementNotInteractableException' exception.
Code:
int number = 1;
String option = "(//ul[contains(#style, 'display: block')]//li//span[contains(text(),'" + valueField + "') or strong[contains(text(),'" + valueField.toUpperCase() + "')]])";
findElement(By.xpath(option+"["+number+"]"));
String[] words = valueField.split(" ");
StringBuilder builder = new StringBuilder();
for (int i = 0; i < words.length; i++) {
builder.append(words[i]);
setFieldByLabel(nameModule, nameLabel, builder.toString());
fastWaitLoading();
for (int y = 0; y < 10; y++) {
if (findElement(By.xpath(option+"["+number+"]")).isDisplayed()) {
new Actions(browser.getWebDriver())
.moveToElement(findElement(option))
.click()
.build()
.perform();
break;
}
number++;
}
}
So I am trying to fully understand your question, and I don't. What I would recommend for a situation like this is, iterate through all elements by creating a list with: findElements(By.xpath ... )
This way you will get a list of webelements and you can iterate through them. Then apply a foreach, assert if element is displayed (it exists as it has been found with findElements) and you should be able to interact with it.
Yeah, everything is in a prominent place)
Missed it
new actions(browser.getWebDriver()) .moveToElement(findElement(**option**)) .click() .build() .perform(); break;
Here
new actions(browser.getWebDriver())
.moveToElement(findElement(**option + "[" + number+"]"**))
.click()
.build()
.perform();
break;

How to compare two datasets to check for missing rows which don't exist in the other dataset?

I have built a method which takes two datasets: dataset1 and retirementSimpleData.
It matches the two datasets based on a primary key/number, defined as cnum, in the code below.
I wanted to return the value of the difference between the getAssets value and the getSums value, and this is working, except for one little problem.
Some of the cnums that exist in dataset1 don't exist in retirementSimpleData. Similarly, some cnums which may exist in retirementSimpleData may not exist in
dataset1. This is resulting in no data being returned for that cnum.
I would like to implement two passes at the end which check in one direction to see if I missed anything. The second pass would check in the opposite direction.
However, not sure how I would go about implementing this.
public void getReportData(int index) {
String date1 = Util.dateTimeToShortString(reportDate);
String date2 = reportDao.getPreviousRetirementDate(date1);
List<SurveyCompareAssetsCheckData> dataset1 = reportDao.getSurveyCheckCompareAssetsData(date1);
List<RetSurveyAssets> retirementSimpleData = reportDao.findRetSurveyByDate(date1);
for (SurveyCompareAssetsCheckData surveyCompareAssetsCheckData : dataset1) {
for (RetSurveyAssets surveyCompareAssetsCheckData2 : retirementSimpleData) {
if (surveyCompareAssetsCheckData.getCnum() == surveyCompareAssetsCheckData2.getCnum()) {
surveyCompareAssetsCheckData.setRetirementsimple(surveyCompareAssetsCheckData2.getSums());
surveyCompareAssetsCheckData.setDifference(surveyCompareAssetsCheckData.getAssets() - surveyCompareAssetsCheckData2.getSums());
Caveat: dataset1 and retirementSimpledata both use existing SQL pulls which I am not allowed to touch, otherwise I would have simply defined new SQL for these methods in my "DAOImpl." Therefore, I have to work with the data I am getting, and programmatically check for this.
Below, is the report which is being generated with my code. As you can see, I am ending up with zeros, which is showing the difference (incorrectly) as zeros, because Cnum #45, in this example simply doesn't exist in the second dataset (retirementSimpleData)
What is the datatype of Cnum, if it is int then default value is Zero.
You have to add else-if condition to check for example:
else if (surveyCompareAssetsCheckData2.getCnum()== 0){
-------- logic here --------------------
}
else if (surveyCompareAssetsCheckData.getCnum() ==0){
----------logic here -----------
}

Figuring out if statement

So my project is about creating a table that adds statistic comparisons to a selection sort table. I've done the bulk of the work but am stuck at this one part that asks me to It should add "comparisons" to "totalComparisons". It should compare "comparisons" to "minComparisons" and set "minComparisons" to whichever is
smaller. It should also set "maxComparisons" in an analogous fashion. The words in quotes are variables. I know I need to write an if statement but I have no idea how to write it. Can somebody show me how to do the part where you compare "comparisons" to "minComparisons" and set "minComparisons" to whichever is
smaller.
My code so far:
private static void endStatistics(){
totalComparisons += comparisons;
if (comparisons ){
}
thanks for any help.
Try this:
if(comparisons < minComparisons)
minComparisons = comparison;
if (comparisons <= minComparisons ){
minComparisons = comparisons;
}
if (minComparisons <= comparisons ){
minComparisons = minComparisons;
}
You don't actually need the second if statement though, just to put it here to show you the whole picture.

Java jSoup - get data from web tables

I am looking to get data from this webpage: http://www.sportinglife.com/greyhounds/racecards/29-10-2014/belle-vue
I have been using jSoup and Java, but can't seem to get the data I am looking for. I need the times of each race (Jump to: 14:18 14:37 14:57 15:17 15:38 15:58 16:18 16:37 16:57 17:17 17:33 17:47 18:04 18:18) and the link that each of them refers to.
I then need to go to each link and print out the 6 dogs in each race.
So the output would look like:
14:18
1 Golden Light
2 Always Late
3 Redley Rooster
4 Redstone Bo Dhu
5 Ballymac Oprah
6 Ballyhill Slide
For each race.
My current code is below, and uses jSoup to extract the runners from the race - but I can't seem to do the first step of getting the race "times" and links to each race page so I can loop through the links and output the runners for each race.
Document doc = Jsoup.connect(
"http://www.sportinglife.com/greyhounds/racecards/29-10-2014/belle-vue/card/834800").get();
Element tableHeader = doc.select("tbody").first();
Map<String, String> data = new HashMap<>();
for (Element element : tableHeader.children()) {
// Here you can do something with each element
String dog = element.select("td:eq(0)").text();
String race = element.select("td:eq(2)").text();
data.put(dog, race);
System.out.println(dog + " " + race);
}
Any help is very much appreciated.... thanks!
Rob
Looking at your page, the race information are not directly in the second TD but in a link (a) in the second TD, then you need to replace :
String race = element.select("td:eq(2)").text();
with :
String race = element.select("td:eq(2) a").text();

how to select the ages which are less than or greater than from drop down in grails and groovy

I have a requirement that should have one drop down containing some conditions on age.
like less than 10days,between 10 to 30 days,between 1 month to 3 months,between 4 month to 12 months,between 1yr to 2 yr.
I have domain class containing one property age(integer).and i am calculating age form dob to current date and storing in DB.I have search criteria to search based on age in search page,So how can i display these condition vales in drop down and when i select one option how to display the result based on age.
presently i am displaying all ages in drop down form the DB, please find the code and help me in doing this, if its not clear please write the comments so that i can explain u.
this is my drop down contaning all dobs
<td><span id="availableAge" ></span></td>
This is my script to get dobs from controller with an ajax call
function generateAge(data){
var list ="<select style='width:100px' id='age' name='age'><option value=''>-Select-</option>";
var opt;
for(var i=0; i<data.ageDetails.length; i++){
opt = "<option value="+data.ageDetails[i].age+">";
opt = opt+data.ageDetails[i].age;
opt = opt+"</option>";
list = list+opt;
}
list = list+"</select>";
var listObj = document.getElementById("availableAge");
if(listObj){
listObj.innerHTML = list;
}
}
It's a bad idea to store age in DB, as it changes all the time - better stick with DOB.
As the option set is fixed, make something like an enum for it, use its values() to render a select
enum AgeCriteriaEnum { NONE, LESS_THAN_10, BETWEEN_10_AND_30, ... so on }
and just do a switch() like:
AgeCriteriaEnum ageEnum = AgeCriteriaEnum.valueOf(params.ageEnum)
Date today = new Date()
Patient.withCriteria {
switch(ageEnum) {
case AgeCriteriaEnum.NONE:
break;
case AgeCriteriaEnum.LESS_THAN_10:
ge('dob', today-10)
break;
case AgeCriteriaEnum.BETWEEN_10_AND_30:
lt('dob', today-10)
ge('dob', today-30)
break;
//... so on
}
}

Categories