Skip to content
This repository has been archived by the owner on Sep 12, 2018. It is now read-only.

Commit

Permalink
Property: Introduce Property
Browse files Browse the repository at this point in the history
Property is a key-value pair table to store any value in the Database.
  • Loading branch information
Yi EungJun committed Jan 27, 2015
1 parent 6299412 commit 2468b37
Show file tree
Hide file tree
Showing 3 changed files with 122 additions and 0 deletions.
1 change: 1 addition & 0 deletions app/Global.java
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ public void onStart(Application app) {
insertInitialData();

Config.onStart();
Property.onStart();
PullRequest.onStart();
NotificationMail.onStart();
NotificationEvent.onStart();
Expand Down
105 changes: 105 additions & 0 deletions app/models/Property.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
/**
* Yobi, Project Hosting SW
*
* Copyright 2014 NAVER Corp.
* http://yobi.io
*
* @Author Yi EungJun
*
* 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 models;

import play.data.validation.Constraints;
import play.db.ebean.Model;
import utils.Diagnostic;

import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.Id;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

@Entity
public class Property extends Model {
public static final Finder<Long, Property> find = new Finder<>(Long.class, Property.class);

private static final long serialVersionUID = 8074682539173273921L;

@Id
public Long id;

@Enumerated(EnumType.STRING)
@Constraints.MaxLength(255)
public Name name;

@Constraints.MaxLength(4000)
public String value;

public static String get(Name name) {
List<Property> properties = find.where().eq("name", name).findList();

if (properties.size() > 0) {
return properties.get(0).value;
} else {
return null;
}
}

public static Long getLong(Name name) {
String value = get(name);

return value == null ? null : Long.valueOf(value);
}

public static void set(Name name, String value) {
Property property = find.where().eq("name", name).findUnique();

if (property == null) {
property = new Property();
property.name = name;
}

property.value = value;
property.save();
}

public static void set(Name name, Long value) {
set(name, value.toString());
}

public static enum Name {
// Add property you need here.
}

public static void onStart() {
Diagnostic.register(new Diagnostic() {
@Override
public List<String> check() {
List<String> errors = new ArrayList<>();

for (Property.Name name : Property.Name.values()) {
Set<Property> properties = Property.find.where().eq("name", name).findSet();
if (properties.size() > 1) {
errors.add(String.format("Property '%s' has duplicated values: %s",
name, properties));
}
}

return errors;
}
});
}
}
16 changes: 16 additions & 0 deletions conf/evolutions/default/95.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# --- !Ups

create table property (
id bigint not null,
name varchar(255),
value varchar(4000),
constraint pk_property primary key (id))
;

create sequence property_seq;

# --- !Downs

drop table if exists property;

drop sequence if exists property_seq;

0 comments on commit 2468b37

Please sign in to comment.