/*
 * Copyright (c) 2008-2018 The Aspectran Project
 *
 * 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.
 */
package com.aspectran.demo.examples.customer;

import com.aspectran.core.activity.Translet;
import com.aspectran.core.component.bean.annotation.Autowired;
import com.aspectran.core.component.bean.annotation.Bean;
import com.aspectran.core.component.bean.annotation.Component;
import com.aspectran.web.support.http.HttpStatusSetter;

import java.util.List;

@Component
@Bean
public class CustomerAction {
    
    @Autowired
    private CustomerDao dao;
    
    public List<Customer> getCustomerList(Translet translet) {
        return dao.getCustomerList();
    }

    public Customer getCustomer(Translet translet) {
        int id = Integer.parseInt(translet.getParameter("id"));

        Customer customer = dao.getCustomer(id);
        
        if(customer == null) {
            HttpStatusSetter.notFound(translet);
            return null;
        }

        return customer;
    }
    
    public Customer insertCustomer(Translet translet) {
        String name = translet.getParameter("name");
        int age = Integer.valueOf(translet.getParameter("age"));
        boolean approved = "Y".equals(translet.getParameter("approved"));
        
        Customer customer = new Customer();
        customer.putValue(Customer.name, name);
        customer.putValue(Customer.age, age);
        customer.putValue(Customer.approved, approved);
        
        int id = dao.insertCustomer(customer);
        
        String resourceUri = translet.getName() + "/" + id;
        HttpStatusSetter.created(translet, resourceUri);

        return customer;
    }
    
    public Customer updateCustomer(Translet translet) {
        int id = Integer.parseInt(translet.getParameter("id"));
        String name = translet.getParameter("name");
        int age = Integer.valueOf(translet.getParameter("age"));
        boolean approved = "Y".equals(translet.getParameter("approved"));

        Customer customer = new Customer();
        customer.putValue(Customer.id, id);
        customer.putValue(Customer.name, name);
        customer.putValue(Customer.age, age);
        customer.putValue(Customer.approved, approved);

        boolean updated = dao.updateCustomer(customer);

        if(!updated) {
            HttpStatusSetter.notFound(translet);
            return null;
        }

        return customer;
    }

    public boolean deleteCustomer(Translet translet) {
        int id = Integer.parseInt(translet.getParameter("id"));

        boolean deleted = dao.deleteCustomer(id);

        if(!deleted) {
            HttpStatusSetter.notFound(translet);
            return false;
        }

        return true;
    }

    public boolean approve(Translet translet) {
        int id = Integer.parseInt(translet.getParameter("id"));
        boolean approved = Boolean.parseBoolean(translet.getParameter("approved"));

        boolean updated = dao.approve(id, approved);

        if(!updated) {
            HttpStatusSetter.notFound(translet);
            return false;
        }

        return true;
    }
    
    public boolean isApproved(Translet translet) {
        int id = Integer.parseInt(translet.getParameter("id"));
        return dao.isApproved(id);
    }

}

@@ -1011,21 +1011,17 @@
 ction %7B%0A
-    
 %0A
+
     @Aut
@@ -1056,21 +1056,17 @@
 ao dao;%0A
-    
 %0A
+
     publ
@@ -1328,33 +1328,25 @@
 stomer(id);%0A
-        
 %0A
+
         if(c
@@ -1472,37 +1472,33 @@
 customer;%0A    %7D%0A
-    
 %0A
+
     public Custo
@@ -1729,25 +1729,17 @@
 ved%22));%0A
-        
 %0A
+
         
@@ -1924,25 +1924,17 @@
 roved);%0A
-        
 %0A
+
         
@@ -1976,17 +1976,9 @@
 r);%0A
-        
 %0A
+
     
@@ -2114,37 +2114,33 @@
 customer;%0A    %7D%0A
-    
 %0A
+
     public Custo
@@ -3199,23 +3199,32 @@
 boolean 
-approve
+updateAttributes
 (Transle
@@ -3541,32 +3541,32 @@
 lse;%0A        %7D%0A%0A
+
         return t
@@ -3579,169 +3579,8 @@
    %7D
-%0A    %0A    public boolean isApproved(Translet translet) %7B%0A        int id = Integer.parseInt(translet.getParameter(%22id%22));%0A        return dao.isApproved(id);%0A    %7D
 %0A%0A%7D%0A

Update demo