View Javadoc
1   /**
2    * Copyright 2007 Björn Voß
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5    * use this file except in compliance with the License. You may obtain a copy of
6    * the License at
7    *
8    * http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13   * License for the specific language governing permissions and limitations under
14   * the License.
15   */
16  package net.sf.oxclient.service;
17  
18  import java.util.List;
19  import java.util.Map;
20  
21  import net.sf.oval.constraint.NotEmpty;
22  import net.sf.oval.constraint.NotNull;
23  import net.sf.oval.guard.Guarded;
24  import net.sf.oxclient.IGroupwareModule;
25  import net.sf.oxclient.IGroupwareSession;
26  import net.sf.oxclient.IQuery;
27  import net.sf.oxclient.mappings.IMappingManager;
28  
29  /**
30   * @author Björn Voß
31   *
32   */
33  @Guarded
34  public abstract class AbsGroupwareModule<ModuleEntityBean, FolderBean>
35  		implements IGroupwareModule<ModuleEntityBean, FolderBean> {
36  
37  	private final String moduleName;
38  	private final Class<ModuleEntityBean> entityClass;
39  	protected IMappingManager mappingManager;
40  	private IGroupwareSession<FolderBean> session = null;
41  
42  	/**
43  	 * @param moduleName
44  	 */
45  	protected AbsGroupwareModule(
46  			@NotNull @NotEmpty final String moduleName,
47  			@NotNull final Class<ModuleEntityBean> entityClass) {
48  		super();
49  		this.moduleName = moduleName;
50  		this.entityClass = entityClass;
51  	}
52  
53  	/* (non-Javadoc)
54  	 * @see net.sf.oxclient.IGroupwareModule#create(java.lang.Object)
55  	 */
56  	public abstract ModuleEntityBean create(final FolderBean parent);
57  
58  	/* (non-Javadoc)
59  	 * @see net.sf.oxclient.IGroupwareModule#create()
60  	 */
61  	public ModuleEntityBean create() {
62  		final FolderBean parent = getDefaultFolder();
63  		return create(parent);
64  	}
65  
66  	/* (non-Javadoc)
67  	 * @see net.sf.oxclient.IGroupwareModule#getByID(java.lang.String)
68  	 */
69  	public ModuleEntityBean getByID(@NotNull @NotEmpty final String objectId) {
70  		final IQuery query = createQuery();
71  		final Map<String, String> params = getQueryParameter(objectId);
72  		query.setParameters(params);
73  		final Map<Integer, String> columns = getColumnMappings();
74  		query.setPropertyColumns(columns);
75  		final ModuleEntityBean result = query.queryForObject(this.entityClass);
76  		return result;
77  	}
78  
79  	public List<ModuleEntityBean> list(final FolderBean folder) {
80  		final IQuery query = createQuery();
81  		query.setParameters(getListParameters(folder));
82  		query.setPropertyColumns(getColumnMappings());
83  		return query.queryForList(getEntityClass());
84  	}
85  
86  	/* (non-Javadoc)
87  	 * @see net.sf.oxclient.IGroupwareModule#getName()
88  	 */
89  	public String getName() {
90  		return this.moduleName;
91  	}
92  
93  	public abstract Map<String, String> getListParameters(final FolderBean parent);
94  
95  	/* (non-Javadoc)
96  	 * @see net.sf.oxclient.IGroupwareModule#save(java.lang.Object)
97  	 */
98  	public void save(final ModuleEntityBean bean) {
99  		// TODO Auto-generated method stub
100 
101 	}
102 
103 	public abstract Map<String, String> getQueryParameter(final String objectId);
104 
105 	public Map<Integer, String> getColumnMappings() {
106 		return this.mappingManager.getMappingForClass(this.entityClass);
107 	}
108 
109 	/* (non-Javadoc)
110 	 * @see net.sf.oxclient.IGroupwareModule#createQuery()
111 	 */
112 	public IQuery createQuery() {
113 		return this.session.createQuery(this.moduleName);
114 	}
115 
116 	/**
117 	 * @param mappingManager the mappingManager to set
118 	 */
119 	public void setMappingManager(final IMappingManager mappingManager) {
120 		this.mappingManager = mappingManager;
121 	}
122 
123 	/**
124 	 * @return the session
125 	 */
126 	public IGroupwareSession<FolderBean> getSession() {
127 		return this.session;
128 	}
129 
130 	/**
131 	 * @param session the session to set
132 	 */
133 	public void setSession(final IGroupwareSession<FolderBean> session) {
134 		this.session = session;
135 	}
136 
137 	/**
138 	 * @return the entityClass
139 	 */
140 	protected Class<ModuleEntityBean> getEntityClass() {
141 		return this.entityClass;
142 	}
143 }