Android Open Source - classy_apps Sql From Node Generator






From Project

Back to project page classy_apps.

License

The source code is released under:

GNU General Public License

If you think the Android project classy_apps listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

/**
    Copyright (C) 2014  www.cybersearch2.com.au
//from   w  w  w  .  j av  a2s.co  m
    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/> */
package au.com.cybersearch2.classyfy.data;

import java.io.IOException;
import java.io.Writer;
import java.text.MessageFormat;

import au.com.cybersearch2.classynode.Node;

/**
 * SqlFromNodeGenerator
 * Takes a Node tree referenced by root Node and writes SQL insert statements to populate "node" and "child_nodes" tables.<br/>
 * Each node name is subject to changes to conform with a program-friendly format. Single quotes in titles are SQL escaped.
 * @author Andrew Bowley
 * 15/04/2014
 */
public class SqlFromNodeGenerator
{
    static String INSERT_NODE = "insert into nodes (_parent_id, name, title, model, level) values ({0},''{1}'',''{2}'',{3},{4});";
    static String INSERT_CHILD_NODES = "insert into child_nodes (_parent, _child) values ({0}, {1});";
    static String INSERT_CATEGORIES = 
        "insert into categories ( node_id, description, created, creator, modified, modifier, identifier) values " +
        "({0},{1},{2},{3},{4},{5},{6});";
    static String INSERT_FOLDERS = 
        "insert into folders ( node_id, description, created, creator, modified, modifier, identifier, " +
        "location, hasDispositionSchedule, dispositionInstructions, dispositionAuthority) values " +
        "({0},{1},{2},{3},{4},{5},{6},{7},{8},{9}";
    static String INSERT_FOLDERS2 = ",{0});";

    
    int nodeIndex;
    
    public void generateSql(Node rootNode, Writer writer) throws IOException
    {
        nodeIndex = 1;
        depthFirst(0, rootNode.getChildren().get(0), writer);
    }

    private void depthFirst(int parentKey, Node node, Writer writer) throws IOException
    {
        FilePlanNodeType filePlanNodeType = new FilePlanNodeType(node);
        String modelId = Integer.toString(node.getModel());
        // Apply name conventions
        String name = node.getName().replace(' ','_');
        name = name.replace(',','_');
        name = name.replace('\'','_');
        name = name.replace("&", "");
        // Escape single quotes in title
        String title = node.getTitle().replace("'", "''");
        String level = Integer.toString(node.getLevel());
        // Top of tree is not persisted, so reference self at level 1
        String parent_id = Integer.toString(parentKey == 0 ? 1 : parentKey); 
        String insertNode = MessageFormat.format(INSERT_NODE, parent_id, name, title, modelId, level);
        writer.write(insertNode + System.getProperty("line.separator"));
        int primaryKey = nodeIndex++;
        String[] values = new String[filePlanNodeType.getModel() == Model.recordCategory ? 8 : 12];
        // Set ManagedRecord fields shared by both models
        // node_id
        values[0] = Integer.toString(primaryKey);
        //Map<String,Object> properties = node.getProperties();
        // description
        values[1] = Node.getProperty(node, RecordField.description.toString(), "").trim();
        // created
        values[2] = Node.getProperty(node, RecordField.created.toString(), null);
        // creator
        values[3] = Node.getProperty(node, RecordField.creator.toString(), "");
        // modified (optional)
        values[4] = Node.getProperty(node, RecordField.modified.toString(), null);
        // modified (optional)
        values[5] = Node.getProperty(node, RecordField.modifier.toString(), null);
        // identifier
        values[6] = Node.getProperty(node, RecordField.identifier.toString(), "");
        if (filePlanNodeType.getModel() == Model.recordFolder)
        {
            // location (optional)
            values[7] = Node.getProperty(node, RecordField.location.toString(), null);
            // hasDispositionSchedule
            String hasDispositionSchedule = Node.getProperty(node, RecordField.recordSearchHasDispositionSchedule.toString(), "false");
            if (hasDispositionSchedule.equals("'true'"))
                hasDispositionSchedule = "1";
            else 
                hasDispositionSchedule = "0";
            values[8] = hasDispositionSchedule;
            // dispositionInstructions (optional)
            values[9] = Node.getProperty(node, RecordField.recordSearchDispositionInstructions.toString(), null);
            // dispositionAuthority (optional)
            values[10] = Node.getProperty(node, RecordField.recordSearchDispositionAuthority.toString(), null);
            StringBuilder insertRecordFolder = new StringBuilder(
                    MessageFormat.format(INSERT_FOLDERS, values[0], values[1], values[2], values[3], values[4], values[5], values[6], values[7], values[8], values[9]));
            insertRecordFolder.append(
                    MessageFormat.format(INSERT_FOLDERS2, values[10]));
            writer.write(insertRecordFolder.toString() + System.getProperty("line.separator"));
        }
        else
        {
            String insertRecordCategory = 
                    MessageFormat.format(INSERT_CATEGORIES, values[0], values[1], values[2], values[3], values[4], values[5], values[6]);

            writer.write(insertRecordCategory + System.getProperty("line.separator"));
        }
        if (parentKey != 0)
        {
            String insertChild = MessageFormat.format(INSERT_CHILD_NODES, Integer.toString(parentKey), Integer.toString(primaryKey));
            writer.write(insertChild + System.getProperty("line.separator"));
        }
        for (Node childNode: node.getChildren())
        {
            depthFirst(primaryKey, childNode, writer);
        }
    }

    public SqlFromNodeGenerator()
    {
    }

}




Java Source Code List

au.com.cybersearch2.classyfy.ClassyFyApplicationModule.java
au.com.cybersearch2.classyfy.ClassyFyApplication.java
au.com.cybersearch2.classyfy.ClassyFyEnvironmentModule.java
au.com.cybersearch2.classyfy.ClassyFyResourceEnvironment.java
au.com.cybersearch2.classyfy.ClassyFyStartup.java
au.com.cybersearch2.classyfy.ClassyFyThreadHelper.java
au.com.cybersearch2.classyfy.MainActivityTest.java
au.com.cybersearch2.classyfy.MainActivity.java
au.com.cybersearch2.classyfy.NodeDetailsDialog.java
au.com.cybersearch2.classyfy.NodeDetailsFragment.java
au.com.cybersearch2.classyfy.ProgressFragment.java
au.com.cybersearch2.classyfy.TitleSearchResultsActivityTest.java
au.com.cybersearch2.classyfy.TitleSearchResultsActivity.java
au.com.cybersearch2.classyfy.TitleSearchResultsFragment.java
au.com.cybersearch2.classyfy.data.DataLoader.java
au.com.cybersearch2.classyfy.data.DataStreamParser.java
au.com.cybersearch2.classyfy.data.FieldDescriptor.java
au.com.cybersearch2.classyfy.data.FilePlanNodeType.java
au.com.cybersearch2.classyfy.data.ManagedRecord.java
au.com.cybersearch2.classyfy.data.Model.java
au.com.cybersearch2.classyfy.data.RecordCategory.java
au.com.cybersearch2.classyfy.data.RecordField.java
au.com.cybersearch2.classyfy.data.RecordFolder.java
au.com.cybersearch2.classyfy.data.SqlFromNodeGenerator.java
au.com.cybersearch2.classyfy.data.alfresco.AlfrescoFilePlanLoaderModule.java
au.com.cybersearch2.classyfy.data.alfresco.AlfrescoFilePlanLoader.java
au.com.cybersearch2.classyfy.data.alfresco.AlfrescoFilePlanXmlParser.java
au.com.cybersearch2.classyfy.helper.FileUtils.java
au.com.cybersearch2.classyfy.provider.ClassyFyProviderTest.java
au.com.cybersearch2.classyfy.provider.ClassyFyProvider.java
au.com.cybersearch2.classyfy.provider.ClassyFySearchEngine.java
au.com.cybersearch2.classyjpa.entity.LoaderPersistenceContainerTest.java
au.com.cybersearch2.classyjpa.entity.TestPersistenceWork.java
au.com.cybersearch2.classyjpa.entity.UserPersistenceContainerTest.java
au.com.cybersearch2.classyutil.Transcript.java
au.com.cybersearch2.example.AndroidHelloTwoDbsModule.java
au.com.cybersearch2.example.AndroidHelloTwoDbs.java
au.com.cybersearch2.example.AppThreadHelper.java
au.com.cybersearch2.example.AppThreadHelper.java
au.com.cybersearch2.example.HelloTwoDbsEnvironmentModule.java
au.com.cybersearch2.example.HelloTwoDbsEnvironmentModule.java
au.com.cybersearch2.example.v2.AndroidHelloTwoDbsModule.java
au.com.cybersearch2.example.v2.AndroidHelloTwoDbs.java
au.com.cybersearch2.example.v2.HelloTwoDbsEnvironmentModule.java
com.example.hellotwodbs.HelloTwoDbsApplication.java
com.example.hellotwodbs.HelloTwoDbsApplication.java
com.example.hellotwodbs.HelloTwoDbs.java
com.example.hellotwodbs.HelloTwoDbs.java