Search in sources :

Example 1 with AbstractDataInstallator

use of ru.ydn.wicket.wicketorientdb.AbstractDataInstallator in project wicket-orientdb by OrienteerBAP.

the class WicketApplication method init.

/**
 * @see org.apache.wicket.Application#init()
 */
@Override
public void init() {
    super.init();
    getApplicationListeners().add(new EmbeddOrientDbApplicationListener(WicketApplication.class.getResource("db.config.xml")) {

        @Override
        public void onAfterServerStartupAndActivation(OrientDbWebApplication app) throws Exception {
            OrientDB orientDB = getServer().getContext();
            orientDB.createIfNotExists(getOrientDbSettings().getDbName(), getOrientDbSettings().getDbType());
        }
    });
    getOrientDbSettings().setDbName(DB_NAME);
    getOrientDbSettings().setDbType(ODatabaseType.MEMORY);
    getOrientDbSettings().setGuestUserName("admin");
    getOrientDbSettings().setGuestPassword("admin");
    OrientDBHttpAPIResource.mountOrientDbRestApi(this);
    getApplicationListeners().add(new AbstractDataInstallator() {

        @Override
        protected void installData(OrientDbWebApplication app, ODatabaseSession db) {
            OSchemaHelper helper = OSchemaHelper.bind(db);
            helper.oClass(CLASS_NAME).oProperty(PROP_NAME, OType.STRING).oProperty(PROP_INT, OType.INTEGER).oProperty(PROP_DATE, OType.DATE);
            if (helper.getOClass().count() == 0) {
                Random random = new Random();
                Date today = new Date(System.currentTimeMillis());
                int delta = 365 * 24 * 60 * 60;
                for (int i = 0; i < 50; i++) {
                    ODocument doc = new ODocument(helper.getOClass());
                    doc.field(PROP_NAME, "Name for #" + i);
                    doc.field(PROP_INT, i);
                    doc.field(PROP_DATE, new Date(today.getTime() + (random.nextInt(2 * delta) - delta) * 1000));
                    doc.save();
                }
            }
        }
    });
    LOG.info("Wicket-OrientDB Demo Web App has been started");
}
Also used : OrientDB(com.orientechnologies.orient.core.db.OrientDB) AbstractDataInstallator(ru.ydn.wicket.wicketorientdb.AbstractDataInstallator) OSchemaHelper(ru.ydn.wicket.wicketorientdb.utils.OSchemaHelper) Random(java.util.Random) EmbeddOrientDbApplicationListener(ru.ydn.wicket.wicketorientdb.EmbeddOrientDbApplicationListener) ODatabaseSession(com.orientechnologies.orient.core.db.ODatabaseSession) Date(java.sql.Date) OrientDbWebApplication(ru.ydn.wicket.wicketorientdb.OrientDbWebApplication) ODocument(com.orientechnologies.orient.core.record.impl.ODocument)

Aggregations

ODatabaseSession (com.orientechnologies.orient.core.db.ODatabaseSession)1 OrientDB (com.orientechnologies.orient.core.db.OrientDB)1 ODocument (com.orientechnologies.orient.core.record.impl.ODocument)1 Date (java.sql.Date)1 Random (java.util.Random)1 AbstractDataInstallator (ru.ydn.wicket.wicketorientdb.AbstractDataInstallator)1 EmbeddOrientDbApplicationListener (ru.ydn.wicket.wicketorientdb.EmbeddOrientDbApplicationListener)1 OrientDbWebApplication (ru.ydn.wicket.wicketorientdb.OrientDbWebApplication)1 OSchemaHelper (ru.ydn.wicket.wicketorientdb.utils.OSchemaHelper)1