001// Copyright 2004, 2005 The Apache Software Foundation
002//
003// Licensed under the Apache License, Version 2.0 (the "License");
004// you may not use this file except in compliance with the License.
005// You may obtain a copy of the License at
006//
007//     http://www.apache.org/licenses/LICENSE-2.0
008//
009// Unless required by applicable law or agreed to in writing, software
010// distributed under the License is distributed on an "AS IS" BASIS,
011// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012// See the License for the specific language governing permissions and
013// limitations under the License.
014
015package org.apache.tapestry.components;
016
017/**
018 * An interface for converting an objects to their primary keys and back. Typically used to
019 * determine how to store a given object as a hidden value when rendering a form.
020 * <p>
021 * This interface is used by the {@link org.apache.tapestry.components.ForBean For component}. When
022 * a primary key converter is available, it is used during the render, and as part of the rewind
023 * phase that processes the form submission.
024 * <p>
025 * During rendering, {@link #getPrimaryKey(Object)} is invoked for each value. This method is
026 * invoked just before the For's body is rendered. The resulting primary key is written into the
027 * client as a hidden form field.
028 * <p>
029 * Likewise, during rewind, {@link #getValue(Object)} is invoked for each key, to get back the same
030 * (or equivalent) object. Again, the method is invoked just before the For's body is rendered.
031 * <p>
032 * The {@link org.apache.tapestry.util.DefaultPrimaryKeyConverter} uses this relationship between a
033 * For component and its primary key converter to track what the current value being rendered or
034 * rewound is.
035 * 
036 * @author mb
037 * @since 4.0
038 */
039public interface IPrimaryKeyConverter
040{
041    /**
042     * Returns the primary key of the given value
043     * 
044     * @param objValue
045     *            the value for which a primary key needs to be extracted
046     * @return the primary key of the value
047     */
048    Object getPrimaryKey(Object value);
049
050    /**
051     * Returns the value corresponding the given primary key
052     * 
053     * @param objPrimaryKey
054     *            the primary key for which a value needs to be generated
055     * @return the generated value corresponding to the given primary key
056     */
057    Object getValue(Object primaryKey);
058
059}