Newer
Older
/**
* OLAT - Online Learning and Training<br>
* http://www.olat.org
* <p>
* Licensed under the Apache License, Version 2.0 (the "License"); <br>
* you may not use this file except in compliance with the License.<br>
* 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,<br>
* software distributed under the License is distributed on an "AS IS" BASIS, <br>
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. <br>
* See the License for the specific language governing permissions and <br>
* limitations under the License.
* <p>
* Copyright (c) since 2004 at Multimedia- & E-Learning Services (MELS),<br>
* University of Zurich, Switzerland.
* <hr>
* <a href="http://www.openolat.org">
* OpenOLAT - Online Learning and Training</a><br>
* This file has been modified by the OpenOLAT community. Changes are licensed
* under the Apache 2.0 license as the original file.
*/
import java.util.Collections;
import java.util.List;
import org.olat.core.commons.persistence.DB;
import org.olat.core.id.OLATResourceable;
import org.olat.core.logging.AssertException;
import org.olat.core.manager.BasicManager;
import org.olat.core.util.CodeHelper;
import org.olat.core.util.coordinate.CoordinatorManager;
import org.olat.core.util.coordinate.SyncerCallback;
import org.olat.core.util.resource.OresHelper;
import org.olat.course.CourseModule;
/**
* A <b>SecurityResourceManager</b> is
*
* @author Andreas Ch. Kapp
*
*/
public class OLATResourceManager extends BasicManager {
private static OLATResourceManager INSTANCE;
private DB dbInstance;
/**
* @return Singleton
*/
public static OLATResourceManager getInstance() {
return INSTANCE;
}
/**
* [used by spring]
*/
private OLATResourceManager() {
INSTANCE = this;
}

srosse
committed
/**
* @param db
*/
public void setDbInstance(DB db) {
this.dbInstance = db;
}
/**
* Creates a new OLATResource instance (but does not persist the instance)
* @param resource
* @return OLATResource
*/
public OLATResource createOLATResourceInstance(OLATResourceable resource) {
return new OLATResourceImpl(resource);
}
public OLATResource createAndPersistOLATResourceInstance(OLATResourceable resource) {
OLATResource r = new OLATResourceImpl(resource);
saveOLATResource(r);
return r;
}
/**
* Creates a new OLATResource instance (but does not persist the instance)
* @param typeName
* @return OLATResource
*/
public OLATResource createOLATResourceInstance(String typeName) {
Long id = new Long(CodeHelper.getForeverUniqueID());
return new OLATResourceImpl(id, typeName);
}
/**
* Creates a new OLATResource instance (but does not persist the instance)
* @param aClass
* @return OLATResource
*/

srosse
committed
public OLATResource createOLATResourceInstance(Class<?> aClass) {
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
String typeName = OresHelper.calculateTypeName(aClass);
return createOLATResourceInstance(typeName);
}
/**
* Saves a resource.
* @param resource
* @return True upon success.
*/
public void saveOLATResource(OLATResource resource) {
if (resource.getResourceableTypeName().length() > 50) throw new AssertException("OlatResource: type length may not exceed 50 chars");
dbInstance.saveObject(resource);
}
/**
* Delete an existing resource.
* @param resource
* @return True upon success.
*/
public void deleteOLATResource(OLATResource resource) {
dbInstance.deleteObject(resource);
}
/**
*
* @param resourceable
* @return true if resourceable was found and deleted, false if it was
* not found.
*/
public void deleteOLATResourceable(OLATResourceable resourceable) {
OLATResource ores = findResourceable(resourceable);
if (ores == null) return;
deleteOLATResource(ores);
}
/**
* Find the OLATResource for the resourceable. If not found, a new
* OLATResource is created and returned.
*
* @param resourceable
* @return an OLATResource representing the resourceable.
*/
public OLATResource findOrPersistResourceable(final OLATResourceable resourceable) {
if (resourceable.getResourceableTypeName() == null) throw new AssertException("typename of olatresourceable can not be null");
// First try to find resourceable without synchronization
OLATResource ores = findResourceable(resourceable);
if (ores != null) {
return ores;
}
// Second there exists no resourcable => try to find and create(if no exists) in a synchronized block
//o_clusterOK by:cg
ores = CoordinatorManager.getInstance().getCoordinator().getSyncer().doInSync(resourceable, new SyncerCallback<OLATResource>(){
public OLATResource execute() {
logDebug("start synchronized-block in findOrPersistResourceable");
OLATResource oresSync = findResourceable(resourceable);
// if not found, persist it.
if (oresSync == null ) {

srosse
committed
if(CourseModule.ORES_TYPE_COURSE.equals(resourceable.getResourceableTypeName())) {
logInfo("OLATResourceManager - createOLATResourceInstance if not found: " + resourceable.getResourceableTypeName() + " " + resourceable.getResourceableId());

srosse
committed
}
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
oresSync = createOLATResourceInstance(resourceable);
saveOLATResource(oresSync);
}
return oresSync;
}
});
return ores;
}
/**
* Find a resourceanle
* @param resourceable
* @return OLATResource object or null if not found.
*/
public OLATResource findResourceable(OLATResourceable resourceable) {
String type = resourceable.getResourceableTypeName();
if (type == null) throw new AssertException("typename of olatresourceable must not be null");
Long id = resourceable.getResourceableId();
return doQueryResourceable(id, type);
}
/**
* Find a resourceable
* @param resourceableId
* @return OLATResource object or null if not found.
*/
public OLATResource findResourceable(Long resourceableId, String resourceableTypeName){
return doQueryResourceable(resourceableId, resourceableTypeName);
}
private OLATResource doQueryResourceable(Long resourceableId, String type){
if (resourceableId == null) resourceableId = OLATResourceImpl.NULLVALUE;

srosse
committed
String s = "select ori from org.olat.resource.OLATResourceImpl ori where ori.resName = :resname and ori.resId = :resid";
List<OLATResource> resources = dbInstance.getCurrentEntityManager()
.createQuery(s, OLATResource.class)
.setParameter("resname", type)
.setParameter("resid", resourceableId)

srosse
committed
.setHint("org.hibernate.cacheable", Boolean.TRUE)
.getResultList();

srosse
committed
if (resources.size() == 0) {
return null;
}
return resources.get(0);
public OLATResource findResourceById(Long key) {
if (key == null) return null;
String s = "select ori from org.olat.resource.OLATResourceImpl ori where ori.key=:resourceKey";
List<OLATResource> resources = dbInstance.getCurrentEntityManager()
.createQuery(s, OLATResource.class)
.setParameter("resourceKey", key)
.getResultList();
return resources.isEmpty() ? null : resources.get(0);
}
public List<OLATResource> findResourceByTypes(List<String> types) {
if(types == null || types.isEmpty()) return Collections.<OLATResource>emptyList();
String s = "select ori from org.olat.resource.OLATResourceImpl ori where ori.resName in (:restrictedType)";
List<OLATResource> resources = dbInstance.getCurrentEntityManager()
.createQuery(s, OLATResource.class)
.setParameter("restrictedType", types)
.getResultList();
return resources;
}