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 ?!
Related
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.
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.
I'm using primefaces autocomlete force selection to search over accounts by name
JSF Component
<p:autoComplete value="#{journal.journal.debit}"
completeMethod="#{account.completeAccount}"
var="var" itemLabel="#{var.name}" itemValue="#{var}"
forceSelection="true" immediate="false" required="true">
Bean Method:
public List<Account> completeAccount(String query) {
List<Account> allAccounts = service.get(Account.class);
List<Account> filteredAccounts = new ArrayList();
for (int i = 0; i < allAccounts.size(); i++) {
Account foundAccount = allAccounts.get(i);
if (foundAccount.getName().toLowerCase().contains(query.toLowerCase())) {
filteredAccounts.add(foundAccount);
}
}
return filteredAccounts;
}
this works fine, now if I want to change the search to search also for account number in the query value. I have used the following:
if (foundAccount.getName().toLowerCase().contains(query.toLowerCase()) || foundAccount.getNumber() == Integer.parseInt(query)) {
filteredAccounts.add(foundAccount);
}
but then, the filter is only returning searching for the number and ignoring name search. How can I achieve this?
I think that what Deepak was trying to say is that what you're doing is perfectly valid, and not an issue with primefaces rather something wrong with your condition. And indeed, the most obvious thing is that there is no Integer.parseInteger(String s) method, at least no in java.lang.Integer.
If this is a typo of some sort, and if you're working with Integer objects (not the int primitives) , make sure that you're comparing them by using .equals method. Comparing Integers by == will work only in the range from -128 - 127, outside of that range it will compare references.
Hope it helps
try this
if (
(foundAccount.getName().toLowerCase().contains(query.toLowerCase()))
||
(foundAccount.getNumber() == Integer.parseInteger(query))
)
{
filteredAccounts.add(foundAccount);
}
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
Is it possible to disable specific dates (for example: 15th May or 23rd June) on the PrimeFaces 3.5 <p:calendar> component? There is min and max, but I need to disable specific dates like public holidays using el expressions so I can use dynamic dates.
Using Hieu's answer, make sure the dates you disable have NO leading zeros in front (eg. '03/03/2013' should be '3/3/2013').
Step 1: write a javascript function to disable a list of dates
var disabledDays = ["5-15-2013", "6-23-2013"];
function disableAllTheseDays(date) {
var m = date.getMonth(), d = date.getDate(), y = date.getFullYear();
for (i = 0; i < disabledDays.length; i++) {
if($.inArray((m+1) + '-' + d + '-' + y,disabledDays) != -1) {
return [false];
}
}
return [true];
}
Step 2: use beforeShowDay attribute of Primefaces datepicker
<p:calendar id="pfdate" navigator="true" pattern="MM-dd-yyyy"
value="#{day}" beforeShowDay="disableAllTheseDays" showOn="button"/>
To disable all friday days, i have used the beforeShowDay attribute on the p:calendar , the code beloow explains this :
<p:calendar ..... mask="true" locale="fr" beforeShowDay="fridaysDisabled" .... required="true" maxdate="new Date();"/>
AND the JavaScript function :
function fridaysDisabled(date)
{
var day = date.getDay();
return [day != 5, '']
}
To disable an array of dates, follow these steps:
Create a string object that contains all the dates:
festivosArray = "[";
for(CalendarioDao dia : festivos){
festivosArray += "'" + dia.getFecha() + "',";
}
festivosArray += "]";
Create a JavaScript function:
<script type="text/javascript">
function deshabFest(date) {
var disabledDays = #{calendarioView.festivosArray};
var string = jQuery.datepicker.formatDate('dd/mm/yy', date);
return [disabledDays.indexOf(string) == -1, '']
}
</script>
Use the beforeshowday attribute to call the function
<p:calendar mode="inline" locale="es" value="#{calendarioView.fechaFestivo}" id="calendarioFestivo" widgetVar="calendarioFestivo" beforeShowDay="deshabFest" />