Showing posts with label example. Show all posts
Showing posts with label example. Show all posts

Appfuse - Manager & ManagerImpl

public interface YourManager extends GenericManager<YourEntity, Long> {

}
public class YourManagerImpl extends GenericManagerImpl<YourEntity, Long> implements YourManager{

    YourDao yourDao;
    
    public YourManagerImpl(YourDao yourDao) {
        super(yourDao);
        
        this.yourDao = yourDao;
    }
}
        
        <constructor-arg ref="yourDao"/>
</bean>

Appfuse - Entity class

@Entity
@Table(name="your_entity")
public class YourEntity extends BaseObject implements Serializable {

    private Long yourEntityId;
    private String name;
    private String description;
    private Date startDate;

    public YourEntity() {
        
    }
    
    
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "education_id")
    public Long getYourEntityId() {
        return yourEntityId;
    }

    public void setEducationId(Long educationId) {
        this.educationId = educationId;
    }

    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;
    }

    @Temporal(javax.persistence.TemporalType.DATE)
    @Column(name = "start_date")
    public Date getStartDate() {
        return startDate;
    }

    public void setStartDate(Date startDate) {
        this.startDate = startDate;
    }

    @Override
    public int hashCode() {
        int hash = 7;
        hash = 19 * hash + (this.name != null ? this.name.hashCode() : 0);
        hash = 19 * hash + (this.description != null ? this.description.hashCode() : 0);
        hash = 19 * hash + (this.startDate != null ? this.startDate.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final Education other = (Education) obj;
        if ((this.name == null) ? (other.name != null) : !this.name.equals(other.name)) {
            return false;
        }
        if ((this.description == null) ? (other.description != null) : !this.description.equals(other.description)) {
            return false;
        }
        if (this.startDate != other.startDate && (this.startDate == null || !this.startDate.equals(other.startDate))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "Education{" + "educationId=" + educationId + ", name=" + name + ", description=" + description + ", startDate=" + startDate + '}';
    }
    
}

Appfuse - Dao & DaoHibernate classes

public interface YourDao extends GenericDao<YourEntity, Long>  {

}
public class YourDaoHibernate extends GenericDaoHibernate<YourEntity, Long> implements YourDao {
    
    public YourDaoHibernate() {
        super(YourEntity.class);
    }
       
}

    <constructor-arg value="com.company.model.YourEntity"/>

Wicket - ListView Ajax update

<html>
<body>
<div wicket:id="wmc">
    
Name Email
</div> Update list </body> </html>
public class MyPage extends WebPage {

    UserDao dao = new UserDao();

    private WebMarkupContainer wmc;

    private IModel<List<User>> userListModel = new LoadableDetachableModel<List<User>>() {
        @Override
        public List<User> load() {
            List<User> userList = dao.getUsers();
            return userList;
        }
    };

    public MyPage(String id) {
        super(id);

        wmc = new WebMarkupContainer("wmc");
        wmc.setOutputMarkupId(true);
        add(wmc);

        ListView listView = new ListView("listView", userListModel) {
            @Override
            protected void populateItem(ListItem item) {
                User user = (User) item.getModelObject();

                item.add(new Label("name", user.getName()));
                item.add(new Label("email", user.getEmail());
            }
        };
        listView.setOutputMarkupId(true);
        wmc.add(listView);

        add(new AjaxFallbackLink("updateWmcLink") {
               @Override
               public void onClick(AjaxRequestTarget target) {
           
                   if (target != null) {
                           
                       target.add(wmc);
                   }
               }
         });
    }

}
public class User {

    private String name;
    private String email;        
   
    public User {
    }

    public Long getName() {
        return name;
    }

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

    public Long getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }
}

Wicket - ListView

<html>
<body>
    
Name Email
</body> </html>
public class MyPage extends WebPage {

    UserDao dao = new UserDao();

    private IModel<List<User>> userListModel = new LoadableDetachableModel<List<User>>() {
        @Override
        public List<User> load() {
            List<User> userList = dao.getUsers();
            return userList;
        }
    };

    public MyPage(String id) {
        super(id);

        ListView listView = new ListView("listView", userListModel) {
            @Override
            protected void populateItem(ListItem item) {
                User user = (User) item.getModelObject();

                item.add(new Label("name", user.getName()));
                item.add(new Label("email", user.getEmail());
            }
        };
        listView.setOutputMarkupId(true);
        add(listView);
    }

}
public class User {

    private String name;
    private String email;        
   
    public User {
    }

    public Long getName() {
        return name;
    }

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

    public Long getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }
}

Javascript - If

var yourNumber = 8;

if(yourNumber == 8)
{
document.write("BINGO!");
}

Javascript - Variables

var s1 = "string 1";
var s2 = " - string 2 ";
var s = s1 + s2;

var number1 = 5;
var number2 = 5;

document.write(s);
document.write(number1 + number2);

Renders:

string 1 - string 2 10

Javascript - Writing to document

Hello World!

document.getElementById("myHeading").innerHTML="Gimme Five!"; document.write("

Javascript did this

");
Renders:

Gimme Five!

Javascript did this

HTML5 - Canvas - Line

<!doctype html>
<html lang="en">
<head>
  

</head>

<body>
  




</body>
</html>


Result:


MySQL - JOIN

"users" table:

user_id name city age
1 Pauline jamesonNew York15
2 Jimmy McNultyLos Angeles44
2 Andrew WilkinsChicago15
2 Bob SeizmoreNew York16

"user_gender" table:

genderuser_id
female1
male2
male3
male4

SELECT users.name,user_gender.gender FROM users,user_gender
WHERE users.user_id = user_gender.user_id;
Result:

namegender
Pauline jamesonfemale
Jimmy McNultymale
Andrew Wilkinsmale
Bob Seizmoremale

MySQL - OR

"users" table:

user_id name city age
1 John DoeNew York15
2 Jimmy McNultyLos Angeles44
2 Andrew WilkinsChicago15
2 Bob SeizmoreNew York16
SELECT * FROM users
WHERE age<18
AND city='New York'
OR city='Chicago';
Result:
user_id name city age
1 John DoeNew York15
2 Bob SeizmoreNew York16
2 Andrew WilkinsChicago15

Java - Type Casting

double d = 99.9;
 
// Type cast double to int
int i = (int) d;

HTML5 - Basic HTML5 template

<!doctype html>
<html lang="en">
<head>
  

  My Title
  

  

</head>

<body>
  
</body>
</html>

Java - How to compare Strings

String string1 = "5up";
String string2 = "5up";

// false
string1 == string2;

// true
string1.equals(string2);

Java - Interface

public interface Animal {
    
    public void makeASound();

}

public class Dog implements Animal {

    public void makeASound() {
        System.out.print("bark");
    }

    public static void main(String args[]){
        Dog dog = new Dog();

        dog.makeASound();
   }

}




Result:

bark

Java - Inheritance

public class Animal {

    public String animalSound;
    
    public Animal(String aSound) {
        animalSound = aSound;
    }
   
    public void makeASound() {
        System.out.println(animalSound);
    } 
}


public class Dog extends Animal {

    public Dog(String aSound) {
        super(aSound);
    }
     
    public static void main(String args[]) {
        Dog dog = new Dog("bark");

        dog.makeASound();
    }
}


Result:

bark

Java - Convert double to int

double d = 1;
int i = (int) d;


Java - Convert long to int

long l = 1;
int i = (int) l;



To do it safely:

public static int convertLongToInt(long l) {
    if (l < Integer.MIN_VALUE || l > Integer.MAX_VALUE) {
        throw new IllegalArgumentException
            ("Invalid value for: " + l);
    }
    return (int) l;
}

Java - For Loop

for(int i = 1; i < 6; i++) {
    System.out.println(i);
}

Will produce:

1
2
3
4
5


Enhanced for loop:

List<Integer> list = new ArrayList();

    list.add(1);
    list.add(2);
    list.add(3);
        
    for (Integer i : list) {
       System.out.println(i);
    }


Will produce:

1
2
3

Java - List, ArrayList

List list = new ArrayList();

// add object to the list
String s = "Hello World!";
list.add(s);