/*
 * Copyright 2012 Shared Learning Collaborative, LLC
 *
 * 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.slc.sli.modeling.tools.selector;

import java.util.Map;
import java.util.List;
import java.io.FileNotFoundException;

import org.slc.sli.modeling.uml.Model;
import org.slc.sli.modeling.uml.ClassType;
import org.slc.sli.modeling.uml.Attribute;
import org.slc.sli.modeling.uml.index.DefaultModelIndex;
import org.slc.sli.modeling.uml.index.ModelIndex;
import org.slc.sli.modeling.uml.AssociationEnd;
import org.slc.sli.modeling.xmi.reader.XmiReader;

/**
 * Command line utility to read an XMI File, and print out features (attributes,
 * associations) of each element type.
 *
 * @author wscott
 *
 */
public class SelectorDoc {

    public static void main(String[] args) {
        final Model model;
        final ModelIndex mi;
        try {
            model = XmiReader.readModel(args[0]);
        } catch (FileNotFoundException e) {
            return;
        }

        if (model != null) {
            mi = new DefaultModelIndex(model);
        } else {
            return;
        }

        String simpleSect = "<simpleSect xml:id = \"selector-%s\">";
        String features = "    <features>";
        String feature = "        <feature type = \"%s\" name = \"%s\"/>";
        String featuresEnd = "    </features>";
        String simpleSectEnd = "</simpleSect>";

        System.out.println("=== ClassTypes ===");

        Map<String, ClassType> classTypes = mi.getClassTypes();
        for(String classTypeKey : classTypes.keySet()) {

            ClassType classType = classTypes.get (classTypeKey);
            String classTypeName = classType.getName();
            List<Attribute> attributes = classType.getAttributes();
            boolean isAssociation = classType.isAssociation();

            System.out.println (String.format (simpleSect, classTypeName));
            System.out.println (features);

            String type = "";
            String name = "";

            if (classType.isAssociation()) {
                type = "Association";
                AssociationEnd lhs = classType.getLHS();
                AssociationEnd rhs = classType.getRHS();
                String lhsName = lhs.getAssociatedAttributeName();
                String rhsName = rhs.getAssociatedAttributeName();

                System.out.println (String.format (feature, type, lhsName));
                System.out.println (String.format (feature, type, rhsName));

                for (Attribute attribute : attributes) {
                    type = "Attribute";
                    name = attribute.getName();
                    System.out.println (String.format (feature, type, name));
                }

            } else {
                type = "Attribute";
                for (Attribute attribute : attributes) {
                    name = attribute.getName();
                    System.out.println (String.format (feature, type, name));
                }
            }

            System.out.println (featuresEnd);
            System.out.println (simpleSectEnd);
        }

    }

}

@@ -1911,46 +1911,49 @@
    S
-ystem.out.println(%22=== ClassTypes ===%22
+tringBuffer selectors = new StringBuffer(
 );%0A%0A
@@ -2211,158 +2211,24 @@
     
-List%3CAttribute%3E attributes = classType.getAttributes();%0A            boolean isAssociation = classType.isAssociation();%0A%0A            System.out.println
+selectors.append
  (St
@@ -2262,24 +2262,31 @@
 assTypeName)
+ + %22%5Cn%22
 );%0A         
@@ -2288,34 +2288,32 @@
         
-System.out.println
+selectors.append
  (featur
@@ -2314,16 +2314,23 @@
 features
+ + %22%5Cn%22
 );%0A%0A    
@@ -2384,16 +2384,84 @@
 me = %22%22;
+%0A            List%3CAttribute%3E attributes = classType.getAttributes();
 %0A%0A      
@@ -2671,93 +2671,57 @@
     
-String lhsName = lhs.getAssociatedAttributeName();%0A                String rhsName = r
+selectors.append (String.format (feature, type, l
 hs.g
@@ -2747,18 +2747,26 @@
 teName()
+) + %22%5Cn%22)
 ;
-%0A
 %0A       
@@ -2774,34 +2774,32 @@
         
-System.out.println
+selectors.append
  (String
@@ -2826,93 +2826,48 @@
 pe, 
-lhsName));%0A                System.out.println (String.format (feature, type, rhsName)
+rhs.getAssociatedAttributeName()) + %22%5Cn%22
 );%0A%0A
@@ -2987,74 +2987,24 @@
     
-name = attribute.getName();%0A                    System.out.println
+selectors.append
  (St
@@ -3023,37 +3023,59 @@
 (feature, type, 
-name)
+attribute.getName()) + %22%5Cn%22
 );%0A             
@@ -3218,74 +3218,24 @@
     
-name = attribute.getName();%0A                    System.out.println
+selectors.append
  (St
@@ -3266,13 +3266,35 @@
 pe, 
-name)
+attribute.getName()) + %22%5Cn%22
 );%0A 
@@ -3337,34 +3337,32 @@
         
-System.out.println
+selectors.append
  (featur
@@ -3366,16 +3366,23 @@
 turesEnd
+ + %22%5Cn%22
 );%0A     
@@ -3392,26 +3392,24 @@
     
-System.out.println
+selectors.append
  (si
@@ -3419,16 +3419,23 @@
 eSectEnd
+ + %22%5Cn%22
 );%0A     
@@ -3436,21 +3436,62 @@
 %0A        %7D%0A%0A
+        System.out.println (selectors);%0A%0A
     %7D%0A%0A%7D%0A

put xml into stringBuffer, clean up code.