<commit_before>/*
 * Copyright 2004-2006 the original author or authors.
 *
 * 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 org.compass.core.converter.mapping.osem;

import org.compass.core.Resource;
import org.compass.core.converter.ConversionException;
import org.compass.core.converter.Converter;
import org.compass.core.mapping.Mapping;
import org.compass.core.mapping.osem.ClassMapping;
import org.compass.core.mapping.osem.HasRefAliasMapping;
import org.compass.core.marshall.MarshallingContext;
import org.compass.core.marshall.MarshallingEnvironment;

/**
 * @author kimchy
 */
public abstract class AbstractRefAliasMappingConverter implements Converter {

    public boolean marshall(Resource resource, Object root, Mapping mapping, MarshallingContext context) throws ConversionException {
        // no need to marshall if it is null
        if (root == null) {
            return false;
        }
        HasRefAliasMapping hasRefAliasMapping = (HasRefAliasMapping) mapping;
        ClassMapping[] classMappings = hasRefAliasMapping.getRefClassMappings();
        ClassMapping classMapping;
        if (classMappings.length == 1) {
            classMapping = classMappings[0];
        } else {
            classMapping = context.getCompassMapping().getClassMappingByClass(root.getClass());
            if (classMapping == null) {
                throw new ConversionException("No class mapping found when marshalling root alias [" 
                        + resource.getAlias() + "] and class [" + root.getClass() + "]");
            }
            classMapping = hasRefAliasMapping.getRefClassMapping(classMapping.getAlias());
            if (classMapping == null) {
                throw new ConversionException("Mapping for root alias [" + resource.getAlias() + 
                        "] with one of its mappings with multiple ref-alias [" + hasRefAliasMapping.getRefAliases() 
                        + "] did not match [" + classMapping.getAlias() + "]");
            }
        }
        Object current = context.getAttribute(MarshallingEnvironment.ATTRIBUTE_CURRENT);
        context.setAttribute(MarshallingEnvironment.ATTRIBUTE_PARENT, current);
        return doMarshall(resource, root, hasRefAliasMapping, classMapping, context);
    }
    
    protected abstract boolean doMarshall(Resource resource, Object root, HasRefAliasMapping hasRefAliasMapping, 
            ClassMapping refMapping, MarshallingContext context) throws ConversionException;
    
    public Object unmarshall(Resource resource, Mapping mapping, MarshallingContext context) throws ConversionException {
        HasRefAliasMapping hasRefAliasMapping = (HasRefAliasMapping) mapping;
        ClassMapping[] classMappings = hasRefAliasMapping.getRefClassMappings();
        ClassMapping classMapping;
        if (classMappings.length == 1) {
            classMapping = classMappings[0];
        } else {
            throw new UnsupportedOperationException("Not supported yet");
        }
        Object current = context.getAttribute(MarshallingEnvironment.ATTRIBUTE_CURRENT);
        context.setAttribute(MarshallingEnvironment.ATTRIBUTE_PARENT, current);
        return doUnmarshall(resource, hasRefAliasMapping, classMapping, context);
    }
    
    protected abstract Object doUnmarshall(Resource resource, HasRefAliasMapping hasRefAliasMapping, 
            ClassMapping refMapping, MarshallingContext context) throws ConversionException;<commit_msg>Initial support for CMP-229 Allow component/reference mappings for collections to be polymorphic<commit_after>/*
 * Copyright 2004-2006 the original author or authors.
 *
 * 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 org.compass.core.converter.mapping.osem;

import org.compass.core.Property;
import org.compass.core.Resource;
import org.compass.core.converter.ConversionException;
import org.compass.core.converter.Converter;
import org.compass.core.mapping.Mapping;
import org.compass.core.mapping.osem.ClassMapping;
import org.compass.core.mapping.osem.HasRefAliasMapping;
import org.compass.core.marshall.MarshallingContext;
import org.compass.core.marshall.MarshallingEnvironment;

/**
 * @author kimchy
 */
public abstract class AbstractRefAliasMappingConverter implements Converter {

    public boolean marshall(Resource resource, Object root, Mapping mapping, MarshallingContext context) throws ConversionException {
        // no need to marshall if it is null
        if (root == null) {
            return false;
        }
        HasRefAliasMapping hasRefAliasMapping = (HasRefAliasMapping) mapping;
        ClassMapping[] classMappings = hasRefAliasMapping.getRefClassMappings();
        ClassMapping classMapping;
        if (classMappings.length == 1) {
            classMapping = classMappings[0];
        } else {
            classMapping = context.getCompassMapping().getClassMappingByClass(root.getClass());
            if (classMapping == null) {
                throw new ConversionException("No class mapping found when marshalling root alias [" 
                        + resource.getAlias() + "] and class [" + root.getClass() + "]");
            }
            classMapping = hasRefAliasMapping.getRefClassMapping(classMapping.getAlias());
            if (classMapping == null) {
                throw new ConversionException("Mapping for root alias [" + resource.getAlias() + 
                        "] with one of its mappings with multiple ref-alias [" + hasRefAliasMapping.getRefAliases() 
                        + "] did not match [" + classMapping.getAlias() + "]");
            }
        }
        Object current = context.getAttribute(MarshallingEnvironment.ATTRIBUTE_CURRENT);
        context.setAttribute(MarshallingEnvironment.ATTRIBUTE_PARENT, current);
        return doMarshall(resource, root, hasRefAliasMapping, classMapping, context);
    }
    
    protected abstract boolean doMarshall(Resource resource, Object root, HasRefAliasMapping hasRefAliasMapping, 
            ClassMapping refMapping, MarshallingContext context) throws ConversionException;
    
    public Object unmarshall(Resource resource, Mapping mapping, MarshallingContext context) throws ConversionException {
        HasRefAliasMapping hasRefAliasMapping = (HasRefAliasMapping) mapping;
        ClassMapping[] classMappings = hasRefAliasMapping.getRefClassMappings();
        ClassMapping classMapping = null;
        if (classMappings.length == 1) {
            classMapping = classMappings[0];
        } else {
            for (int i = 0; i < classMappings.length; i++) {
                if (classMappings[i].isPoly()) {
                    Property pClassName = resource.getProperty(classMappings[i].getClassPath().getPath());
                    if (pClassName != null && pClassName.getStringValue() != null) {
                        // we found the classMapping we marshalled, use it
                        classMapping = classMappings[i];
                        break;
                    }
                }
            }
            if (classMapping == null) {
                // we did not find anything stored in the index, it most have poly-class set
                for (int i = 0; i < classMappings.length; i++) {
                    if (classMappings[i].getPolyClass() != null) {
                        classMapping = classMappings[i];
                        break;
                    }
                }
            }
            if (classMapping == null) {
                // this should noy happen
                throw new ConversionException("Mapping for root alias [" + resource.getAlias() + 
                        "] with one of its mappings with multiple ref-alias [" + hasRefAliasMapping.getRefAliases() 
                        + "] could not find a matching class (either stored in the index or configured)");
            }
        }
        Object current = context.getAttribute(MarshallingEnvironment.ATTRIBUTE_CURRENT);
        context.setAttribute(MarshallingEnvironment.ATTRIBUTE_PARENT, current);
        return doUnmarshall(resource, hasRefAliasMapping, classMapping, context);
    }
    
    protected abstract Object doUnmarshall(Resource resource, HasRefAliasMapping hasRefAliasMapping, 
            ClassMapping refMapping, MarshallingContext context) throws ConversionException;
{'fraction_non_alphanumeric': 0.08115351629502573, 'fraction_numerical': 0.003966552315608919, 'mean_word_length': 2.8359375, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '381135', 'n_tokens_mistral': 2282, 'n_tokens_neox': 2220, 'n_words': 652}