/*
 * Copyright (c) 2010-2018 Evolveum
 *
 * 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.evolveum.midpoint.repo.sql.helpers.mapper;

import com.evolveum.midpoint.repo.sql.data.common.enums.SchemaEnum;
import com.evolveum.midpoint.repo.sql.helpers.modify.MapperContext;
import com.evolveum.midpoint.repo.sql.util.RUtil;
import com.evolveum.midpoint.util.exception.SystemException;
import org.apache.commons.lang.StringUtils;

/**
 * Created by Viliam Repan (lazyman).
 */
public class EnumMapper implements Mapper<Enum, SchemaEnum> {

    @Override
    public SchemaEnum map(Enum input, MapperContext context) {
        String repoEnumClass = null;
        try {
            String className = input.getClass().getSimpleName();
            className = StringUtils.left(className, className.length() - 4);

            repoEnumClass = "com.evolveum.midpoint.repo.sql.data.common.enums.R" + className;
            Class clazz = Class.forName(repoEnumClass);

            if (!SchemaEnum.class.isAssignableFrom(clazz)) {
                throw new SystemException("Can't translate enum value " + input);
            }

            return RUtil.getRepoEnumValue(input, clazz);
        } catch (ClassNotFoundException ex) {
            throw new SystemException("Couldn't find class '" + repoEnumClass + "' for enum '" + input + "'", ex);
        }
    }
}

@@ -652,16 +652,85 @@
 apper;%0A%0A
+import com.evolveum.midpoint.repo.sql.data.common.enums.RExportType;%0A
 import c
@@ -789,16 +789,16 @@
 maEnum;%0A
-
 import c
@@ -968,16 +968,88 @@
 eption;%0A
+import com.evolveum.midpoint.xml.ns._public.common.common_3.ExportType;%0A
 import o
@@ -1387,16 +1387,190 @@
 Name();%0A
+            Class clazz;%0A            if (input instanceof ExportType) %7B%0A                clazz = RExportType.class;      // todo fix this brutal hack%0A            %7D else %7B%0A    
         
@@ -1638,17 +1638,17 @@
 ) - 4);%0A
-%0A
+ 
         
@@ -1651,16 +1651,19 @@
         
+   
 repoEnum
@@ -1744,29 +1744,27 @@
             
-Class
+   
  clazz = Cla
@@ -1789,16 +1789,30 @@
 mClass);
+%0A            %7D
 %0A%0A      

Fix exception when changing report output type