From b8dd4d7c91da87992d4c743ef315de0b557e4ddf Mon Sep 17 00:00:00 2001
From: Daniel Haag <none@none>
Date: Wed, 29 Jun 2016 15:15:16 +0200
Subject: [PATCH] OPENOLAT-257: Change the property handler for StudySubject to
 the new Generic254CharTextPropertyHandler (was the generic 127 char handler)

---
 .../Generic254CharTextPropertyHandler.java    | 163 ++++++++++++++++++
 .../userPropertriesHandlersContext.xml        |   2 +-
 2 files changed, 164 insertions(+), 1 deletion(-)
 create mode 100644 src/main/java/at/ac/uibk/user/propertyhandlers/Generic254CharTextPropertyHandler.java

diff --git a/src/main/java/at/ac/uibk/user/propertyhandlers/Generic254CharTextPropertyHandler.java b/src/main/java/at/ac/uibk/user/propertyhandlers/Generic254CharTextPropertyHandler.java
new file mode 100644
index 00000000000..868b7922cb9
--- /dev/null
+++ b/src/main/java/at/ac/uibk/user/propertyhandlers/Generic254CharTextPropertyHandler.java
@@ -0,0 +1,163 @@
+/**
+ * <a href="http://www.openolat.org">
+ * OpenOLAT - Online Learning and Training</a><br>
+ * <p>
+ * Licensed under the Apache License, Version 2.0 (the "License"); <br>
+ * you may not use this file except in compliance with the License.<br>
+ * You may obtain a copy of the License at the
+ * <a href="http://www.apache.org/licenses/LICENSE-2.0">Apache homepage</a>
+ * <p>
+ * Unless required by applicable law or agreed to in writing,<br>
+ * software distributed under the License is distributed on an "AS IS" BASIS, <br>
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. <br>
+ * See the License for the specific language governing permissions and <br>
+ * limitations under the License.
+ * <p>
+ * Initial code contributed and copyrighted by<br>
+ * frentix GmbH, http://www.frentix.com
+ * <p>
+ * Modified by<br>
+ * Daniel Haag, http://www.uibk.ac.at
+ * <p>
+ */
+package at.ac.uibk.user.propertyhandlers;
+
+import java.util.Locale;
+import java.util.Map;
+import java.util.regex.Pattern;
+
+import org.olat.core.gui.components.form.ValidationError;
+import org.olat.core.gui.components.form.flexible.FormItem;
+import org.olat.core.gui.components.form.flexible.FormItemContainer;
+import org.olat.core.gui.components.form.flexible.FormUIFactory;
+import org.olat.core.gui.components.form.flexible.elements.TextElement;
+import org.olat.core.gui.components.form.flexible.impl.elements.ItemValidatorProvider;
+import org.olat.core.id.User;
+import org.olat.user.AbstractUserPropertyHandler;
+import org.olat.user.UserManager;
+
+/**
+ * <h3>Description:</h3>
+ * This generic text property provides a userfield that has a maximum of 254
+ * characters length. It can contain any string, the only validity check that is
+ * performed is the not empty check if the property is declared as mandatory.
+ * <p>
+ * Based on org.olat.user.propertyhandlersGeneric127CharTextPropertyHandler
+ * <p>
+ * Initial Date: 27.07.2007 <br>
+ * 
+ * @author Florian Gnaegi, frentix GmbH, http://www.frentix.com
+ */
+
+public class Generic254CharTextPropertyHandler extends
+		AbstractUserPropertyHandler {
+
+	private Pattern regExp;
+	private String regExpMsgKey;
+
+	/**
+	 * @see org.olat.user.propertyhandlers.UserPropertyHandler#updateUserFromFormItem(org.olat.core.id.User, org.olat.core.gui.components.form.flexible.FormItem)
+	 */
+	@Override
+	public void updateUserFromFormItem(User user, FormItem formItem) {
+		String internalValue = getStringValue(formItem);
+		setInternalValue(user, internalValue);
+	}
+
+	/**
+	 * @see org.olat.user.propertyhandlers.UserPropertyHandler#getStringValue(org.olat.core.gui.formelements.FORMELEMENTOLD)
+	 */
+	@Override
+	public String getStringValue(FormItem formItem) {
+		return ((org.olat.core.gui.components.form.flexible.elements.TextElement) formItem).getValue();
+	}
+
+	/**
+	 * @see org.olat.user.propertyhandlers.UserPropertyHandler#getStringValue(java.lang.String, java.util.Locale)
+	 */
+	@Override
+	public String getStringValue(String displayValue, Locale locale) {
+		// default implementation is to use same as displayValue
+		return displayValue;
+	}
+	
+	/**
+	 *  
+	 * @see org.olat.user.propertyhandlers.UserPropertyHandler#addFormItem(java.util.Locale, org.olat.core.id.User, java.lang.String, boolean, org.olat.core.gui.components.form.flexible.FormItemContainer)
+	 */
+	@Override
+	public FormItem addFormItem(Locale locale, final User user, String usageIdentifyer, boolean isAdministrativeUser,	FormItemContainer formItemContainer) {
+		org.olat.core.gui.components.form.flexible.elements.TextElement tElem = null;
+		tElem = FormUIFactory.getInstance().addTextElement(getName(), i18nFormElementLabelKey(), 127, getInternalValue(user), formItemContainer);
+		tElem.setItemValidatorProvider(new ItemValidatorProvider() {
+			@Override
+			public boolean isValidValue(String value, ValidationError validationError, Locale locale) {
+				return Generic254CharTextPropertyHandler.this.isValidValue(user, value, validationError, locale);
+			}
+		});
+		tElem.setLabel(i18nFormElementLabelKey(), null);
+		UserManager um = UserManager.getInstance();
+		if ( um.isUserViewReadOnly(usageIdentifyer, this) && ! isAdministrativeUser) {
+			tElem.setEnabled(false);
+		}
+		if (um.isMandatoryUserProperty(usageIdentifyer, this)) {
+			tElem.setMandatory(true);
+		}
+		return tElem;
+	}
+	
+	/**
+	 * @see org.olat.user.propertyhandlers.UserPropertyHandler#isValid(org.olat.core.gui.components.form.flexible.FormItem, java.util.Map)
+	 */
+	@Override
+	public boolean isValid(User user, FormItem formItem, Map<String,String> formContext) {
+		TextElement textElemItem = (TextElement) formItem;
+		if (textElemItem.isMandatory()) {
+			if (textElemItem.isEmpty("new.form.mandatory")) {
+				return false;
+			}
+		} else {
+			if (textElemItem.getValue().equals("")) {
+				return true;
+			}
+		}
+
+		if (regExp != null) {
+			return regExp.matcher(textElemItem.getValue()).matches();
+		}
+		
+		//TODO unique user property (doesn't bulk change unique property)
+		
+		return true;
+	}
+
+	
+	/**
+	 * @see org.olat.user.propertyhandlers.UserPropertyHandler#isValidValue(java.lang.String, org.olat.core.gui.components.form.ValidationError, java.util.Locale)
+	 */
+	@Override
+	public boolean isValidValue(User user, String value, ValidationError validationError, Locale locale) {
+		if (value != null && value.length() > 254) {
+			validationError.setErrorKey("general.error.max.254");
+			return false;
+		}
+		
+		if (regExp != null) {
+			if (!regExp.matcher(value).matches()) {
+				validationError.setErrorKey(regExpMsgKey);
+				return false;
+			}
+		}
+		
+		return true;
+	}
+	
+	public void setRegExp(String rx) {
+		this.regExp = Pattern.compile(rx);
+	}
+	
+	public void setRegExpMsgKey(String key) {
+		this.regExpMsgKey = key;
+	}
+
+}
diff --git a/src/main/java/org/olat/user/propertyhandlers/_spring/userPropertriesHandlersContext.xml b/src/main/java/org/olat/user/propertyhandlers/_spring/userPropertriesHandlersContext.xml
index cd25ea97667..1497a44acfd 100644
--- a/src/main/java/org/olat/user/propertyhandlers/_spring/userPropertriesHandlersContext.xml
+++ b/src/main/java/org/olat/user/propertyhandlers/_spring/userPropertriesHandlersContext.xml
@@ -166,7 +166,7 @@
 		<property name="deletable" value="true" />
 	</bean>
 	
-	<bean id="userPropertyStudySubject" class="org.olat.user.propertyhandlers.Generic127CharTextPropertyHandler">
+	<bean id="userPropertyStudySubject" class="at.ac.uibk.user.propertyhandlers.Generic254CharTextPropertyHandler">
 		<property name="name" value="studySubject" />
 		<property name="group" value="institute" />
 		<property name="deletable" value="true" />
-- 
GitLab