All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.viaoa.context.OAContext Maven / Gradle / Ivy

There is a newer version: 3.7.10
Show newest version
/*  Copyright 1999 Vince Via [email protected]
    Licensed 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 com.viaoa.context;

import java.util.concurrent.ConcurrentHashMap;
import com.viaoa.hub.Hub;
import com.viaoa.object.OAObject;
import com.viaoa.util.OAConv;
import com.viaoa.util.OAString;

public class OAContext {
    private static final ConcurrentHashMap> hmContextHub = new ConcurrentHashMap<>();
    private static final Object NullContext = new Object();

    
    /**
     * Property path used to find the user property for allowing users to edit objects/properties/etc that are annotatied as processed.
     * Defaults to "EditProcessed"
     */
    private static String allowEditProcessedPropertyPath = "EditProcessed"; 
    public static void setAllowEditProcessedPropertyPath(String pp) {
        OAContext.allowEditProcessedPropertyPath = pp;
    }
    public static String getAllowEditProcessedPropertyPath() {
        return OAContext.allowEditProcessedPropertyPath;
    }
    public static boolean canEditProcessed() {
        return canEditProcessed(NullContext);
    }
    public static boolean canEditProcessed(Object context) {
        if (context == null) context = NullContext;
        if (OAString.isEmpty(allowEditProcessedPropertyPath)) return false;
        OAObject oaObj = getContext(context);
        if (oaObj == null) return false;
        
        Object val = oaObj.getProperty(OAContext.allowEditProcessedPropertyPath);
        boolean b = OAConv.toBoolean(val);
        return b;
    }
    

    private static String allowAdminEditPropertyPath = "EditProcessed"; 
    public static void setAllowAdminEditPropertyPath(String pp) {
        OAContext.allowAdminEditPropertyPath = pp;
    }
    public static String getAllowAdminEditPropertyPath() {
        return OAContext.allowAdminEditPropertyPath;
    }
    public static boolean canAdminEdit() {
        return canAdminEdit(NullContext);
    }
    public static boolean isAdmin() {
        return canAdminEdit(null);
    }
    public static boolean isAdmin(Object context) {
        return canAdminEdit(context);
    }
    public static boolean canAdminEdit(Object context) {
        if (context == null) context = NullContext;
        if (OAString.isEmpty(allowAdminEditPropertyPath)) return false;
        OAObject oaObj = getContext(context);
        if (oaObj == null) return false;
        
        Object val = oaObj.getProperty(OAContext.allowAdminEditPropertyPath);
        boolean b = OAConv.toBoolean(val);
        return b;
    }
    
    public static void setContext(Object context, OAObject obj) {
        if (obj == null) return;
        if (context == null) context = NullContext;
        Hub h = new Hub();
        h.add(obj);
        h.setAO(obj);
        addContextHub(context, h);
    }
    public static OAObject getContext() {
        return getContext(null);
    }
    public static OAObject getContext(Object context) {
        Hub hub = getContextHub(context);
        if (hub == null) return null;
        return hub.getAO();
    }

    
    public static void addContextHub(Object context, Hub hub) {
        if (hub == null) return;
        if (context == null) context = NullContext;
        if (hub.getAO() == null) hub.setPos(0);
        hmContextHub.put(context, hub);
    }
    public static void removeContextHub(Object context) {
        if (context == null) context = NullContext;
        hmContextHub.remove(context);
    }

    public static Hub getContextHub() {
        return getContextHub(null);
    }
    public static Hub getContextHub(Object context) {
        // if (context == null)  //todo: qqqqqq look in threadLocal            
        if (context == null) context = NullContext;
        return hmContextHub.get(context); 
    }

    
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy