KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mmbase > datatypes > ConfirmPasswordDataType


1 /*
2
3 This software is OSI Certified Open Source Software.
4 OSI Certified is a certification mark of the Open Source Initiative.
5
6 The license (Mozilla version 1.0) can be read at the MMBase site.
7 See http://www.MMBase.org/license
8
9 */

10 package org.mmbase.datatypes;
11
12 import java.util.*;
13 import org.mmbase.bridge.*;
14 import org.mmbase.datatypes.processors.Processor;
15 import org.mmbase.util.*;
16 import org.mmbase.util.logging.*;
17
18 /**
19  * A confirmed password datatype must have the same value as another field of the node (and makes
20  * only sense as a field of a node).
21  *
22  * @author Michiel Meeuwissen
23  * @version $Id: ConfirmPasswordDataType.java,v 1.10 2006/07/17 07:32:29 pierre Exp $
24  * @since MMBase-1.8
25  */

26 public class ConfirmPasswordDataType extends StringDataType {
27     private static final Logger log = Logging.getLoggerInstance(ConfirmPasswordDataType.class);
28
29     private static final long serialVersionUID = 1L; // increase this if object serialization changes (which we shouldn't do!)
30

31     protected PasswordRestriction passwordRestriction = new PasswordRestriction("password");
32
33     /**
34      * Constructor for string data type.
35      * @param name the name of the data type
36      */

37     public ConfirmPasswordDataType(String JavaDoc name) {
38         super(name);
39     }
40
41     protected void inheritRestrictions(BasicDataType origin) {
42         super.inheritRestrictions(origin);
43         if (origin instanceof ConfirmPasswordDataType) {
44             ConfirmPasswordDataType dataType = (ConfirmPasswordDataType) origin;
45             passwordRestriction.inherit(dataType.passwordRestriction);
46         }
47     }
48     protected void cloneRestrictions(BasicDataType origin) {
49         super.cloneRestrictions(origin);
50         if (origin instanceof ConfirmPasswordDataType) {
51             ConfirmPasswordDataType dataType = (ConfirmPasswordDataType) origin;
52             passwordRestriction = new PasswordRestriction(dataType.passwordRestriction);
53         }
54     }
55
56     protected Collection validateCastValue(Collection errors, Object JavaDoc castValue, Object JavaDoc value, Node node, Field field) {
57         errors = super.validateCastValue(errors, castValue, value, node, field);
58         errors = passwordRestriction.validate(errors, castValue, node, field);
59         return errors;
60     }
61     /**
62      * The field property is the name of the other password field that this fields 'confirms'. It default to 'password'.
63      * In datatype XML it can be set with the generic <property name="field" value="..." />
64      */

65     public void setField(String JavaDoc field) {
66         passwordRestriction.setValue(field);
67     }
68     /**
69      * Returns the name of the field which is 'confirmed' by this datatype.
70      */

71     public String JavaDoc getField() {
72         edit();
73         return passwordRestriction.getField();
74     }
75
76     protected StringBuffer JavaDoc toStringBuffer() {
77         StringBuffer JavaDoc buf = super.toStringBuffer();
78         buf.append(" confirm(").append(passwordRestriction.getValue()).append(")");
79         return buf;
80     }
81
82     protected class PasswordRestriction extends AbstractRestriction {
83         PasswordRestriction(PasswordRestriction source) {
84             super(source);
85         }
86         PasswordRestriction(String JavaDoc field) {
87             super("confirmpassword", field);
88         }
89         protected final String JavaDoc getField() {
90             return (String JavaDoc) value;
91         }
92
93         protected boolean simpleValid(final Object JavaDoc v, final Node node, final Field field) {
94             if (node != null && field != null && v != null) {
95                 if (! node.isChanged(getField())) return true;
96
97                 Field passwordField = node.getNodeManager().getField(getField());
98                 Processor setProcessor = passwordField.getDataType().getProcessor(PROCESS_SET);
99                 Object JavaDoc processedValue = setProcessor.process(node, field, v);
100                 String JavaDoc passwordValue = (String JavaDoc) node.getObjectValue(getField());
101                 if (log.isDebugEnabled()) {
102                     log.debug("Password checking " + (node.isNew() ? "new" : "existing") + " node. Password field " + passwordField + " set-processor " + setProcessor);
103                     log.debug("Offered value '" + v + "' --> '" + processedValue);
104                     log.debug("Comparing '" + passwordValue + "' with '" + processedValue + "'(" + v + ")");
105                 }
106                 return passwordValue.equals(v) || passwordValue.equals(processedValue);
107             } else {
108                 return true;
109             }
110         }
111     }
112
113 }
114
Popular Tags