package org.sliner.impl;

import org.jmotor.util.CollectionUtilities;
import org.jmotor.util.StringUtilities;
import org.sliner.Condition;
import org.sliner.Sorter;
import org.sliner.SqlLiner;
import org.sliner.SqlWrapper;
import org.sliner.generator.SelectionGenerator;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Component:
 * Description:
 * Date: 13-6-19
 *
 * @author Andy Ai
 */
public class SqlLinerImpl implements SqlLiner {
    private SelectionGenerator selectionGenerator;

    @Override
    public SqlWrapper wrap(String key, Map<String, String> conditions, String operator, List<String> sorters) {
        StringBuilder sqlBuilder = new StringBuilder("select * from ");
        sqlBuilder.append(selectionGenerator.generateEntityName(key));
        List<Condition> _conditions = selectionGenerator.generateConditions(key, conditions);
        SqlWrapper sqlWrapper = new SqlWrapper();
        if (CollectionUtilities.isNotEmpty(_conditions)) {
            sqlBuilder.append(" WHERE ");
            List<String> lines = new ArrayList<String>(_conditions.size());
            List<String> names = new ArrayList<String>(_conditions.size());
            List<Object> values = new ArrayList<Object>(_conditions.size());
            for (Condition condition : _conditions) {
                StringBuilder lineBuilder = new StringBuilder();
                lineBuilder.append(condition.getColumnName());
                lineBuilder.append(" ");
                lineBuilder.append(condition.getOperator().getOperator());
                lineBuilder.append(" ?");
                lines.add(lineBuilder.toString());
                names.add(condition.getName());
                values.add(condition.getValue());
            }
            sqlBuilder.append(StringUtilities.join(lines, StringUtilities.surround(operator, " ")));
            sqlWrapper.setNames(names.toArray(new String[names.size()]));
            sqlWrapper.setValues(values.toArray(new Object[values.size()]));
        }
        List<Sorter> _sorters = selectionGenerator.generateSorters(key, sorters);
        if (CollectionUtilities.isNotEmpty(_sorters)) {
            sqlBuilder.append(" ORDER BY ");
            List<String> lines = new ArrayList<String>(_sorters.size());
            for (Sorter sorter : _sorters) {
                lines.add(sorter.getColumnName() + " " + sorter.getOperator().getOperator());
            }
            sqlBuilder.append(StringUtilities.join(lines, StringUtilities.COMMA));
        }
        sqlWrapper.setSql(sqlBuilder.toString());
        return sqlWrapper;
    }

    public void setSelectionGenerator(SelectionGenerator selectionGenerator) {
        this.selectionGenerator = selectionGenerator;
    }
}

@@ -710,24 +710,74 @@
  * from %22);%0A
+        SqlWrapper sqlWrapper = new SqlWrapper();%0A
         sqlB
@@ -831,24 +831,86 @@
 Name(key));%0A
+        if (CollectionUtilities.isNotEmpty(conditions)) %7B%0A    
         List
@@ -999,54 +999,8 @@
     
-    SqlWrapper sqlWrapper = new SqlWrapper();%0A
     
@@ -1058,32 +1058,36 @@
 ) %7B%0A            
+   
+ 
 sqlBuilder.appen
@@ -1096,24 +1096,28 @@
 %22 WHERE %22);%0A
+    
             
@@ -1184,32 +1184,36 @@
 ));%0A            
+    
 List%3CString%3E nam
@@ -1268,24 +1268,28 @@
             
+ 
+   
 List%3CObject%3E
@@ -1349,24 +1349,28 @@
             
+    
 for (Conditi
@@ -1395,24 +1395,28 @@
 nditions) %7B%0A
+    
             
@@ -1476,32 +1476,36 @@
                 
+    
 lineBuilder.appe
@@ -1543,32 +1543,36 @@
                 
+    
 lineBuilder.appe
@@ -1588,32 +1588,36 @@
                 
+   
+ 
 lineBuilder.appe
@@ -1667,32 +1667,36 @@
                 
+    
 lineBuilder.appe
@@ -1713,32 +1713,36 @@
                 
+ 
+   
 lines.add(lineBu
@@ -1776,16 +1776,20 @@
         
+  
+  
 names.ad
@@ -1804,32 +1804,36 @@
 ion.getName());%0A
+    
                 
@@ -1862,24 +1862,28 @@
 etValue());%0A
+    
             
@@ -1888,32 +1888,36 @@
   %7D%0A            
+    
 sqlBuilder.appen
@@ -1993,32 +1993,36 @@
 ));%0A            
+    
 sqlWrapper.setNa
@@ -2071,32 +2071,36 @@
 ));%0A            
+   
+ 
 sqlWrapper.setVa
@@ -2148,34 +2148,107 @@
 e()%5D));%0A        
+    
 %7D%0A
+        %7D%0A        if (CollectionUtilities.isNotEmpty(sorters)) %7B%0A    
         List%3CSor
@@ -2313,32 +2313,36 @@
 rters);%0A        
+    
 if (CollectionUt
@@ -2377,32 +2377,36 @@
 ) %7B%0A            
+    
 sqlBuilder.appen
@@ -2426,32 +2426,36 @@
 %22);%0A            
+   
+ 
 List%3CString%3E lin
@@ -2491,32 +2491,36 @@
 orters.size());%0A
+    
             for 
@@ -2556,32 +2556,36 @@
                 
+    
 lines.add(sorter
@@ -2650,34 +2650,42 @@
 ));%0A            
+ 
-%7D%0A
+   %7D%0A    
             sqlB
@@ -2747,24 +2747,38 @@
 es.COMMA));%0A
+            %7D%0A
         %7D%0A  

Handle null point exception