Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. /
 
 
 package org.apache.cayenne.pref;
 
 

Author(s):
Andrei Adamchik
 
 public class DomainPreference extends _DomainPreference {
 
     protected DomainProperties properties;
 
     protected Properties getProperties() {
 
         if ( == null) {
              = new DomainProperties();
 
             String values = getKeyValuePairs();
             if (values != null && values.length() > 0) {
                 try {
                     .load(new ByteArrayInputStream(values.getBytes()));
                 }
                 catch (IOException ex) {
                     throw new PreferenceException("Error loading properties."ex);
                 }
             }
         }
 
         return ;
     }
 
     protected void encodeProperties() {
         if (this. == null) {
             setKeyValuePairs(null);
         }
         else {
             ByteArrayOutputStream out = new ByteArrayOutputStream();
 
             try {
                 .store(outnull);
             }
             catch (IOException ex) {
                 throw new PreferenceException("Error storing properties."ex);
             }
 
             setKeyValuePairs(out.toString());
         }
     }

    
Returns a generic preference for associated with a given DomainPreference.
 
     protected PreferenceDetail getPreference() {
         PreferenceDetail preference = new PreferenceDetail();
         preference.setDomainPreference(this);
         return preference;
     }

    
Locates and returns a detail preference of a given class.
 
     protected PreferenceDetail getPreference(Class javaClassboolean create) {
 
         // detail object PK must match...
 
         int pk = DataObjectUtils.intPKForObject(this);
         PreferenceDetail preference = (PreferenceDetail) DataObjectUtils
                 .objectForPK(getDataContext(), javaClasspk);
 
         if (preference != null) {
             preference.setDomainPreference(this);
         }
 
         if (preference != null || !create) {
            return preference;
        }
        preference = (PreferenceDetailgetDataContext()
                .createAndRegisterNewObject(javaClass);
        preference.setDomainPreference(this);
        getDataContext().commitChanges();
        return preference;
    }

    
Overrides super implementation to handle non-persistent properties on object state changes.
    public void setPersistenceState(int state) {
        // if invalidated
        if (state == .) {
             = null;
        }
        super.setPersistenceState(state);
    }
    class DomainProperties extends Properties {
        public Object setProperty(String keyString value) {
            Object old = super.setProperty(keyvalue);
            if (!Util.nullSafeEquals(oldvalue)) {
                modified();
            }
            return old;
        }
        void modified() {
            // more efficient implementation should only call encode on commit using
            // DataContext events... still there is a bug that prevents DataObject from
            // changing its state during "willCommit", so for now do this...
            encodeProperties();
        }
    }
New to GrepCode? Check out our FAQ X