Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Copyright (c) 2011, 2013 Oracle and/or its affiliates. All rights reserved. This program and the accompanying materials are made available under the terms of the Eclipse Public License v1.0 and Eclipse Distribution License v. 1.0 which accompanies this distribution. The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html and the Eclipse Distribution License is available at http://www.eclipse.org/org/documents/edl-v10.php. /
 
 package org.eclipse.persistence.jpa.rs.resources.common;
 
 import java.net.URI;
 import java.util.List;
 import java.util.Map;
 
 import  javax.ws.rs.core.PathSegment;
 import  javax.ws.rs.core.UriInfo;
 
 import  org.eclipse.persistence.jaxb.JAXBContext;
 import  org.eclipse.persistence.jaxb.JAXBContextFactory;
 import  org.eclipse.persistence.jaxb.MarshallerProperties;

Author(s):
gonural
 
 
 public abstract class AbstractResource {
     public static final String SERVICE_VERSION_FORMAT = "v\\d\\.\\d"
     protected PersistenceContextFactory factory;
 
     public static final String SERVICE_VERSION_1_0 = "v1.0"

    
Sets the persistence factory.

Parameters:
factory the new persistence factory
 
     public void setPersistenceFactory(PersistenceContextFactory factory) {
         this. = factory;
     }

    
Gets the persistence factory.

Returns:
the persistence factory
 
         return getPersistenceFactory(Thread.currentThread().getContextClassLoader());
     }
    
Gets the persistence factory.

Returns:
the persistence factory
 
         if ( == null) {
              = buildPersistenceContextFactory(loader);
         }
         return ;
     }
     
         ServiceLoader<PersistenceContextFactoryProvidercontextFactoryLoader = ServiceLoader.load(PersistenceContextFactoryProvider.classloader);
 
         for (PersistenceContextFactoryProvider providercontextFactoryLoader){
             PersistenceContextFactory factory = provider.getPersistenceContextFactory(null);
             if (factory != null){
                 return factory;
             }
         }
        return null;
    }

    
Get a map of the matrix parameters associated with the URI path segment of the current request In JPA-RS, things that user sets (such as parameters of named queries, etc.) are treated as matrix parameters List of valid matrix parameters for JPA-RS is defined in MatrixParameters

Parameters:
info the info
segment the segment
Returns:
the matrix parameters
See also:
MatrixParameters
    protected static Map<StringStringgetMatrixParameters(UriInfo infoString segment) {
        Map<StringStringmatrixParameters = new HashMap<StringString>();
        for (PathSegment pathSegment : info.getPathSegments()) {
            if (pathSegment.getPath() != null && pathSegment.getPath().equals(segment)) {
                for (Entry<StringList<String>> entry : pathSegment.getMatrixParameters().entrySet()) {
                    matrixParameters.put(entry.getKey(), entry.getValue().get(0));
                }
                return matrixParameters;
            }
        }
        return matrixParameters;
    }

    
Get the URI query parameters of the current request In JPA-RS, predefined attributes (such as eclipselink query hints) are treated as query parameters List of valid query parameters for JPA-RS is defined in QueryParameters

Parameters:
info the info
Returns:
the query parameters
See also:
QueryParameters
    protected static Map<StringObjectgetQueryParameters(UriInfo info) {
        Map<StringObjectqueryParameters = new HashMap<StringObject>();
        for (String key : info.getQueryParameters().keySet()) {
            queryParameters.put(keyinfo.getQueryParameters().getFirst(key));
        }
        return queryParameters;
    }
    
    
Creates the shell jaxb element list.

Parameters:
reportItems the report items
Returns:
the list
    @SuppressWarnings({ "rawtypes""unchecked" })
    protected List<JAXBElementcreateShellJAXBElementList(List<ReportItemreportItemsObject record) {
        List<JAXBElementjaxbElements = new ArrayList<JAXBElement>(reportItems.size());
        if ((reportItems != null) && (reportItems.size() > 0)) {
            for (int index = 0; index < reportItems.size(); index++) {
                ReportItem reportItem = reportItems.get(index);
                Object reportItemValue = record;
                if (record instanceof Object[]) {
                    reportItemValue = ((Object[]) record)[index];
                }
                Class reportItemValueType = null;
                if (reportItemValue != null) {
                    reportItemValueType = reportItemValue.getClass();
                }
                if (reportItemValueType == null) {
                    // try other paths to determine the type of the report item 
                    DatabaseMapping dbMapping = reportItem.getMapping();
                    if (dbMapping != null) {
                        reportItemValueType = dbMapping.getAttributeClassification();
                    } else {
                        ClassDescriptor desc = reportItem.getDescriptor();
                        if (desc != null) {
                            reportItemValueType = desc.getJavaClass();
                        } 
                    }
                }
                // so, we couldn't determine the type of the report item, stop here... 
                if (reportItemValueType == null) {
                    return null;
                }
                
                JAXBElement element = new JAXBElement(new QName(reportItem.getName()), reportItemValueTypereportItemValue);
                jaxbElements.add(reportItem.getResultIndex(), element);
            }
        }
        return jaxbElements;
    }
    
    
    protected static boolean isValidVersion(String version) {
        if ((version == null) || (.equals(version))) {
            return true;
        }
        return false;
    }
    
    protected PersistenceContext getPersistenceContext(String persistenceUnitURI baseURIString versionMap<StringObjectinitializationProperties) {
        if (!isValidVersion(version)) {
            JPARSLogger.fine("unsupported_service_version_in_the_request"new Object[] { version });
            throw new IllegalArgumentException();
        }
        return getPersistenceFactory().get(persistenceUnitbaseURIversioninitializationProperties);
    }
    
    protected String getRelationshipPartner(Map<StringStringmatrixParamsMap<StringObjectqueryParams) {
        String partner = null;
        // Fix for Bug 396791 - JPA-RS: partner should be treated as a query parameter
        //
        // For backwards compatibility, we check both, matrix and query parameters.
        if ((queryParams != null) && (!queryParams.isEmpty())) {
            partner = (StringqueryParams.get(.);
        }
        if (partner == null) {
            if ((matrixParams != null) && (!matrixParams.isEmpty())) {
                partner = (StringmatrixParams.get(.);
            }
        }
        return partner;
    }
    
    protected String marshallMetadata(Object metadataString mediaTypethrows JAXBException {
        Class<?>[] jaxbClasses = new Class[] { Link.classAttribute.classDescriptor.classLinkTemplate.classPersistenceUnit.classQuery.classLinkList.classQueryList.class };
        JAXBContext context = (JAXBContext) JAXBContextFactory.createContext(jaxbClassesnull);
        Marshaller marshaller = context.createMarshaller();
        marshaller.setProperty(MarshallerProperties.JSON_INCLUDE_ROOT, .);
        marshaller.setProperty(MarshallerProperties.MEDIA_TYPE, mediaType);
        marshaller.setProperty(MarshallerProperties.JSON_REDUCE_ANY_ARRAYS, true);
        StringWriter writer = new StringWriter();
        marshaller.marshal(metadatawriter);
        return writer.toString();
    }
New to GrepCode? Check out our FAQ X