Source

grails-propertyset / src / groovy / com / opensymphony / module / propertyset / gorm / GroovyPropertySet.groovy

package com.opensymphony.module.propertyset.gorm

import com.opensymphony.module.propertyset.*

/**
 * Copyright (c) 2012, Christian Junk
 * All rights reserved.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */
class GroovyPropertySet extends AbstractGroovyPropertySet {

    private Long entityId;
    private String entityName;

    @Override
    void init(Map config, Map args) {
        super.init(config, args)

        this.entityId = (Long) args.get("entityId")
        this.entityName = (String) args.get("entityName")
    }

    @Override
    protected void setImpl(int type, String key, Object value) {

        def item = PropertyEntry.findByEntityNameAndEntityIdAndPropertyKey( entityName, entityId, key )

        if (item == null) {
            item = new PropertyEntry(entityName: entityName, entityId: entityId, propertyKey: key)
        }
        else if (item.getPropertyType() != type)
        {
            throw new PropertyException("Existing key '" + key + "' does not have matching type of " + type);
        }

        switch (type)
        {
            case BOOLEAN:
                PropertyNumber propertyNumber = item.getNumberVal()

                if (propertyNumber == null) {
                    def numberVal = new PropertyNumber()
                    numberVal.setPropertyValue( ((Boolean) value).booleanValue() ? 1 : 0 )
                    item.setNumberVal( numberVal )
                }
                else {
                    propertyNumber.setPropertyValue( ((Boolean) value).booleanValue() ? 1 : 0 )
                }
                break;

            case DOUBLE:
                PropertyDecimal propertyDecimal = item.getDecimalVal()

                if (propertyDecimal == null) {
                    def decimalVal = new PropertyDecimal()
                    decimalVal.setPropertyValue( ((Double) value).doubleValue() )
                    item.setDecimalVal( decimalVal )
                }
                else {
                    propertyDecimal.setPropertyValue( ((Double) value).doubleValue() )
                }
                break;

            case STRING:
                PropertyString propertyString = item.getStringVal()

                if (propertyString == null) {
                    def stringVal = new PropertyString()
                    stringVal.setPropertyValue( (String) value )
                    item.setStringVal( stringVal )
                }
                else {
                    propertyString.setPropertyValue( (String) value )
                }
                break;

            case TEXT:
                PropertyText propertyText = item.getTextVal()

                if (propertyText == null) {
                    def textVal = new PropertyText()
                    textVal.setPropertyValue( (String) value )
                    item.setTextVal( textVal )
                }
                else {
                    propertyText.setPropertyValue( (String) value )
                }
                break;

            case LONG:

                PropertyNumber propertyNumber = item.numberVal

                if (propertyNumber == null) {
                    def numberVal = new PropertyNumber()
                    numberVal.setPropertyValue( ((Long) value).longValue() )
                    item.setNumberVal(  numberVal )
                }
                else {
                    propertyNumber.setPropertyValue( ((Long) value).longValue() )
                }
                break;

            case INT:
                PropertyNumber propertyNumber = item.numberVal

                if (propertyNumber == null) {
                    def numberVal = new PropertyNumber()
                    numberVal.setPropertyValue( ((Long) value).longValue() )
                    item.setNumberVal(  numberVal )
                }
                else {
                    propertyNumber.setPropertyValue( ((Long) value).longValue() )
                }
                break;

            case DATE:
                PropertyDate propertyDate = item.dateVal

                if (propertyDate == null) {
                    def dateVal = new PropertyDate()
                    dateVal.setPropertyValue( (Date) value )
                    item.setDateVal( dateVal )
                }
                else {
                    propertyDate.setPropertyValue( (Date) value )
                }
                break;

            default:
                throw new PropertyException("type " + type + " not supported");
        }

        item.setPropertyType(type);

        item.save(failOnError: true, flush: true)
    }

    @Override
    protected Object get(int type, String key) {

        def item = PropertyEntry.findByPropertyKey(key)

        if (item == null)
        {
            return null;
        }

        if (item.getPropertyType() != type)
        {
            throw new PropertyException("key '" + key + "' does not have matching type of " + type);
        }

        switch (type)
        {
            case BOOLEAN:
                return new Boolean( item.getNumberVal().getPropertyValue() != 0 )

            case DOUBLE:
                return item.getDecimalVal().getPropertyValue()

            case STRING:
                return item.getStringVal().getPropertyValue()

            case TEXT:
                return item.getTextVal().getPropertyValue()

            case LONG:
                return item.getNumberVal().getPropertyValue()

            case INT:
                return  item.getNumberVal().getPropertyValue().intValue()

            case DATE:
                return item.getDateVal().getPropertyValue();
        }

        throw new PropertyException("type " + type + " not supported");
    }

    Collection getKeys(String prefix, int type) {

        if ((prefix != null) && (type > 0)) {
            // all_keys_with_type_like
            PropertyEntry.findAllByEntityIdAndEntityNameAndPropertyType(entityId, entityName, type).collect { item ->
                item.propertyKey.startsWith(prefix)
            }

        } else if (prefix != null) {
            // all_keys_like
            PropertyEntry.findAllByEntityIdAndEntityName(entityId, entityName).collect { item ->
                item.propertyKey.startsWith(prefix)
            }
        } else if (type > 0) {
            // all_keys_with_type
            PropertyEntry.findAllByEntityIdAndEntityNameAndPropertyType(entityId, entityName, type)
        } else {
            // all_keys
            PropertyEntry.findAllByEntityIdAndEntityName(entityId, entityName)
        }

    }

    int getType(String key) {
        PropertyEntry.findByEntityIdAndEntityNameAndPropertyKey(entityId, entityName, key).getPropertyType();
    }

    boolean exists(String key) {
        return ( PropertyEntry.findByEntityIdAndEntityNameAndPropertyKey(entityId, entityName, key) == null )
    }

    void remove(String key) {
        def item =  PropertyEntry.findByEntityIdAndEntityNameAndPropertyKey(entityId, entityName, key)
        item.delete( failOnError: true, flush: true )
    }

    void remove() {
        PropertyEntry.executeUpdate( "delete PropertyEntry p where p.entityId = :entityId and p.entityName = :entityName", [entityId: entityId, entityName: entityName] )
    }
}
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.