--- /dev/null
+package org.tizen.cli.command;\r
+\r
+import org.slf4j.Logger;\r
+import org.slf4j.LoggerFactory;\r
+\r
+abstract public class\r
+AbstractCommand\r
+implements CommandWithParameter\r
+{\r
+ \r
+ protected Logger logger = LoggerFactory.getLogger( getClass() );\r
+\r
+}\r
--- /dev/null
+package org.tizen.cli.command;\r
+\r
+import java.io.IOException;\r
+import java.net.URL;\r
+\r
+import org.tizen.cli.util.TemplateModelProvider;\r
+import org.tizen.common.core.command.ExecutionContext;\r
+import org.tizen.common.verrari.Realm;\r
+import org.tizen.common.verrari.RealmFactory;\r
+import org.tizen.common.verrari.Template;\r
+import org.tizen.common.verrari.TemplateContext;\r
+import org.tizen.common.verrari.TemplateException;\r
+import org.tizen.common.verrari.storage.FileStorage;\r
+\r
+public class CreateTemplate\r
+extends AbstractCommand\r
+implements CommandWithParameter\r
+{\r
+ \r
+ @Override\r
+ public void process(\r
+ final ExecutionContext context,\r
+ final String[] args\r
+ ) throws IOException, TemplateException\r
+ {\r
+ if ( null == args || 0 == args.length )\r
+ {\r
+ context.getPrompter().error( "One more template id required" );\r
+ return ;\r
+ }\r
+ Realm realm = (Realm) context.getValue( PluginConstants.REALM );\r
+ \r
+ if ( null == realm )\r
+ {\r
+ final URL url = new URL( "file:///home/bylee/.install/tizen-cli/template/config.json" );\r
+ realm = RealmFactory.getInstance().create( url );\r
+ }\r
+ \r
+ final FileStorage storage = new FileStorage( context.getFileHandler() );\r
+ \r
+ final TemplateModelProvider models = new TemplateModelProvider( context );\r
+ \r
+ TemplateContext.set( models );\r
+ try\r
+ {\r
+ for ( final String arg : args )\r
+ {\r
+ logger.debug( "Arg :{}", arg );\r
+ final Template template = realm.getTemplate( arg );\r
+ \r
+ logger.debug( "Template :{}", template );\r
+ \r
+ template.template( models, storage );\r
+ \r
+ }\r
+ }\r
+ finally\r
+ {\r
+ TemplateContext.clear();\r
+ }\r
+ \r
+ }\r
+\r
+}\r
--- /dev/null
+package org.tizen.cli.command;\r
+\r
+public class PluginConstants\r
+{\r
+\r
+ public static final String OBSERVER = "pic.file.observer";\r
+ public static final String PLUGINS = "pic.server.plugin";\r
+ \r
+ public static final String REALM = "pic.template.realm";\r
+ \r
+ \r
+ public static final String PROP_DOCBASE = "document.base";\r
+ \r
+ public static final String PROP_CWD = "user.dir";\r
+ \r
+\r
+}\r
--- /dev/null
+package org.tizen.cli.command;\r
+\r
+import static org.tizen.common.util.CollectionUtil.isEmpty;\r
+import static org.tizen.common.util.StringUtil.isEmpty;\r
+\r
+import java.util.Collection;\r
+\r
+import org.tizen.common.core.command.ExecutionContext;\r
+import org.tizen.common.verrari.AttributeContainer;\r
+import org.tizen.common.verrari.Condition;\r
+import org.tizen.common.verrari.Condition.Operation;\r
+import org.tizen.common.verrari.Realm;\r
+import org.tizen.common.verrari.SearchableRealm;\r
+import org.tizen.common.verrari.template.TemplateConstants;\r
+\r
+public class SearchTemplate\r
+extends AbstractCommand\r
+implements CommandWithParameter\r
+{\r
+\r
+ @Override\r
+ public void process(\r
+ final ExecutionContext context,\r
+ final String[] args\r
+ )\r
+ throws Exception\r
+ {\r
+ if ( null == args || 0 == args.length )\r
+ {\r
+ context.getPrompter().error( "A keyword required" );\r
+ return ;\r
+ }\r
+ final Realm realm = (Realm) context.getValue( PluginConstants.REALM );\r
+ \r
+ if ( !( realm instanceof SearchableRealm ) )\r
+ {\r
+ context.getPrompter().error( "Search unsupported realm" );\r
+ return ;\r
+ }\r
+ \r
+ final SearchableRealm searchable = (SearchableRealm) realm;\r
+ final String keyword = args[0];\r
+ final Collection<AttributeContainer> templates = searchable.search(\r
+ new Condition( Operation.Contains, "keyword", keyword )\r
+ );\r
+\r
+ if ( isEmpty( templates ) )\r
+ {\r
+ context.getPrompter().notify( "No template with '" + keyword + "'" );\r
+ return ;\r
+ }\r
+ for ( final AttributeContainer template : templates )\r
+ {\r
+ final StringBuilder buffer = new StringBuilder();\r
+ final String name = template.getAttribute( TemplateConstants.ATTR_NAME );\r
+ final String desc = template.getAttribute( TemplateConstants.ATTR_DESCRIPTION );\r
+ buffer.append( name );\r
+ buffer.append( " - " );\r
+ if ( isEmpty( desc ) )\r
+ {\r
+ buffer.append( "undescribed" );\r
+ }\r
+ else\r
+ {\r
+ buffer.append( desc );\r
+ }\r
+ \r
+ context.getPrompter().notify( buffer.toString() );\r
+ }\r
+ }\r
+\r
+}\r