How to save only one item of select many check box? - jsf

when I an offer create an oferta object (offer) it saves all of the items in the select many check box (this are location as per company selected(empresa)), not only the checked ones:
Thank you very much for any help you can provide.
<h:outputLabel value="#{bundle.CreateOfertaLabel_empresaidEmpresa}" for="empresaidEmpresa" />
<h:selectOneMenu id="empresaidEmpresa"
value="#{ofertaController.selected.empresaidEmpresa}"
title="#{bundle.CreateOfertaTitle_empresaidEmpresa}"
required="true"
requiredMessage="#{bundle.CreateOfertaRequiredMessage_empresaidEmpresa}">
<f:ajax event="valueChange" execute="empresaidEmpresa" render="ubicacionCollection" />
<f:selectItems value="#{empresaController.itemsAvailableSelectOne}"/>
</h:selectOneMenu>
<h:outputLabel value="#{bundle.CreateOfertaLabel_ubicacionCollection}" for="ubicacionCollection" />
<h:selectManyCheckbox id="ubicacionCollection"
value="#{ubicacionXEmpresa}"
title="#{bundle.CreateOfertaTitle_ubicacionCollection}" >
<f:converter id="ubicacionConverter" converterId="ubicacionConverter"/>
<f:selectItems id="ubicacionCollectionItems"
value="#{ofertaController.selected.empresaidEmpresa.ubicacionCollection}"
var="ubicacionXEmpresa"
itemLabel="#{ubicacionXEmpresa.barrio}"
itemValue="#{ubicacionXEmpresa}"/>
</h:selectManyCheckbox>
Your correct the lines should be:
<h:outputLabel value="#{bundle.CreateOfertaLabel_ubicacionCollection}" for="ubicacionCollection" />
<h:selectManyCheckbox id="ubicacionCollection"
value="#{ofertaController.selected.ubicacionCollection}"
title="#{bundle.CreateOfertaTitle_ubicacionCollection}" >
<f:converter id="ubicacionConverter" converterId="ubicacionConverter"/>
<f:selectItems id="ubicacionCollectionItems"
value="#{ofertaController.selected.empresaidEmpresa.ubicacionCollection}"
var="ubicacionXEmpresa"
itemLabel="#{ubicacionXEmpresa.barrio}"
itemValue="#{ubicacionXEmpresa}"/>
</h:selectManyCheckbox>
But know faces show me this on creation of an offer
Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.3.2.v20111125-r10461): org.eclipse.persistence.exceptions.DatabaseException Internal Exception: com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Duplicate entry '32-1' for key 'PRIMARY' Error Code: 1062 Call: INSERT INTO oferta_has_ubicacion (ubicacion_idUbicacion, oferta_idOferta) VALUES (?, ?) bind => [2 parameters bound] Query: DataModifyQuery(name="ubicacionCollection" sql="INSERT INTO oferta_has_ubicacion (ubicacion_idUbicacion, oferta_idOferta) VALUES (?, ?)")
Here is my create method:
public void create(Oferta oferta) {
if (oferta.getUbicacionCollection() == null) {
oferta.setUbicacionCollection(new ArrayList<Ubicacion>());
}
if (oferta.getEmpresaCollection() == null) {
oferta.setEmpresaCollection(new ArrayList<Empresa>());
}
EntityManager em = null;
try {
em = getEntityManager();
em.getTransaction().begin();
Empresa empresaidEmpresa = oferta.getEmpresaidEmpresa();
if (empresaidEmpresa != null) {
empresaidEmpresa = em.getReference(empresaidEmpresa.getClass(), empresaidEmpresa.getIdEmpresa());
oferta.setEmpresaidEmpresa(empresaidEmpresa);
}
Collection<Ubicacion> attachedUbicacionCollection = new ArrayList<Ubicacion>();
for (Ubicacion ubicacionCollectionUbicacionToAttach : oferta.getUbicacionCollection()) {
ubicacionCollectionUbicacionToAttach = em.getReference(ubicacionCollectionUbicacionToAttach.getClass(), ubicacionCollectionUbicacionToAttach.getIdUbicacion());
attachedUbicacionCollection.add(ubicacionCollectionUbicacionToAttach);
}
oferta.setUbicacionCollection(attachedUbicacionCollection);
Collection<Empresa> attachedEmpresaCollection = new ArrayList<Empresa>();
for (Empresa empresaCollectionEmpresaToAttach : oferta.getEmpresaCollection()) {
empresaCollectionEmpresaToAttach = em.getReference(empresaCollectionEmpresaToAttach.getClass(), empresaCollectionEmpresaToAttach.getIdEmpresa());
attachedEmpresaCollection.add(empresaCollectionEmpresaToAttach);
}
oferta.setEmpresaCollection(attachedEmpresaCollection);
em.persist(oferta);
if (empresaidEmpresa != null) {
empresaidEmpresa.getOfertaCollection().add(oferta);
empresaidEmpresa = em.merge(empresaidEmpresa);
}
for (Ubicacion ubicacionCollectionUbicacion : oferta.getUbicacionCollection()) {
ubicacionCollectionUbicacion.getOfertaCollection().add(oferta);
ubicacionCollectionUbicacion = em.merge(ubicacionCollectionUbicacion);
}
for (Empresa empresaCollectionEmpresa : oferta.getEmpresaCollection()) {
empresaCollectionEmpresa.getOfertaCollection().add(oferta);
empresaCollectionEmpresa = em.merge(empresaCollectionEmpresa);
}
em.getTransaction().commit();
} finally {
if (em != null) {
em.close();
}
}
}

value="#{ofertaController.selected.ubicacionCollection}"
The other was a many to many relationship problem jpa issue

Related

Displaying Image with Jakarta Faces, JavaBeans in Java [duplicate]

This question already has answers here:
How do I compare strings in Java?
(23 answers)
Conditionally displaying JSF components
(3 answers)
Ajax update/render does not work on a component which has rendered attribute
(1 answer)
Closed 6 days ago.
I'm new to Java, I'm writing a webapp using Jakarta Faces and Prime faces that, based on the user's birthdate input, will display their Chinese zodiac animal and corresponding image. I've got the message working to display what the animal is, but I'm having issues getting the image to render on the web page. Any suggestions are wildly appreciated. My code is pasted below. I believe it is due to the rendering of the graphic image:
<p:outputPanel id="outputPanel">
<p:graphicImage value="#{currentPersonCalculatorRequestScopedView.zodiacSignImage}"
rendered="${currentPersonCalculatorRequestScopedView.zodiacSignImage}"
width="120px"
height="120px"
alt="Zodiac Image #{currentPersonCalculatorRequestScopedView.zodiacSignImage}"
/>
</p:outputPanel>
<div>
<p:commandButton id="buttonChineseZodiac"
value="Find Chinese Zodiac"
action="#{currentPersonCalculatorRequestScopedView.onZodiac()}"
update=":messages :growl" />
</div>
The Jakarta faces:
public void onZodiac() {
Messages.addGlobalInfo("Chinese Zodiac Symbol is {0}", currentPerson.chineseZodiac());
//zodiacSignValue = currentPerson.chineseZodiac();
if (currentPerson.chineseZodiac() == "Monkey") {
zodiacSignImage = _zodiacImages[0];
} else if (currentPerson.chineseZodiac() == "Rooster") {
zodiacSignImage = _zodiacImages[1];
} else if (currentPerson.chineseZodiac() == "Dog") {
zodiacSignImage = _zodiacImages[2];
} else if (currentPerson.chineseZodiac() == "Pig") {
zodiacSignImage = _zodiacImages[3];
} else if (currentPerson.chineseZodiac() == "Rat") {
zodiacSignImage = _zodiacImages[4];
} else if (currentPerson.chineseZodiac() == "Ox") {
zodiacSignImage = _zodiacImages[5];
} else if (currentPerson.chineseZodiac() == "Tiger") {
zodiacSignImage = _zodiacImages[6];
} else if (currentPerson.chineseZodiac() == "Rabbit") {
zodiacSignImage = _zodiacImages[7];
} else if (currentPerson.chineseZodiac() == "Dragon") {
zodiacSignImage = _zodiacImages[8];
} else if (currentPerson.chineseZodiac() == "Snake") {
zodiacSignImage = _zodiacImages[9];
} else if (currentPerson.chineseZodiac() == "horse") {
zodiacSignImage = _zodiacImages[10];
} else {
zodiacSignImage = _zodiacImages[11];
}
}
Try updating the top panel where the button is. After the action, if your java function works fine, the image should take the new value. I don't understand rendered condition of graphicImage element, it should be a boolean condition.
<div>
<p:commandButton id="buttonChineseZodiac"
value="Find Chinese Zodiac"
action="#{currentPersonCalculatorRequestScopedView.onZodiac()}"
update=":messages :growl, :outputPanel" />
</div>

Exception trying to poll with PrimeFaces

I am getting an exception trying to poll with PrimeFaces. I'm using solution #1 from Primefaces fileUpload and session timout.
javax.faces.FacesException: class java.lang.Long is not supported as "interval" for p:poll
<p:fileUpload id="fileUploadControl" oncomplete="PF('xmlUploadDialog').hide()"
listener="#{xmlUploadBean.handleFileUpload}" mode="advanced"
allowTypes="/(\.|\/)(xml|zip)$/" update=":mainForm:scriptTableId"
multiple="true" />
<p:poll interval="#{session.maxInactiveInterval - 10}" async="true" />
In PrimeFaces 10, the attribute interval does not take a Long it can take an Integer or Duration.
See the code:
Object interval = poll.getInterval();
int convertedInterval;
if (interval instanceof Integer) {
convertedInterval = (Integer) interval;
}
else if (interval instanceof Duration) {
convertedInterval = (int) ((Duration) interval).getSeconds();
}
else if (interval instanceof String) {
try {
convertedInterval = Integer.parseInt((String) interval);
}
catch (NumberFormatException e) {
throw new FacesException(interval + " is not a valid integer for \"interval\" for p:poll", e);
}
}
else {
throw new FacesException(interval.getClass() + " is not supported as \"interval\" for p:poll");
}
Support for Long was added to PrimeFaces 11.

p:autoComplete does not work with single char

I have a page with two inputtext (I'll name them "name" and "id"): one that compares the text entered with a string and another one that compares it with an int converted to string.
Now the problem is this: if I have an "id" == 1 and type 1 into the inputtext, the autocomplete shows only results with two or more digits/chars (so 11,31,117 etc but not 1)...
This is the html:
<p:autoComplete id="CustomerId" value="#{myBean.CustomerBean.id}"
completeMethod="#{myBean.autoCompleteId}"
maxResults="10">
<p:ajax event="itemSelect" listener="#{myBean.selectCustomerById}"
update="resultMessage name idComp table newAssociation" />
</p:autoComplete>
And this is the autocomplete method:
public List<String> autoCompleteId(String query) {
CustomerList = myService.selectByFilters(CustomerBean);
setAcList(new ArrayList<String>());
for (int i = 0; i < CustomerList.size(); i++) {
CustomerAnag tip = CustomerList .get(i);
if(String.valueOf(tip.getId()).contains(query) || String.valueOf(tip.getId()).equals(query)) {
acList.add(tip.getId().toString());
}
}
return acList;
}
What in the sweet heaven am I doing wrong ?!

How to get strength indicator in Primefaces Password component from backing bean

I want to get strength indicator from the xhtml, in the backing beans.
If the "indicator" said Weak, i will do some action.
Right now this is my xhtml code
<h:form id="changePasswordForm" >
<p:messages id="changePasswordMessages" />
<h:panelGrid columns="3" >
<h:outputText for="oldPassword" value="CurrentPassword" />
<p:password id="oldPassword" value="#{changePasswordBean.oldPassword}"
label="CurrentPassword" required="true" feedback="false" minLength="6" />
<p:message for="oldPassword" display="icon" />
<p:spacer height="4px" />
<p:spacer height="4px" />
<p:spacer height="4px" />
<h:outputText for="newPassword1" value="#{NewPassword}" />
<p:password id="newPassword1" value="#{changePasswordBean.newPassword1}"
label="NewPassword" required="true" feedback="true" minLength="6" match="newPassword2"/>
<p:message for="newPassword1" display="icon" />
<h:outputText for="newPassword2" value="#{ConfirmPassword}" />
<p:password id="newPassword2" value="#{changePasswordBean.newPassword2}"
label="ConfirmPassword" required="true" feedback="true" minLength="6" />
<p:message for="newPassword2" display="icon" />
</h:panelGrid>
<table style="border:0; width:100%;">
<tr>
<td colspan="2">
<p:separator style="margin:0;" />
</td>
</tr>
<tr>
<td class="input" style="width:50%;">
<p:commandButton value="#{Save}"
process=":changePasswordForm"
update=":changePasswordForm"
actionListener="#{changePasswordBean.save()
icon="ui-icon-disk" />
</td>
</tr>
</table>
</h:form>
The problem now is, i don't know how to get the "weak", or "strong" message from the UI in the backing beans.
somebody could help me?
i'm using JSF 2, and PrimeFaces 3.4.
BEHOLD
The translation of the JavaScript implementation to Java.
Source
public class PasswordValidator implements Serializable {
/**
* Less than this is weak, more that this is good.
*/
public final static int MEDIUM = 30;
/**
* More than this is strong.
*/
public final static int STRONG = 80;
private String password;
private int score;
public PasswordValidator() {
}
public PasswordValidator(String password) {
setPassword(password);
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
validatePassword();
}
private void validatePassword() {
score = testStrength(password);
}
public int getScore() {
return score;
}
public boolean isWeak() {
return score <= MEDIUM;
}
public boolean isAtLeastGood() {
return score >= MEDIUM;
}
public boolean isStrong() {
return score >= STRONG;
}
public boolean isSecure() {
return score == 100;
}
public static int testStrength(String d) {
if (d == null || d.isEmpty())
return 0;
//var b=0,c=0,a=this;
float b = 0;
int c;
//c=d.match("[0-9]");b+=a.normalize(c?c.length:1/4,1)*25;
c = countMatches(d, "[0-9]"); // asks for at least one number
b += normalize(c != 0 ? 1 : 1 / 4F, 1) * 25;
//c=d.match("[a-zA-Z]");b+=a.normalize(c?c.length:1/2,3)*10;
c = countMatches(d, "[a-zA-Z]"); // matches only latin characters, not other character sets
b += normalize(c != 0 ? 1 : 1 / 2F, 3) * 10;
//c=d.match("[!##$%^&*?_~.,;=]");b+=a.normalize(c?c.length:1/6,1)*35;
c = countMatches(d, "[!##$%^&*?_~.,;=]"); // asks for at least on symbol
b += normalize(c != 0 ? 1 : 1 / 6F, 1) * 35;
//c=d.match("[A-Z]");b+=a.normalize(c?c.length:1/6,1)*30;
c = countMatches(d, "[A-Z]"); // asks for at least one capital letter
b += normalize(c != 0 ? 1 : 1 / 6F, 1) * 30;
//b*=d.length/8;
b *= d.length() / 8F;
System.out.println(b);
//return b>100?100:b
return b > 100 ? 100 : (int) b;
}
private static float normalize(float a, float c) {
return a - c <= 0 ? a / c : 1 + 0.5F * (a / (a + c / 4));
}
private static int countMatches(String container, String regex) {
int i = 0;
Matcher m = Pattern.compile(regex).matcher(container);
while (m.find())
i++;
return i;
}
}
Usage
PasswordValidator.testStrength("password"); // e.g. 83
// or
PasswordValidator pv = new PasswordValidator(); // or new PasswordValidator("password")
pv.setPassword("password");
pv.getScore(); // e.g. 83
pv.isAtLeastGood(); // e.g. true
pv.isStrong(); // e.g. true
Tests
Results of password strength score from the JavaScript/PrimeFaces implementation and mine.
Password My class PrimeFaces
123456 28 28.125
Ofar-E*Qnmcm_eSPA 100 100
123456789aZ 88 88.22916666666666
2010.11.02 83 83.33333333333334
mississDOGippi 79 79.47916666666666
Works Perfect!!!
Notes
The PrimeFaces password component considers a password consisting of 31 letters (without capitals, numbers or symbols) to be a strong password, which is not true.
To test PrimeFaces component execute PrimeFaces.widget.Password.prototype.testStrength(PF('password-widget-var').jq.val())
To get the code of the javascript function execute PrimeFaces.widget.Password.prototype.testStrength and PrimeFaces.widget.Password.prototype.normalize
Source code of PrimeFaces.widget.Password.prototype.testStrength
function (d){
var b=0, c=0, a=this;
c=d.match("[0-9]"); b+=a.normalize(c?c.length:1/4,1)*25;
c=d.match("[a-zA-Z]"); b+=a.normalize(c?c.length:1/2,3)*10;
c=d.match("[!##$%^&*?_~.,;=]"); b+=a.normalize(c?c.length:1/6,1)*35;
c=d.match("[A-Z]"); b+=a.normalize(c?c.length:1/6,1)*30;
b*=d.length/8;
return b>100?100:b
}
Source code of PrimeFaces.widget.Password.prototype.normalize
function (a,c){var b=a-c;if(b<=0){return a/c}else{return 1+0.5*(a/(a+c/4))}}
There is no reason to use float type in Java for score so I used integer.
Both implementations accept only latin character sets.
(again) Works Perfect!!!
So, all you have to do in the back-end is pass the password to PasswordValidator and you will get its strength, which will be the same value as the one calculated by PrimeFaces. To calculate if the password is weak, good or strong as calculated by PrimeFaces use the corresponding methods of the PasswordValidator.

Primefaces Draggable Drop Position

I need to get the drop position of a draggable panel. But i cannot figure out how to do it. I've tried to get the style but I've got unrelated information.
Here is my xhtml :
<h:form id="dnd">
<p:panel id="draggable1"
style="z-index:1; width: 60px; height: 60px;">
<h:outputText value="CAM-1" />
<p:draggable for="draggable1"
revert ="false"/>
</p:panel>
<p:panel id="droppable"
style="z-index:1; width: 600px; height: 600px;">
<p:droppable for="droppable">
<p:ajax listener="#{myBean.onDrop}" />
</p:droppable>
</p:panel>
</h:form>
Here is my backing bean :
public void onDrop(DragDropEvent dragDropEvent) {
String dragId = dragDropEvent.getDragId();
UIComponent draggedItem = FacesContext.getCurrentInstance().getViewRoot().findComponent(dragId);
System.out.println(draggedItem.getClientId());
Panel draggedPanel = (Panel) draggedItem;
String style = draggedPanel.getStyle();
System.out.println(style);
String styleClass = draggedPanel.getStyleClass();
System.out.println(styleClass);
}
Any help will be appreciated. Thanks in advance.
PrimeFaces is using jQuery UI .droppable(), to get the position you should alter the event binding in PrimeFaces.widget.Droppable, this is done in bindDropListener.
Adding a couple of request params to the original request would do it, here's an example:
PrimeFaces.widget.Droppable.prototype.bindDropListener = function() {
var _self = this;
this.cfg.drop = function(event, ui) {
if (_self.cfg.onDrop) {
_self.cfg.onDrop.call(_self, event, ui);
}
if (_self.cfg.behaviors) {
var dropBehavior = _self.cfg.behaviors['drop'];
if (dropBehavior) {
var ext = {
params: [
{name: _self.id + '_dragId', value: ui.draggable.attr('id')},
{name: _self.id + '_dropId', value: _self.cfg.target},
{name: ui.draggable.attr('id') + '_left', value: ui.position.left},
{name: ui.draggable.attr('id') + '_top', value: ui.position.top}
]
};
dropBehavior.call(_self, ext);
}
}
};
}
The change here is adding two more parameters to the ext params of the request, where the positions of the draggable item (left, top) are present.
On the other side of the event onDrop(DragDropEvent dragDropEvent), you can find them as request parameters as I have mentioned.
public void onDrop(DragDropEvent dragDropEvent) {
String dargId = dragDropEvent.getDragId();
Map<String, String> params = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap();
String left = params.get(dargId + "_left");
String top = params.get(dargId + "_top");
}
Note: the ajax event should be present in p:droppable (as in the question)
You can find a small example on github, and a working demo.

Resources