Thursday, 15 October 2015

Laporan Praktikum II | Membuat inventory barang menggunakan maven ecplise

Pada praktikum kali ini saya akan membuat aplikasi sederhana yaitu inventory barang menggunakan maven eclipse.


Sebelum kita menginstall eclipse, pastikan Sistem Operasi Linux kita telah terinstall jdk. Jika telah terinstall, Eclipse versi terbaru dapat di download di 
http://eclipse.org/ Berikut langkah-langkah instalasi Eclipse IDE. Download Eclipse, pilih versi sesuai dengan sistem operasi yang digunakan. Gunakan Eclipse IDE for Java EE Developer.

 
Setelah proses download selesai extract paket yang telah terdownload (instalasi Eclipse IDE tidak menggunkan wizard sama sekali). Kemudian jalankan Eclipse (secara umum eclipse.exe pada windows atau eclipse pada linux).


  Pilih workspace yang akan digunakan sebagai tempat penyimpanan proyek
 
Kemudian klik OK, maka eclipse IDE siap digunakan.
langkah selanjutnya menginstall lampp. perhatikan berikut ini
Download aplikasi lampp di http://dl-lokal.info
Setelah proses download selesai extrak paket lampp ke direktori /opt/. Caranya menggunakan perintah sudo tar xvfz Downloads/xampp-linux-1.7.3a.tar.gz -C /opt/ di terminal.
  Dan untuk menjalankan lampp tersebut kita menggunakan perintah sudo /opt/lampp/lampp start di terminal.
Sebagai permulaan, kita akan membuat cimande project untuk menampilkan form Admin Cimande, dengan langkah – langkah sebagai berikut :
Buat sebuah database dengan nama gampang.
  • Buat Maven project dengan cara File → New → Other
  • Pilih Maven → Maven Project, kemudian klik Next 
  • Kemudian akam tampil New Maven Project, Klik Next 
    Klik Add Archetype untuk penambahan plugin cimande. Ketik sesuai gambar di bawah ini :
     
    • kemudian klik OK. Maka secara otomatis Eclipse akan mendownload plugin dari cimande.
    • Jika prosesnya telah selesai, Pada field Catalog pilih All Catalogs kemudian pada field Filter ketik org.blueoxygen sehingga otomatis GroupId, ArtifactId dan Version akan tampil. 
    • Pilih versio 2.0, kemudian klik Next
      Masukkan Group Id dan Artifact Id. Sebagai contoh:
      GroupId :
      org.blueoxygenArtifactId : myproject
      • Kemudian klik Finish
        Maka akan terbentuk struktur direktory dari cimande tersebut.
         Notice : Jika struktur direktory dari project cimande seperti gambar diatas maka pembuat project kita berhasil. Tetapi jika tidak maka tunggu beberapa saat karena eclipse tersebut sedang mendownload file – file yang dibutuhkan. Proses kita bisa lihat di Progress.

        • Buka file hibernate.properties kemudian edit sesuai dengan username, password, database yang telah kita buat tadi.

        •  Masukkan perintah untuk menggenerate table schema dengan cara klik kanan project → Run As → Maven Build. Isi Goals dengan perintah hibernate3:hbm2ddl
        • Untuk mengimport data table kedalam database gunakan  perintah
          initialize dbunit:operation
        • jalankan perintah tomcat:run, maka secara otomatis Maven akan mendeploy aplikasi ke dalam server serta menjalankan server. 
        • Setelah server berhasil dijalankan, proyek dapat dibuka melalui url http://localhost:8080/(namaproject). Masukkan username admin dan password blueoxygen untuk dapat masuk ke halaman wokspace administrator.

      langkah selanjutnya Buatlah 4 Package di folder src/main/java dengan nama sebagai berikut
      10

      Buat class CategoryController.java dan ItemController.java di package org.mycompany.controller.

      package org.mycompany.controller;

      import org.blueoxygen.cimande.commons.CimandeModuleAction;
      import org.mycompany.entity.Category;
      import org.mycompany.service.CategoryService;
      import org.springframework.beans.factory.annotation.Autowired;
      import com.opensymphony.xwork2.validator.annotations.RequiredStringValidator;
      import com.opensymphony.xwork2.validator.annotations.Validations;

      public class CategoryController extends CimandeModuleAction {

          @Autowired
          private CategoryService itemCategoryService;

          private Category itemCategory = new Category();
          private Integer limit = 0;
          private Integer page = 0;

          public CategoryController() {
              model.put("itemCategory", itemCategory);
          }

          // POST /module/category/create
          // POST /module/category/edit/{id}
          @Validations(requiredStrings = { @RequiredStringValidator(fieldName = "itemCategory.name", message = "Name cannot be empty", trim = true) })
          public String categoryPost() {
              itemCategoryService.saveItemCategory(itemCategory);
              return SUCCESS;
          }

          // GET /module/category/create
          // GET /module/category/edit/{id}
          public String categoryGet() {
              model.put(
                      "itemCategory",
                      itemCategoryService.getItemCategoryById(itemCategory.getId()
                              + ""));
              return INPUT;
          }

          // DELETE /module/category/edit/{id}
          public String categoryDelete() {
              itemCategoryService.deleteItemCategory(itemCategoryService
                      .getItemCategoryById(itemCategory.getId()));

              return SUCCESS;
          }

          // POST/GET /module/category/filter
          public String filter() {
              try {
                  limit = model.get("rows") == null ? 0 : new Integer(model.get(
                          "rows").toString());
                  page = model.get("page") == null ? 0 : new Integer(model
                          .get("page").toString());

              } catch (NumberFormatException e) {
                  e.printStackTrace();
              }

              if (limit == 0 && page == 0) {
                  limit = 10;
                  page = 1;

              }
              model.put("rows", limit);
              model.put("page", page);

              int count = (int) itemCategoryService.getItemCategoryCount("", "");
              int total = count / limit;
              if (total % limit > 0)
                  total++;

              model.put("itemCategorys",
                      itemCategoryService.getItemCategoryList("", "", limit, page));
              if (total == 0)
                  total++;
              model.put("total", total);
              model.put("records", total);
             
              return SUCCESS;
          }

      }


      /**
       * ItemController.java
       *
       * Created on May 10, 2011 2:19:09 PM
       */
      package org.mycompany.controller;

      import org.blueoxygen.cimande.commons.CimandeModuleAction;
      import org.mycompany.entity.Item;
      import org.mycompany.service.CategoryService;
      import org.mycompany.service.ItemService;
      import org.springframework.beans.factory.annotation.Autowired;
      import com.opensymphony.xwork2.validator.annotations.IntRangeFieldValidator;
      import com.opensymphony.xwork2.validator.annotations.RequiredStringValidator;
      import com.opensymphony.xwork2.validator.annotations.Validations;

      /**
       * @author Dian Aditya
       *
       */

      public class ItemController extends CimandeModuleAction {
          @Autowired
          private ItemService itemService;
          @Autowired
          private CategoryService itemCategoryService;
          private Item item = new Item();
          private int limit = 0;
          private int page = 0;

          public ItemController() {
              model.put("item", item);
          }

          // POST /module/item/create
          // POST /module/item/create/{id}
          @Validations(requiredStrings = { @RequiredStringValidator(fieldName = "item.name", message = "Name cannot be empty", trim = true) }, intRangeFields = { @IntRangeFieldValidator(fieldName = "item.price", min = "0", message = "Must be greater than zero") })
          public String itemPost() {
              itemService.saveItem(item);

              return SUCCESS;
          }

          // GET /module/item/create
          // GET /module/item/edit/{id}
          public String itemGet() {
              model.put("item", itemService.getItemById(item.getId() + ""));

              model.put("itemCategorys", itemCategoryService.getAllItemCategory());
              return INPUT;
          }

          // DELETE /module/item/edit/{id}
          public String itemDelete() {
              itemService.deleteItem(itemService.getItemById(item.getId()));

              return SUCCESS;
          }

          // POST/GET /module/item/filter
          public String filter() {

              try {
                  limit = model.get("rows") == null ? 0 : new Integer(model.get(
                          "rows").toString());
                  page = model.get("page") == null ? 0 : new Integer(model
                          .get("page").toString());
              } catch (NumberFormatException e) {
                  e.printStackTrace();
              }

              if (limit == 0 && page == 0) {
                  limit = 10;
                  page = 1;

              }
             
              model.put("rows", limit);
              model.put("page", page);

              int count = (int) itemService.getItemCount("", "");
              int total = count / limit;
              if (total % limit > 0)
                  total++;

              model.put("items", itemService.getItemList("", "", limit, page));
              if(total==0)
                  total++;
              model.put("total", total);
              model.put("records", total);
              return SUCCESS;
          }
      }
       
      Buat class CategoryDao.java dan item ItemDao di package  org.blueoxygen.mycompany.dao package org.mycompany.dao;

      import java.util.List;

      import org.blueoxygen.cimande.commons.LogInformation;
      import org.mycompany.entity.Category;
      import org.blueoxygen.cimande.persistence.hibernate.dao.HibernatePersistenceDaoManager;
      import org.blueoxygen.cimande.security.User;
      import org.hibernate.Criteria;
      import org.hibernate.criterion.MatchMode;
      import org.hibernate.criterion.Order;
      import org.hibernate.criterion.Projections;
      import org.hibernate.criterion.Restrictions;
      import org.springframework.stereotype.Repository;

      @Repository
      public class CategoryDao extends
              HibernatePersistenceDaoManager<Category> {
          public void saveItemCategory(Category itemCategory) {
              if (itemCategory == null)
                  return;

              if (itemCategory.getId() == null) {
                  createItemCategory(itemCategory);
              } else if (itemCategory.getId().trim().equalsIgnoreCase("")) {
                  createItemCategory(itemCategory);
              } else {
                  itemCategory.getLogInformation().setLastUpdateBy(
                          getCurrentUser().getId());
                  itemCategory.getLogInformation().setLastUpdateDate(getCurretTime());

                  merge(itemCategory);
              }
          }

          public long getItemCategoryCountByCriteria(String name, String description) {
              Criteria criteria = getItemCategoryCriteria(name, description);
              criteria.setProjection(Projections.rowCount());

              return new Long(criteria.uniqueResult() + "");
          }

          @SuppressWarnings("unchecked")
          public List<Category> getItemCategoryByCriteria(String name,
                  String description, int limit, int page) {

              return getItemCategoryCriteria(name, description).setMaxResults(limit)
                      .setFirstResult((page - 1) * limit).list();
          }

          private Criteria getItemCategoryCriteria(String name, String description) {
              Criteria criteria = sessionFactory.getCurrentSession().createCriteria(
                      Category.class);

              criteria.add(Restrictions.like("name", name, MatchMode.ANYWHERE));
              criteria.add(Restrictions.like("description", description,
                      MatchMode.ANYWHERE));
              criteria.addOrder(Order.asc("code"));

              return criteria;
          }

          private void createItemCategory(Category itemCategory) {
              User user = getCurrentUser();
              LogInformation logInformation = new LogInformation();
              logInformation.setCreateBy(user.getId());
              logInformation.setCreateDate(getCurretTime());
              logInformation.setLastUpdateBy(user.getId());
              logInformation.setLastUpdateDate(getCurretTime());
              itemCategory.setId(null);
              itemCategory.setLogInformation(logInformation);

              persist(itemCategory);
          }
         
      }
       

      /**
       * ItemDao.java
       *
       * Created on May 10, 2011 2:19:49 PM
       */
      package org.mycompany.dao;

      import java.util.List;

      import org.blueoxygen.cimande.commons.LogInformation;
      import org.mycompany.entity.Item;
      import org.blueoxygen.cimande.persistence.hibernate.dao.HibernatePersistenceDaoManager;
      import org.blueoxygen.cimande.security.User;
      import org.hibernate.Criteria;
      import org.hibernate.criterion.MatchMode;
      import org.hibernate.criterion.Projections;
      import org.hibernate.criterion.Restrictions;
      import org.springframework.stereotype.Repository;

      /**
       * @author Dian Aditya
       *
       */
      @Repository
      public class ItemDao extends HibernatePersistenceDaoManager<Item> {

          public void saveItem(Item item) {
              if (item == null)
                  return;

              if (item.getId() == null) {
                  createItem(item);
              } else if (item.getId().trim().equalsIgnoreCase("")) {
                  createItem(item);
              } else {
                  item.getLogInformation().setLastUpdateBy(getCurrentUser().getId());
                  item.getLogInformation().setLastUpdateDate(getCurretTime());

                  merge(item);
              }
          }

          public long getItemCountByCriteria(String name, String description) {
              Criteria criteria = getItemCriteria(name, description);
              criteria.setProjection(Projections.rowCount());

              return new Long(criteria.uniqueResult() + "");
          }

          @SuppressWarnings("unchecked")
          public List<Item> getItemByCriteria(String name, String description,
                  int limit, int page) {

              return getItemCriteria(name, description).setMaxResults(limit)
                      .setFirstResult((page - 1) * limit).list();
          }

          private Criteria getItemCriteria(String name, String description) {
              Criteria criteria = sessionFactory.getCurrentSession().createCriteria(
                      Item.class);

              criteria.add(Restrictions.like("name", name, MatchMode.ANYWHERE));
              criteria.add(Restrictions.like("description", description,
                      MatchMode.ANYWHERE));

              return criteria;
          }

          private void createItem(Item item) {
              User user = getCurrentUser();
              LogInformation logInformation = new LogInformation();
              logInformation.setCreateBy(user.getId());
              logInformation.setCreateDate(getCurretTime());
              logInformation.setLastUpdateBy(user.getId());
              logInformation.setLastUpdateDate(getCurretTime());

              item.setId(null);
              item.setLogInformation(logInformation);

              persist(item);
          }
      }
       

      Buat class Category.java dan Item di package  org.blueoxygen.mycompany.entity

      package org.mycompany.entity;

      import javax.persistence.Entity;
      import javax.persistence.Inheritance;
      import javax.persistence.InheritanceType;
      import javax.persistence.Table;

      import org.blueoxygen.cimande.commons.DefaultPersistence;

      /**
       * @author Praditya Eldorado
       *
       */

      @Entity
      @Table(name = "module_category")
      @Inheritance(strategy = InheritanceType.SINGLE_TABLE)
      public class Category extends DefaultPersistence {
          private String code;
          private String name;
          private String description;

          public String getCode() {
              return code;
          }

          public void setCode(String code) {
              this.code = code;
          }

          public String getName() {
              return name;
          }

          public void setName(String name) {
              this.name = name;
          }

          public String getDescription() {
              return description;
          }

          public void setDescription(String description) {
              this.description = description;
          }

      }
       

      /**
       * Item.java
       *
       * Created on May 10, 2011 1:54:59 PM
       */
      package org.mycompany.entity;

      import javax.persistence.Column;
      import javax.persistence.Entity;
      import javax.persistence.Inheritance;
      import javax.persistence.InheritanceType;
      import javax.persistence.JoinColumn;
      import javax.persistence.ManyToOne;
      import javax.persistence.Table;

      import org.blueoxygen.cimande.commons.DefaultPersistence;

      /**
       * @author Dian Aditya
       *
       */
      @Entity
      @Table(name = "module_item")
      @Inheritance(strategy = InheritanceType.SINGLE_TABLE)
      public class Item extends DefaultPersistence {
          private String name;
          private int price;
          private String description;
          private Category category;

          /**
           * @return the name
           */
          @Column
          public String getName() {
              return name;
          }

          /**
           * @param name
           *            the name to set
           */
          public void setName(String name) {
              this.name = name;
          }

          /**
           * @return the price
           */
          @Column
          public int getPrice() {
              return price;
          }

          /**
           * @param price
           *            the price to set
           */
          public void setPrice(int price) {
              this.price = price;
          }

          /**
           * @return the description
           */
         
          public String getDescription() {
              return description;
          }

          /**
           * @param description
           *            the description to set
           */
          public void setDescription(String description) {
              this.description = description;
          }

          @ManyToOne
          @JoinColumn(name = "category_id")
          public Category getCategory() {
              return category;
          }

          public void setCategory(Category category) {
              this.category = category;
          }

      }
       

      Buat class CategoryService.java dan ItemService di package  org.blueoxygen.mycompany.service

      package org.mycompany.service;

      import java.util.List;

      import org.mycompany.entity.Category;

      public interface CategoryService {
          void saveItemCategory(Category itemCategory);

          void deleteItemCategory(Category itemCategory);

          Category getItemCategoryById(String id);

          long getItemCategoryCount(String name, String description);

          List<Category> getItemCategoryList(String name, String description,
                  int limit, int page);

          List<Category> getAllItemCategory();
      }
       


      /**
       * ItemService.java
       *
       * Created on May 10, 2011 2:22:25 PM
       */
      package org.mycompany.service;

      import java.util.List;

      import org.mycompany.entity.Item;

      /**
       * @author Dian Aditya
       *
       */
      public interface ItemService {
          void saveItem(Item item);

          void deleteItem(Item item);

          Item getItemById(String id);

          long getItemCount(String name, String description);

          List<Item> getItemList(String name, String description,
                  int limit, int page);
      }
       

      Buat class CategoryServiceImpl.java dan ItemServiceImpl di package  org.mycompany.service.impl

      package org.mycompany.service.impl;

      import java.util.List;

      import org.mycompany.dao.CategoryDao;
      import org.mycompany.entity.Category;
      import org.mycompany.entity.Item;
      import org.mycompany.service.CategoryService;
      import org.springframework.beans.factory.annotation.Autowired;
      import org.springframework.stereotype.Service;
      import org.springframework.transaction.annotation.Transactional;

      @Service
      @Transactional(readOnly = true)
      public class CategoryServiceImpl implements CategoryService {
          @Autowired
          private CategoryDao itemCategoryDao;

          @Transactional
          public void saveItemCategory(Category itemCategory) {
              itemCategoryDao.saveItemCategory(itemCategory);
          }

          @Transactional
          public void deleteItemCategory(Category itemCategory) {
              itemCategoryDao.remove(itemCategory);
          }

          public Category getItemCategoryById(String id) {
              return itemCategoryDao.getById(Category.class, id);
          }

          public long getItemCategoryCount(String name, String description) {
              return itemCategoryDao
                      .getItemCategoryCountByCriteria(name, description);
          }

          public List<Category> getItemCategoryList(String name,
                  String description, int limit, int page) {
              return itemCategoryDao.getItemCategoryByCriteria(name, description,
                      limit, page);
          }

          public List<Category> getAllItemCategory() {

              return itemCategoryDao.findAll(Category.class);
          }

      }
       


      /**
       * ItemServiceImpl.java
       *
       * Created on May 10, 2011 2:22:16 PM
       */
      package org.mycompany.service.impl;

      import java.util.List;

      import org.mycompany.dao.ItemDao;
      import org.mycompany.entity.Item;
      import org.mycompany.service.ItemService;
      import org.springframework.beans.factory.annotation.Autowired;
      import org.springframework.stereotype.Service;
      import org.springframework.transaction.annotation.Transactional;

      /**
       * @author Dian Aditya
       *
       */
      @Service
      @Transactional(readOnly = true)
      public class ItemServiceImpl implements ItemService {

          @Autowired
          private ItemDao itemDao;

          @Transactional
          public void saveItem(Item item) {
              itemDao.saveItem(item);
          }

          @Transactional
          public void deleteItem(Item item) {
              itemDao.remove(item);
          }

          public Item getItemById(String id) {
              return itemDao.getById(Item.class, id);
          }

          public long getItemCount(String name, String description) {
              return itemDao.getItemCountByCriteria(name, description);
          }

          public List<Item> getItemList(String name, String description, int limit,
                  int page) {
              return itemDao.getItemByCriteria(name, description, limit, page);
          }


      Kemudian restart tomcat:run. Buka web browser dan ketikan url http://localhost:8080/myproject/ . Kemudian login dengan username : inventoryadmin, password : inventoryadmin 
      maka hasilnya seperti dibawah ini
      11
       
SEMOGA BERMANFAAT




luvne.com resepkuekeringku.com desainrumahnya.com yayasanbabysitterku.com