Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature #8912: giving the possibility to select fastly users and groups without using the user panel. #828

Merged
merged 1 commit into from Jul 19, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -315,4 +315,17 @@ default String getDisplayedName() {
* connected.
*/
String getDurationOfCurrentSession();

/**
* Indicates if a limitation exists about the number of receivers the user can notify manually.
* @return true if the limitation exists, false otherwise.
*/
boolean isUserManualNotificationUserReceiverLimit();

/**
* Gets the maximum user receivers the user can notify manually.
* @return the maximum user receivers the user can notify manually. If the value is not greater
* than 0, the user is not limited.
*/
int getUserManualNotificationUserReceiverLimitValue();
}
Expand Up @@ -42,6 +42,8 @@

public class DBUtil {

private static final int MAX_NB_ATTEMPT = 100;

/**
* @return the DateFieldLength
*/
Expand Down Expand Up @@ -140,7 +142,7 @@ public static int getNextId(final String identifierName) throws SQLException {
public static int getNextId(final String identifierName, final String tableFieldIdentifierName)
throws SQLException {
final String identifierNameLowerCase = identifierName.toLowerCase(Locale.ROOT);
for (int nbAttempts = 0; ; nbAttempts++) {
for (int nbAttempts = 0; nbAttempts < MAX_NB_ATTEMPT; nbAttempts++) {

// Getting the next unique identifier value from uniqueId table
Integer nextUniqueMaxId = nextUniqueIdentifierValue(identifierNameLowerCase);
Expand All @@ -155,13 +157,10 @@ public static int getNextId(final String identifierName, final String tableField
// The next identifier value has been well computed
return nextUniqueMaxId;
}

if (nbAttempts > 2) {
throw new SilverpeasRuntimeException(
"computing of next id not possible for " + identifierName + " with " +
tableFieldIdentifierName + "primary key");
}
}
throw new SilverpeasRuntimeException(
"computing of next id not possible for " + identifierName + " with " +
tableFieldIdentifierName + "primary key");
}

/**
Expand Down
Expand Up @@ -185,7 +185,7 @@ public boolean exists() {
}
}

public String getStringWithParams(String resName, String... params) {
public String getStringWithParams(String resName, Object... params) {
String msgPattern = getString(resName);
return MessageFormat.format(msgPattern, params);
}
Expand Down
Expand Up @@ -27,6 +27,7 @@ ContainsTooLargeText = ne doit pas exc\u00e9der
Characters = caract\u00e8res

userPanel = S\u00e9lectionner un utilisateur
usersPanel = S\u00e9lectionner des utilisateurs
clearUser = R\u00e9initialiser le champ

groupPanel = S\u00e9lectionner un groupe
Expand Down
Expand Up @@ -27,6 +27,7 @@ ContainsTooLargeText = darf nicht \u00fcbersteigen
Characters = Zeichen

userPanel = Einen Benutzer w\u00e4hlen
usersPanel = Benutzers w\u00e4hlen
clearUser = Feld reinitialisieren

groupPanel = W\u00e4hlen Sie eine Gruppe
Expand Down
Expand Up @@ -27,6 +27,7 @@ ContainsTooLargeText = is limited to
Characters = characters

userPanel = Select a user
usersPanel = Select users
clearUser = Initialize field

groupPanel = Select a group
Expand Down
Expand Up @@ -27,6 +27,7 @@ ContainsTooLargeText = ne doit pas exc\u00e9der
Characters = caract\u00e8res

userPanel = S\u00e9lectionner un utilisateur
usersPanel = S\u00e9lectionner des utilisateurs
clearUser = R\u00e9initialiser le champ

groupPanel = S\u00e9lectionner un groupe
Expand Down
Expand Up @@ -258,8 +258,6 @@ private int actualMaxIdInUniqueIdFor(String tableName) throws SQLException {
}

@Test
@Ignore
// TODO This test fails to often in jenkins. Enable it again once it is correctly fixed
public void nextUniqueIdUpdateForAnExistingTablesShouldWorkAndConcurrency() throws Exception {
long startTime = System.currentTimeMillis();
try {
Expand Down
Expand Up @@ -180,10 +180,15 @@ JdbcSqlQuery toSQLQuery(String fields) {
}

if (criteria.isCriterionOnNameSet()) {
String normalizedName =
criteria.getCriterionOnName().replaceAll("'", "''").replaceAll("\\*", "%");
query.and("(lower(st_user.firstName) like lower(?) OR lower(st_user.lastName) like lower(?))",
normalizedName, normalizedName);
String normalizedName = criteria.getCriterionOnName().replaceAll("'", "''");
if (normalizedName.contains("%")) {
query.and("(lower(st_user.firstName || st_user.lastName) like lower(?))", normalizedName);
} else {
normalizedName = normalizedName.replaceAll("\\*", "%");
query.and(
"(lower(st_user.firstName) like lower(?) OR lower(st_user.lastName) like lower(?))",
normalizedName, normalizedName);
}
}

if (criteria.isCriterionOnUserIdsSet()) {
Expand Down
Expand Up @@ -762,19 +762,12 @@ private Invitation getInvitation(String fromUserId, String toUserId) {
return invitation;
}

/**
* Indicates if a limitation exists about the number of receivers the user can notify manually.
* @return true if the limitation exists, false otherwise.
*/
@Override
public boolean isUserManualNotificationUserReceiverLimit() {
return getUserManualNotificationUserReceiverLimitValue() > 0;
}

/**
* Gets the maximum user receivers the user can notify manually.
* @return the maximum user receivers the user can notify manually. If the value is not greater
* than 0, the user is not limited.
*/
@Override
public int getUserManualNotificationUserReceiverLimitValue() {
int limit = 0;
if (isUserManualNotificationRecipientLimitEnabled() && (isAccessUser() || isAccessGuest() ||
Expand Down
Expand Up @@ -188,7 +188,8 @@ public void displayScripts(final JspWriter jw, final PagesContext pagesContext)
out.append(" field = document.getElementById(\"").append(currentFieldName).append("\");\n");
out.append(" if (field == null) {\n");
// try to find field by name
out.append(" field = $(\"input[name=").append(currentFieldName).append("]\");\n");
out.append(" var $field = $(\"input[name=").append(currentFieldName).append("]\");\n");
out.append(" field = $field.length ? $field[0] : null;\n");
out.println("}");
out.append(" if (field != null) {\n");
fieldDisplayer.displayScripts(out, fieldTemplate, pc);
Expand Down
Expand Up @@ -23,26 +23,31 @@
*/
package org.silverpeas.core.contribution.content.form.displayers;

import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.silverpeas.core.contribution.content.form.Field;
import org.apache.commons.fileupload.FileItem;

import org.apache.ecs.xhtml.div;
import org.silverpeas.core.contribution.content.form.Field;
import org.silverpeas.core.contribution.content.form.FieldDisplayer;
import org.silverpeas.core.contribution.content.form.FieldTemplate;
import org.silverpeas.core.contribution.content.form.Form;
import org.silverpeas.core.contribution.content.form.FormException;
import org.silverpeas.core.contribution.content.form.PagesContext;
import org.silverpeas.core.contribution.content.form.Util;
import org.silverpeas.core.contribution.content.form.field.GroupField;
import org.silverpeas.core.util.WebEncodeHelper;
import org.silverpeas.core.util.StringUtil;
import org.silverpeas.core.util.URLUtil;
import org.silverpeas.core.html.plugin.UserGroupSelectProducer;
import org.silverpeas.core.util.file.FileUploadUtil;

import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import static org.silverpeas.core.contribution.content.form.displayers.UserFieldDisplayer
.produceMandatoryCheck;
import static org.silverpeas.core.html.plugin.UserGroupSelectProducer.SelectionType.GROUP;
import static org.silverpeas.core.html.plugin.UserGroupSelectProducer.withContainerId;
import static org.silverpeas.core.util.StringUtil.getBooleanValue;
import static org.silverpeas.core.util.StringUtil.isDefined;

/**
* A GroupFieldDisplayer is an object which allow to select a group and display it in HTML and can
* retrieve via HTTP any updated value.
Expand All @@ -54,7 +59,7 @@
*/
public class GroupFieldDisplayer extends AbstractFieldDisplayer<GroupField> {

private final static int GROUP = 1;
private static final int NB_HTML_ELEMENTS = 2;

/**
* Returns the name of the managed types.
Expand All @@ -74,23 +79,10 @@ public String[] getManagedTypes() {
* </ul>
*/
@Override
public void displayScripts(PrintWriter out, FieldTemplate template, PagesContext PagesContext)
public void displayScripts(PrintWriter out, FieldTemplate template, PagesContext pagesContext)
throws java.io.IOException {
String language = PagesContext.getLanguage();

if (!GroupField.TYPE.equals(template.getTypeName())) {

}
if (template.isMandatory() && PagesContext.useMandatory()) {
out.println(" if (isWhitespace(stripInitialWhitespace(field.value))) {");
out.println(" errorMsg+=\" - '"
+ WebEncodeHelper.javaStringToJsString(template.getLabel(language))
+ "' " + Util.getString("GML.MustBeFilled", language)
+ "\\n\";");
out.println(" errorNb++;");
out.println(" }");
}
Util.getJavascriptChecker(template.getFieldName(), PagesContext, out);
produceMandatoryCheck(out, template, pagesContext);
Util.getJavascriptChecker(template.getFieldName(), pagesContext, out);
}

/**
Expand All @@ -104,82 +96,45 @@ public void displayScripts(PrintWriter out, FieldTemplate template, PagesContext
@Override
public void display(PrintWriter out, GroupField field, FieldTemplate template,
PagesContext pageContext) throws FormException {
String language = pageContext.getLanguage();
String selectGroupImg = Util.getIcon("groupPanel");
String selectGroupLab = Util.getString("groupPanel", language);
String deleteImg = Util.getIcon("delete");
String deleteLab = Util.getString("clearGroup", language);

String groupName = "";
String groupId = "";
StringBuilder html = new StringBuilder();

String fieldName = template.getFieldName();

if (!GroupField.TYPE.equals(field.getTypeName())) {

} else {
groupId = field.getGroupId();
}
if (!field.isNull()) {
groupName = field.getValue();
}
html.append("<input type=\"hidden\"" + " id=\"").append(fieldName).append("\" name=\"").
append(fieldName).append("\" value=\"").
append(WebEncodeHelper.javaStringToHtmlString(groupId)).append("\" />");

String displayedElementId = fieldName + "_name";

if (!template.isHidden()) {
html.append("<input type=\"text\" disabled=\"disabled\" size=\"50\" " + " id=\"").
append(displayedElementId).append("\" name=\"").append(fieldName).
append("$$name\" value=\"").
append(WebEncodeHelper.javaStringToHtmlString(groupName)).append("\" />");
}

if (!template.isHidden() && !template.isDisabled() && !template.isReadOnly()) {
final boolean writable =
!template.isHidden() && !template.isDisabled() && !template.isReadOnly();
final String language = pageContext.getLanguage();
final String selectGroupLab = Util.getString("groupPanel", language);
final String deleteGroupLab = Util.getString("clearGroup", language);
final String fieldName = template.getFieldName();
final String rootContainerId = "select-user-group-" + fieldName;
final String groupId = field.getTypeName().equals(GroupField.TYPE) ? field.getGroupId() : "";

final UserGroupSelectProducer selectGroup = withContainerId(rootContainerId)
.withGroupInputName(fieldName)
.selectionOf(GROUP)
.multiple(false)
.readOnly(!writable)
.hidden(template.isHidden())
.withGroupIds(groupId)
.withUserPanelButtonLabel(selectGroupLab)
.withRemoveButtonLabel(deleteGroupLab);
if (writable) {
Map<String, String> parameters = template.getParameters(pageContext.getLanguage());
boolean groupsOfInstanceOnly =
StringUtil.getBooleanValue(parameters.get("groupsOfInstanceOnly"));
String roles = parameters.get("roles");
if (StringUtil.isDefined(roles)) {
groupsOfInstanceOnly = true;
}
html.append("&nbsp;<a href=\"#\" onclick=\"javascript:SP_openWindow('")
.append(URLUtil.getApplicationURL())
.append("/RselectionPeasWrapper/jsp/open" + "?formName=")
.append(pageContext.getFormName())
.append("&elementId=").append(fieldName)
.append("&elementName=").append(displayedElementId)
.append("&selectable=").append(GROUP)
.append("&selectedGroup=").append((groupId == null) ? "" : groupId)
.append(groupsOfInstanceOnly ? "&instanceId=" + pageContext.getComponentId() : "")
.append(StringUtil.isDefined(roles) ? "&roles=" + roles : "")
.append("','selectGroup',800,600,'');return false;\" >");
html.append("<img src=\"").append(selectGroupImg).
append("\" width=\"15\" height=\"15\" border=\"0\" alt=\"").append(selectGroupLab).
append("\" align=\"top\" title=\"").append(selectGroupLab).append("\"/></a>");
html.append("&nbsp;<a href=\"#\" onclick=\"javascript:").
append("$('#").append(fieldName).append("').val('');").
append("$('#").append(displayedElementId).append("').val('')").
append(";return false;\">");
html.append("<img src=\"").append(deleteImg).append(
"\" width=\"15\" height=\"15\" border=\"0\" alt=\"").append(deleteLab).append(
"\" align=\"top\" title=\"").append(deleteLab).append("\"/></a>");

if (template.isMandatory() && pageContext.useMandatory()) {
html.append(Util.getMandatorySnippet());
boolean groupsOfInstanceOnly =
getBooleanValue(parameters.get("groupsOfInstanceOnly")) || isDefined(roles);
if (groupsOfInstanceOnly) {
selectGroup.filterOnComponentId(pageContext.getComponentId());
}
selectGroup.filterOnRoles(roles);
selectGroup.mandatory(template.isMandatory() && pageContext.useMandatory());
}

out.println(html);
out.println(new div().setID(rootContainerId));
out.println(selectGroup.produce());
}

@Override
public List<String> update(String newId, GroupField field, FieldTemplate template,
PagesContext pagesContext) throws FormException {
if (field.getTypeName().equals(GroupField.TYPE)) {
if (!StringUtil.isDefined(newId)) {
if (!isDefined(newId)) {
field.setNull();
} else {
field.setGroupId(newId);
Expand All @@ -204,7 +159,7 @@ public boolean isDisplayedMandatory() {
*/
@Override
public int getNbHtmlObjectsDisplayed(FieldTemplate template, PagesContext pagesContext) {
return 2;
return NB_HTML_ELEMENTS;
}

@Override
Expand All @@ -213,7 +168,7 @@ public List<String> update(List<FileItem> items, GroupField field, FieldTemplate
String itemName = template.getFieldName();
String value = FileUploadUtil.getParameter(items, itemName);
if (pageContext.getUpdatePolicy() == PagesContext.ON_UPDATE_IGNORE_EMPTY_VALUES
&& !StringUtil.isDefined(value)) {
&& !isDefined(value)) {
return new ArrayList<>();
}
return update(value, field, template, pageContext);
Expand Down