One to Many search using AND condition - java

I have the following product which contain many colors.
I wish to find the product which contain at least RED and GREEN.
Product class
String id;
List<Color> colors{};
Color class
id
color
kindly ignore the syntax error.
I'm able to use the following to search OR condition.
Criteria criteria = createCriteria();
criteria.createAlias("colors","colors");
List<String> colorsList = new LinkedList();
colorsList.add("GREEN");
colorsList.add("RED");
criteria.add(Restriction.in("colors.color",colorsList);
The above will give me products which has red or green in their colors BUT not products which contain at least RED AND GREEN.
Example
Product: RED GREEN - PASS
Product: RED GREEN YELLOW - PASS
Product: RED YELLOW - FAIL
Thanks in advance.

the idea is we select all products with the colors and count each product, then products with both colors should have a count of 2 as the number of colors
DetachedCriteria colorCrit = DetachedCriteria.For(Product.class)
.createAlias("colors","color")
.add(Restriction.eq("color.color", "RED")
.add(Restriction.eq("color.color", "GREEN")
.SetProjection(Projections.Group("id"))
.add(Restriction.eq(Projections.rowCount(), 2));
Criteria criteria = createCriteria()
.add(Subqueries.in("id", colorCrit)
.list();
Update:
there is an issue for hibernate for exactly this. the last comment describes how to use.

Related

If Statement that checks something's background color?

I have a small application that randomizes a background color (red, green or blue) and sets them for 9 backgrounds.
The app also randomizes a color an user has to click to get a point.
My question is, is there any way in Java in AndroidStudio that I can make an if statement that somehow takes the value of an object's background color?
if ( color_user_has_to_pick == background_color) {
point++
}
else {
nothing }
You can try the following way to get the background color of a view and the checking if that matches with user picked color
ColorDrawable viewColor = (ColorDrawable) view.getBackground();
int colorId = viewColor.getColor();
if(someColorId == colorId) {
//....
}
If there's a set number of background colors, I would just compare the resource ids since it's pretty convenient:
if (R.id.blue === ((ColorDrawable) view.getBackground()).getColor()) {
// Your logic
}
But, as an alternative you could disregard the colour comparison and just create a data structure to hold the current values of the game (the state), assign identifiers to these colour values and compare said identifiers.

PDFClown: Creating a TextMarkup leads to an inaccurate Box of the TextMarkup

Im working with PDFClown to analyze and work with PDFDocuments. My aim is to highlight all numbers within a table. For all numbers which belong together (For example: All numbers in one column of a table) I will create one TextMarkup with a List of Quads. First of all it looks like everythink work well: All highlights on the left belong to one TextMarkup and all Highlights on the right belong to another TextMarkup.
But when analyzing the size of the TextMarkup the size is bigger than it looks at the picture. So when drawing for example a rectangle arround the left TextMarkup box the rectangle intersects the other column despite no highlight of the left TextMarkup intersects the other column. Is there a way to optimize the Box of the TextMarkup? I think there is a bulbous ending of the box so that the box is intersecting the other TextMarkup
This is the code which creates the TextMarkup:
List<Quad> highlightQuads = new ArrayList<Quad>();
for (TextMarkup textMarkup : textMarkupsForOneAnnotation) {
Rectangle2D textBox = textMarkup.getBox();
Rectangle2D.Double rectangle = new Rectangle2D.Double(textBox.getX(), textBox.getY(), textBox.getWidth(), textBox.getHeight());
highlightQuads.add(Quad.get(rectangle));
}
if (highlightQuads.size() > 0) {
TextMarkup _textMarkup = new TextMarkup(pagesOfNewFile.get(lastFoundNewFilePage).getPage(), highlightQuads,"", MarkupTypeEnum.Highlight);
_textMarkup.setColor(DeviceRGBColor.get(Color.GREEN));
_textMarkup.setVisible(true);
allTextMarkUps.add(_textMarkup);
}
Here is an example file Example
Thank You !!
Your code is not really self contained (I cannot run it as it in particular misses the input data), so I could only do a bit of PDF Clown code analysis. That code analysis, though, did indeed turn up a PDF Clown implementation detail that would explain your observation.
How does PDF Clown calculate the dimensions of the markup annotation?
The markup annotation rectangle must be big enough to include all quads plus start and end decorations (rounded left and right caps on markup rectangle).
PDF Clown calculates this rectangle as follows in TextMarkup:
public void setMarkupBoxes(
List<Quad> value
)
{
PdfArray quadPointsObject = new PdfArray();
double pageHeight = getPage().getBox().getHeight();
Rectangle2D box = null;
for(Quad markupBox : value)
{
/*
NOTE: Despite the spec prescription, Point 3 and Point 4 MUST be inverted.
*/
Point2D[] markupBoxPoints = markupBox.getPoints();
quadPointsObject.add(PdfReal.get(markupBoxPoints[0].getX())); // x1.
quadPointsObject.add(PdfReal.get(pageHeight - markupBoxPoints[0].getY())); // y1.
quadPointsObject.add(PdfReal.get(markupBoxPoints[1].getX())); // x2.
quadPointsObject.add(PdfReal.get(pageHeight - markupBoxPoints[1].getY())); // y2.
quadPointsObject.add(PdfReal.get(markupBoxPoints[3].getX())); // x4.
quadPointsObject.add(PdfReal.get(pageHeight - markupBoxPoints[3].getY())); // y4.
quadPointsObject.add(PdfReal.get(markupBoxPoints[2].getX())); // x3.
quadPointsObject.add(PdfReal.get(pageHeight - markupBoxPoints[2].getY())); // y3.
if(box == null)
{box = markupBox.getBounds2D();}
else
{box.add(markupBox.getBounds2D());}
}
getBaseDataObject().put(PdfName.QuadPoints, quadPointsObject);
/*
NOTE: Box width is expanded to make room for end decorations (e.g. rounded highlight caps).
*/
double markupBoxMargin = getMarkupBoxMargin(box.getHeight());
box.setRect(box.getX() - markupBoxMargin, box.getY(), box.getWidth() + markupBoxMargin * 2, box.getHeight());
setBox(box);
refreshAppearance();
}
private static double getMarkupBoxMargin(
double boxHeight
)
{return boxHeight * .25;}
So it takes the bounding box of all the quads and adds left and right margins each as wide as a quarter of the height of this whole bounding box.
What is the result in your case?
While this added margin width is sensible if there is only a single quad, in case of your markup annotation which includes many quads on top of one another, this results in a giant, unnecessary margin.
How to improve the code?
As the added caps depend on the individual caps and not their combined bounding box, one can improve the code by using the maximum height of the individual quads instead of the height of the bounding box of all quads, e.g. like this:
Rectangle2D box = null;
double maxQuadHeight = 0;
for(Quad markupBox : value)
{
double quadHeight = markupBox.getBounds2D().getHeight();
if (quadHeight > maxQuadHeight)
maxQuadHeight = quadHeight;
...
}
...
double markupBoxMargin = getMarkupBoxMargin(maxQuadHeight);
box.setRect(box.getX() - markupBoxMargin, box.getY(), box.getWidth() + markupBoxMargin * 2, box.getHeight());
setBox(box);
If you don't want to patch PDF Clown for this, you can also execute this code (with minor adaptations) after constructing the TextMarkup _textMarkup to correct the precalculated annotation rectangle.
Is this fixing a PDF Clown error?
It is not an error as there is no need for the text markup annotation rectangle to be minimal; PDF Clown could also always use the whole crop box for each such annotation.
I would assume, though, that the author of the code wanted to calculate a somewhat minimal rectangle but only optimized for single line and so in a way did not live up to his own expectations...
Are there other problems in this code?
Yes. The text a markup annotation marks needs not be horizontal, it may be there at an angle, it could even be vertical. In such a case some margin would also be needed at the top and the bottom of the annotation rectangle, not (only) at the left and the right.

libgdx, table in scrollpane

I'm trying to make a shop for my game.
In this shop, there should be 2 scroll pannel : one with your iventory's stuff, the other one with what the dealer sells.
(It 'd like to make it this way)
http://i.stack.imgur.com/5gLUr.jpg
So at first, I tried to put a list in my ScrollPane, but I can't put both image of my weapons and text in those.
So I tried to put a table in my ScrollPane : as long as I have stuff in my inventory, I add cells with the item's texture, another one with the name of the weapon, ...)
But there's no way I found to select the table ...
So I'm a bit confused : if I use a List, I can't put texture and make it look fancy, but with Tables, I can't select the items ...
Have you guys got any ideas of how I should do that ?
Thanks !
EDIT : Here is some code, but no everything since it's pretty ugly !
Table test3 = new Table(skin2);
for(WeaponElement weapon : GameScreen.dataLoader.getWeaponArray()){
Image img = new Image(new Texture(weapon.getIconPath()));
test3.add(img).width(32);
test3.add().width(10);
test3.add(weapon.getName()).align(Align.left).row();
}
panelShop = new ScrollPane(test3, skin2);
panelShop.layout();
So basically, I add all my weapons into my test3 table rows add it into my ScrollPane, but i'd like to make them selectable, with I can do by using lists but if I do so, I can't use anymore tables to make it look great...
What about using Buttons to push? The example below shows a table with one button a row. Use a Button with the text you want to display and add a picture beside it.
Table scrollableTable = new Table(skin);
scrollableTable.setBounds(0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
for(WeaponElement weapon : GameScreen.dataLoader.getWeaponArray()){
final TextButton button = new TextButton(wapon.name, skin, "default");
button.addListener(new ClickListener() {
#Override
public void clicked(InputEvent event, float x, float y) {
// do stuff
});
scrollableTable.add(button).center();
// add a picture
Image img = new Image(new Texture(weapon.getIconPath()));
scrollableTable.add(img).width(32);
// finish a row
scrollableTable.row();
}
Try Tree ui component and use it as table. It is selectable and accepts Actor as node (use Table as row layout).
See com.badlogic.gdx.tests.TestTree (link) example.

AndroidPlot: Adding and Removing XYSeries

I'm having a problem with this implementation:
if(blueActive) {
blueFormat = new LineAndPointFormatter(Color.rgb(0,0,255), null, null);
blue = new SimpleXYSeries(xArray,yArray, selectedDate);
Log.e(TAG , "blueActive");
cvPlot.addSeries(blue, blueFormat);
}
if(redActive) {
redFormat = new LineAndPointFormatter(Color.rgb(255,0,0), null, null);
red = new SimpleXYSeries(xArray,yArray, selectedDate);
Log.e(TAG , "redActive");
cvPlot.addSeries(red, redFormat);
}
if(greenActive) {
greenFormat = new LineAndPointFormatter(Color.rgb(0,255,0), null, null);
green = new SimpleXYSeries(xArray,yArray, selectedDate);
Log.e(TAG , "greenActive");
cvPlot.addSeries(green, greenFormat);
}
cvPlot.redraw();
The series are plotted with the correct xArray and yArray, but when the graph redraws, all the plots are the same color. I'm looking for this code to redraw the plot with three different colors. Am I doing something logically wrong here?
Although this question wasn't very descriptive with the other code involved, ultimately the answer was to add this into the onClick that started the process of adding the plots:
xArray.clear();
yArray.clear();
The lines were overlapping, and when the next color was added, since the red plot was selected after blue, the two arrays grew to include the points of the blue and red, making it look like the color wasn't changing. In other words, the red array included the blue array, and since those points were identical to the blue plot, I couldn't see the blue plot since the red plot was on top!
I had set up a Log.d long before this problem but really didn't notice the array doubling since I didn't log the count because I only wanted to see that the x and y values were populating.
Log your array counts and clear your arrays rook dawg!!

How to get a Swing ListSelectionEvent's "source" item?

I am trying to write a ListSelectionListener for a JList that knows which list item the user is selecting away from, and which list item the user is selecting to. So if a list has three items in it {Apple, Orange, Pear}, and the current selection is on Orange and the user selects Pear, then:
srcFruit is Orange; and
destFruit is Pear
Here's the code I have:
myList.addListSelectionListener(new ListSelectionListener() {
public void valueChanged(ListSelectionEvent fruitSelectionEvent) {
printSourceAndDestFruit(myList, fruitSelectionEvent);
}
});
private void printSourceAndDestFruit(JList list, ListSelectionEvent event) {
FruitVO srcFruit = (FruitVO)list.getModel().getElementAt(event.getFirstIndex());
FruitVO destFruit = (FruitVO)list.getModel().getElementAt(event.getLastIndex());
System.out.println("srcFruit = " + srcFruit.getName() + " and destFruit = " = destFruit.getName());
}
When the application loads and initializes the JList, there is no default selection. When I take the following actions:
Click Orange
Click Pear
Click Orange again
Here's the print out I get:
srcFruit = Orange and destFruit = Pear
srcFruit = Orange and destFruit = Pear
Where am I going wrong here? Are getFirstIndex()/getLastIndex() buggy or just not the correct Swing methods to be using?
Here's the output I should be seeing:
srcFruit = Orange and destFruit = Pear
srcFruit = Pear and destFruit = Orange
So even though I made 3 selects (mouse clicks), since the first time I click Orange wasn't a change from one value to the next, I believe it is correct to not fire and call printSourceAndDestFruit. The I select Pear and it is correct in stating that srcFruit is Orange and that destFruit is Pear. But when I click back to Orange the 2nd println should have srcFruit as Pear and destFruit as Orange. Why doesn't it?!?!
Thanks in advance!
The first and last index are not what you think they are. Here's what the javadoc says:
getFirstIndex()
Returns the index of the first row whose selection may have changed.
getLastIndex()
Returns the index of the last row whose selection may have changed.
So, since the selection of pear and orange changes at each click, and since pear is after orange in the list, getFirstIndex() always returns Orange and getLastIndex() always returns Pear.
If you want to compare the new selection with the last one, then keep the last selection in some member variable, and compare it with the new selection each time the selection changes, and getValueIsAdjusting() returns false.
The reason that selection is in the order Orange, Pear is that the DefaultListSelectionModel calls fireValueChanged solely based on the items index location in the ListModel rather than the new selection index.
This related bug report asked a similar question but shows that this was normal behavior and showed that you could add a workaround by using:
FruitVO srcFruit = (FruitVO)list.getModel().getElementAt(list.getMinSelectionIndex());
FruitVO destFruit = (FruitVO)list.getModel().getElementAt(list.getMaxSelectionIndex());

Categories