Skip to content

Commit

Permalink
#117 merged NewMetadata corrupted branch - just usable classes.
Browse files Browse the repository at this point in the history
  • Loading branch information
rinkesj committed Feb 26, 2015
1 parent 1f06f18 commit 096d846
Show file tree
Hide file tree
Showing 7 changed files with 652 additions and 0 deletions.
108 changes: 108 additions & 0 deletions src/main/java/cz/zcu/kiv/eegdatabase/data/dao/SimpleTemplateDao.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
package cz.zcu.kiv.eegdatabase.data.dao;

import cz.zcu.kiv.eegdatabase.data.pojo.Template;

import java.util.List;

/**
* ********************************************************************************************************************
* <p/>
* This file is part of the eegdatabase project
* <p/>
* ==========================================
* <p/>
* Copyright (C) 2014 by University of West Bohemia (http://www.zcu.cz/en/)
* <p/>
* **********************************************************************************************************************
* <p/>
* 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
* <p/>
* http://www.apache.org/licenses/LICENSE-2.0
* <p/>
* 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.
* <p/>
* **********************************************************************************************************************
* <p/>
* SimpleTemplateDao, 2014/07/02 11:34 Prokop
* <p/>
* ********************************************************************************************************************
*/
public class SimpleTemplateDao extends SimpleGenericDao<Template, Integer> implements TemplateDao {

public SimpleTemplateDao(){
super(Template.class);
}

@Override
public List<Template> getTemplatesByPerson(int personId) {
String hqlQuery = "from Template t where t.personByPersonId.personId = :personId";
List<Template> list = getSessionFactory().getCurrentSession().createQuery(hqlQuery).setParameter("personId", personId).list();

return list;
}

@Override
public List<Template> getDefaultTemplates() {
String hqlQuery = "from Template t where t.isDefault = true";
List<Template> list = getHibernateTemplate().find(hqlQuery);

return list;
}

/**
* Finds all default and user's templates.
* Distinct select and ordered by isDefault param (default templates first).
*
* @param personId id of a user
* @return default + user's templates
*/
@Override
public List<Template> getUsableTemplates(int personId) {
String hqlQuery = "select distinct t from Template t where t.isDefault = true or t.personByPersonId.personId = :personId order by t.isDefault asc";
List<Template> list = getSessionFactory().getCurrentSession().
createQuery(hqlQuery).
setParameter("personId", personId).list();
return list;
}

@Override
public Template getTemplateByPersonAndName(int personId, String name) {
String hqlQuery = "from Template t where t.personByPersonId.personId = :personId and t.name=:name";
List<Template> list = getSessionFactory().getCurrentSession().
createQuery(hqlQuery).
setParameter("personId", personId).
setParameter("name", name).list();

if (list != null && !list.isEmpty()){
return list.get(0);
}
return null;
}

@Override
public boolean isDefault(int id) {
String hqlQuery = "select t.isDefault from Template t where t.templateId = :id";
List<Integer> list = getSessionFactory().getCurrentSession().createQuery(hqlQuery).setParameter("id", id).list();
if (list.isEmpty()) {
return false;
}
if (list.get(0) == 1) {
return true;
} else {
return false;
}
}

@Override
public boolean canSaveName(String name, int personId) {
String hqlQuery = "from Template t where t.personByPersonId.personId = :personId and t.name=:name";
List<Template> list = getSessionFactory().getCurrentSession().
createQuery(hqlQuery).
setParameter("personId", personId).
setParameter("name", name).list();
return (list.size() == 0);
}
}
51 changes: 51 additions & 0 deletions src/main/java/cz/zcu/kiv/eegdatabase/data/dao/TemplateDao.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package cz.zcu.kiv.eegdatabase.data.dao;

import cz.zcu.kiv.eegdatabase.data.pojo.Template;

import java.util.List;

/**
* ********************************************************************************************************************
* <p/>
* This file is part of the eegdatabase project
* <p/>
* ==========================================
* <p/>
* Copyright (C) 2014 by University of West Bohemia (http://www.zcu.cz/en/)
* <p/>
* **********************************************************************************************************************
* <p/>
* 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
* <p/>
* http://www.apache.org/licenses/LICENSE-2.0
* <p/>
* 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.
* <p/>
* **********************************************************************************************************************
* <p/>
* TemplateDao, 2014/07/02 11:18 Prokop
* <p/>
* ********************************************************************************************************************
*/
public interface TemplateDao extends GenericDao<Template, Integer> {

public List<Template> getTemplatesByPerson(int personId);

public List<Template> getDefaultTemplates();

/**
* Finds all default and user's templates
* @param personId id of a user
* @return default + user's templates
*/
public List<Template> getUsableTemplates(int personId);

public Template getTemplateByPersonAndName(int personId, String name);

public boolean isDefault(int id);

public boolean canSaveName(String name, int personId);
}
126 changes: 126 additions & 0 deletions src/main/java/cz/zcu/kiv/eegdatabase/data/pojo/Template.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
package cz.zcu.kiv.eegdatabase.data.pojo;

import org.hibernate.annotations.GenericGenerator;

import javax.persistence.*;
import java.io.Serializable;
import java.util.Arrays;

/**
* ********************************************************************************************************************
* <p/>
* This file is part of the eegdatabase project
* <p/>
* ==========================================
* <p/>
* Copyright (C) 2014 by University of West Bohemia (http://www.zcu.cz/en/)
* <p/>
* **********************************************************************************************************************
* <p/>
* 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
* <p/>
* http://www.apache.org/licenses/LICENSE-2.0
* <p/>
* 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.
* <p/>
* **********************************************************************************************************************
* <p/>
* Template, 2014/07/07 13:23 Prokop
* <p/>
* ********************************************************************************************************************
*/
@Entity
public class Template implements Serializable {
private int templateId;
private byte[] template;
private Person personByPersonId;
private Boolean isDefault;
private String name;

public Template() {
}

public Template(int templateId, byte[] template, Person personByPersonId) {
this.templateId = templateId;
this.template = template;
this.personByPersonId = personByPersonId;
}

@GenericGenerator(name = "generator", strategy = "increment")
@Id
@GeneratedValue(generator = "generator")
@Column(name = "template_id", nullable = false, insertable = true, updatable = true)
public int getTemplateId() {
return templateId;
}

public void setTemplateId(int templateId) {
this.templateId = templateId;
}

@Basic
@Column(name = "template", nullable = false, insertable = true, updatable = true)
public byte[] getTemplate() {
return template;
}

public void setTemplate(byte[] template) {
this.template = template;
}

@ManyToOne
@JoinColumn(name = "person_id", referencedColumnName = "person_id")
public Person getPersonByPersonId() {
return personByPersonId;
}

public void setPersonByPersonId(Person personByPersonId) {
this.personByPersonId = personByPersonId;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof Template)) return false;

Template template1 = (Template) o;

if (templateId != template1.templateId) return false;
if (personByPersonId != null ? !personByPersonId.equals(template1.personByPersonId) : template1.personByPersonId != null)
return false;
if (!Arrays.equals(template, template1.template)) return false;

return true;
}

@Override
public int hashCode() {
int result = templateId;
result = 31 * result + (template != null ? Arrays.hashCode(template) : 0);
result = 31 * result + (personByPersonId != null ? personByPersonId.hashCode() : 0);
return result;
}

@Basic
@Column(name = "is_default", nullable = true, insertable = true, updatable = true)
public Boolean getIsDefault() {
return isDefault;
}

public void setIsDefault(Boolean isDefault) {
this.isDefault = isDefault;
}

@Basic
@Column(name = "name", nullable = false, insertable = true, updatable = true, length = 240)
public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package cz.zcu.kiv.eegdatabase.wui.core.experiments.metadata;

import cz.zcu.kiv.eegdatabase.data.pojo.Template;
import cz.zcu.kiv.eegdatabase.wui.core.GenericFacade;

import java.util.List;

/**
* ********************************************************************************************************************
* <p/>
* This file is part of the eegdatabase project
* <p/>
* ==========================================
* <p/>
* Copyright (C) 2014 by University of West Bohemia (http://www.zcu.cz/en/)
* <p/>
* **********************************************************************************************************************
* <p/>
* 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
* <p/>
* http://www.apache.org/licenses/LICENSE-2.0
* <p/>
* 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.
* <p/>
* **********************************************************************************************************************
* <p/>
* TemplateFacade, 2014/07/08 14:44 Prokop
* <p/>
* ********************************************************************************************************************
*/
public interface TemplateFacade extends GenericFacade<Template, Integer> {

public List<Template> getTemplatesByPerson(int personId);

public List<Template> getDefaultTemplates();

/**
* Finds all default and user's templates
* @param personId id of a user
* @return default + user's templates
*/
public List<Template> getUsableTemplates(int personId);

public Template getTemplateByPersonAndName(int personId, String name);

public boolean isDefault(int id);

public boolean canSaveName(String name, int personId);
}
Loading

0 comments on commit 096d846

Please sign in to comment.