sample.WelcomeController.java Source code

Java tutorial

Introduction

Here is the source code for sample.WelcomeController.java

Source

/*
 * #%L
 * Wisdom-Framework
 * %%
 * Copyright (C) 2013 - 2014 Wisdom Framework
 * %%
 * 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.
 * #L%
 */
package sample;

import com.fasterxml.jackson.databind.JsonNode;
import com.google.common.collect.Iterables;
import com.google.common.collect.Lists;
import com.orientechnologies.orient.core.sql.query.OSQLSynchQuery;
import javassist.util.proxy.Proxy;
import org.apache.felix.ipojo.annotations.Requires;
import org.apache.felix.ipojo.annotations.Validate;

import org.wisdom.api.DefaultController;
import org.wisdom.api.annotations.*;
import org.wisdom.api.content.Json;
import org.wisdom.api.http.HttpMethod;
import org.wisdom.api.http.Result;
import org.wisdom.api.http.Results;
import org.wisdom.orientdb.object.OrientDbCrud;
import sample.models.Adr;
import sample.models.Group;
import sample.models.Thing;
import sample.models.User;

import javax.validation.constraints.NotNull;
import java.util.*;

/**
 * Your first Wisdom Controller.
 */
@Controller
public class WelcomeController extends DefaultController {

    static {
        Class workaround = Proxy.class;
    }

    @Model(value = Thing.class)
    private OrientDbCrud<Thing, String> thingCrud;

    @Model(value = Group.class)
    private OrientDbCrud<Group, String> groupCrud;

    @Model(value = User.class)
    private OrientDbCrud<User, String> userCrud;

    @Requires
    Json json;

    @Validate
    public void start() {
        if (!userCrud.findAll().iterator().hasNext()) {
            User usr = new User();

            usr.setName("user A");

            User usr2 = new User();
            usr2.setName("user B");

            Thing t1 = new Thing();
            t1.setThingName("this is the A thing");
            Thing t2 = new Thing();
            t2.setThingName("this is the B thing");
            Thing t3 = new Thing();
            t3.setThingName("this is the C thing");

            Group g1 = new Group();
            g1.setGroupName("group A");
            Group g2 = new Group();
            g2.setGroupName("group B");

            usr.setBelongsTo(g1);
            usr.getM_things().addAll(Lists.newArrayList(t1, t2, t3));
            usr.getM_adr().add(new Adr("A", "B"));
            usr.getM_adr().add(new Adr("C", "D"));

            usr2.setBelongsTo(g2);
            usr2.getM_things().addAll(Lists.newArrayList(t2, t3));
            usr2.getM_adr().add(new Adr("C", "D"));
            usr2.getM_adr().add(new Adr("E", "F"));

            g1.getMembers().add(usr);
            g2.getMembers().add(usr2);

            userCrud.save(Lists.newArrayList(usr, usr2));

        }
    }

    @Route(method = HttpMethod.GET, uri = "/orientdb/{name}")
    public Result test3(@NotNull @PathParameter("name") String name) {

        if (name.equalsIgnoreCase("*"))
            return ok(Iterables.toArray(userCrud.findAll(), User.class)).json();
        else
            return ok(userCrud.query(
                    new OSQLSynchQuery<User>("select from User where m_adr contains ( adr2 ='" + name + "')")))
                            .json();

    }

}