001    /**
002     * DefineNewIdsMethod.java
003     * jCOLIBRI2 framework. 
004     * @author Juan A. Recio-García.
005     * GAIA - Group for Artificial Intelligence Applications
006     * http://gaia.fdi.ucm.es
007     * 07/01/2007
008     */
009    package jcolibri.method.revise;
010    
011    import java.util.HashMap;
012    
013    import jcolibri.cbrcore.Attribute;
014    import jcolibri.cbrcore.CBRCase;
015    import jcolibri.cbrcore.CaseComponent;
016    
017    
018    
019    /**
020     * Defines new ids for the case components of a case. This way it can be stored in the persistence media without overwriting an existing case.
021     * @author Juan A. Recio-Garcia
022     *
023     */
024    public class DefineNewIdsMethod {
025    
026            /**
027             * Changes the values of the ID attributes of a case with new ones. 
028             * This method traverses the CaseComponent tree of a case modifing the values of the ids attributes with new objects.
029             * @param _case to modify the ids 
030             * @param componentsKeys stores the new values of the IDs attributes
031             * @throws jcolibri.exception.ExecutionException
032             */
033            public static void defineNewIdsMethod(CBRCase _case, HashMap<Attribute, Object> componentsKeys) throws jcolibri.exception.ExecutionException
034            {
035                            defineNewIds(_case.getDescription(), componentsKeys);
036                            defineNewIds(_case.getSolution(), componentsKeys);
037                            defineNewIds(_case.getJustificationOfSolution(), componentsKeys);
038                            defineNewIds(_case.getResult(), componentsKeys);
039            }
040            
041            private static void defineNewIds(CaseComponent cc, HashMap<Attribute, Object> componentsKeys) throws jcolibri.exception.ExecutionException
042            {
043                    if(cc == null)
044                            return;
045                    Attribute keyAtt = cc.getIdAttribute();
046                    Object newkeyvalue = componentsKeys.get(keyAtt);
047                    
048                    try {
049                            if(newkeyvalue != null)
050                                    keyAtt.setValue(cc, newkeyvalue);
051    
052                            for(java.lang.reflect.Field f: cc.getClass().getDeclaredFields())
053                            {
054                                    Attribute at = new Attribute(f);
055                                    Object o = at.getValue(cc);
056                                    if(o instanceof CaseComponent)
057                                            defineNewIds((CaseComponent)o, componentsKeys);
058                            }
059                    } catch (Exception e) {
060                            org.apache.commons.logging.LogFactory.getLog(DefineNewIdsMethod.class).error(e);
061                    } 
062                    
063            }
064    }