<commit_before>package com.sixsq.slipstream.persistence;

/*
 * +=================================================================+
 * SlipStream Server (WAR)
 * =====
 * Copyright (C) 2013 SixSq Sarl (sixsq.com)
 * =====
 * 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.
 * -=================================================================-
 */

import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.EntityManager;
import javax.persistence.Id;
import javax.persistence.Inheritance;
import javax.persistence.InheritanceType;
import javax.persistence.Lob;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.NoResultException;
import javax.persistence.OneToOne;
import javax.persistence.Query;
import javax.persistence.Transient;

import org.simpleframework.xml.Attribute;
import org.simpleframework.xml.Element;
import org.simpleframework.xml.ElementArray;

import com.sixsq.slipstream.exceptions.ValidationException;
import com.sixsq.slipstream.module.ModuleVersionView;
import com.sixsq.slipstream.module.ModuleView;
import com.sixsq.slipstream.run.RunView.RunViewList;
import com.sixsq.slipstream.util.ModuleUriUtil;

/**
 * Unit test see:
 * 
 * @see ModuleTest
 * 
 */
@SuppressWarnings("serial")
@Entity
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@NamedQueries({
		@NamedQuery(name = "moduleLastVersion", query = "SELECT m FROM Module m WHERE m.version = (SELECT MAX(m.version) FROM Module m WHERE m.name = :name AND m.deleted != TRUE)"),
		@NamedQuery(name = "moduleViewLatestChildren", query = "SELECT NEW com.sixsq.slipstream.module.ModuleView(m.resourceUri, m.description, m.category, m.customVersion, m.authz) FROM Module m WHERE m.parentUri = :parent AND m.version = (SELECT MAX(c.version) FROM Module c WHERE c.name = m.name AND c.deleted != TRUE)"),
		@NamedQuery(name = "moduleViewAllVersions", query = "SELECT NEW com.sixsq.slipstream.module.ModuleVersionView(m.resourceUri, m.version, m.lastModified, m.commit, m.authz, m.category) FROM Module m WHERE m.name = :name AND m.deleted != TRUE"),
		@NamedQuery(name = "moduleAll", query = "SELECT m FROM Module m WHERE m.deleted != TRUE"),
		@NamedQuery(name = "moduleViewPublished", query = "SELECT NEW com.sixsq.slipstream.module.ModuleViewPublished(m.resourceUri, m.description, m.category, m.customVersion, m.authz, m.logoLink) FROM Module m WHERE m.published != null AND m.deleted != TRUE") })
public abstract class Module extends Parameterized<Module, ModuleParameter> {

	public final static String RESOURCE_URI_PREFIX = "module/";

	public final static int DEFAULT_VERSION = -1;

	private static Module loadByUri(String uri) {
		EntityManager em = PersistenceUtil.createEntityManager();
		Module m = em.find(Module.class, uri);

		Module latestVersion = loadLatest(uri);
		if (latestVersion == null) return m;
		
		m.setIsLatestVersion(latestVersion.version);
		return m;
	}

	public static Module loadLatest(String resourceUri) {
		EntityManager em = PersistenceUtil.createEntityManager();
		Query q = em.createNamedQuery("moduleLastVersion");
		String name = ModuleUriUtil
				.extractModuleNameFromResourceUri(resourceUri);
		q.setParameter("name", name);
		Module module;
		try {
			module = (Module) q.getSingleResult();
			module.setIsLatestVersion(module.version);
		} catch (NoResultException ex) {
			module = null;
		}
		return module;<commit_msg>Bugfix<commit_after>package com.sixsq.slipstream.persistence;

/*
 * +=================================================================+
 * SlipStream Server (WAR)
 * =====
 * Copyright (C) 2013 SixSq Sarl (sixsq.com)
 * =====
 * 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.
 * -=================================================================-
 */

import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.EntityManager;
import javax.persistence.Id;
import javax.persistence.Inheritance;
import javax.persistence.InheritanceType;
import javax.persistence.Lob;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.NoResultException;
import javax.persistence.OneToOne;
import javax.persistence.Query;
import javax.persistence.Transient;

import org.simpleframework.xml.Attribute;
import org.simpleframework.xml.Element;
import org.simpleframework.xml.ElementArray;

import com.sixsq.slipstream.exceptions.ValidationException;
import com.sixsq.slipstream.module.ModuleVersionView;
import com.sixsq.slipstream.module.ModuleView;
import com.sixsq.slipstream.run.RunView.RunViewList;
import com.sixsq.slipstream.util.ModuleUriUtil;

/**
 * Unit test see:
 * 
 * @see ModuleTest
 * 
 */
@SuppressWarnings("serial")
@Entity
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@NamedQueries({
		@NamedQuery(name = "moduleLastVersion", query = "SELECT m FROM Module m WHERE m.version = (SELECT MAX(m.version) FROM Module m WHERE m.name = :name AND m.deleted != TRUE)"),
		@NamedQuery(name = "moduleViewLatestChildren", query = "SELECT NEW com.sixsq.slipstream.module.ModuleView(m.resourceUri, m.description, m.category, m.customVersion, m.authz) FROM Module m WHERE m.parentUri = :parent AND m.version = (SELECT MAX(c.version) FROM Module c WHERE c.name = m.name AND c.deleted != TRUE)"),
		@NamedQuery(name = "moduleViewAllVersions", query = "SELECT NEW com.sixsq.slipstream.module.ModuleVersionView(m.resourceUri, m.version, m.lastModified, m.commit, m.authz, m.category) FROM Module m WHERE m.name = :name AND m.deleted != TRUE"),
		@NamedQuery(name = "moduleAll", query = "SELECT m FROM Module m WHERE m.deleted != TRUE"),
		@NamedQuery(name = "moduleViewPublished", query = "SELECT NEW com.sixsq.slipstream.module.ModuleViewPublished(m.resourceUri, m.description, m.category, m.customVersion, m.authz, m.logoLink) FROM Module m WHERE m.published != null AND m.deleted != TRUE") })
public abstract class Module extends Parameterized<Module, ModuleParameter> {

	public final static String RESOURCE_URI_PREFIX = "module/";

	public final static int DEFAULT_VERSION = -1;

	private static Module loadByUri(String uri) {
		EntityManager em = PersistenceUtil.createEntityManager();
		Module m = em.find(Module.class, uri);

		Module latestVersion = loadLatest(uri);
		if (latestVersion == null || m == null) return m;
		
		m.setIsLatestVersion(latestVersion.version);
		return m;
	}

	public static Module loadLatest(String resourceUri) {
		EntityManager em = PersistenceUtil.createEntityManager();
		Query q = em.createNamedQuery("moduleLastVersion");
		String name = ModuleUriUtil
				.extractModuleNameFromResourceUri(resourceUri);
		q.setParameter("name", name);
		Module module;
		try {
			module = (Module) q.getSingleResult();
			module.setIsLatestVersion(module.version);
		} catch (NoResultException ex) {
			module = null;
		}
		return module;
{'fraction_non_alphanumeric': 0.13896595208070617, 'fraction_numerical': 0.002269861286254729, 'mean_word_length': 5.259668508287293, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 6}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '535458', 'n_tokens_mistral': 2355, 'n_tokens_neox': 2280, 'n_words': 635}