<commit_before>/*
 * The contents of this file are subject to the OpenMRS Public 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://license.openmrs.org
 *
 * Software distributed under the License is distributed on an "AS IS"
 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
 * License for the specific language governing rights and limitations
 * under the License.
 *
 * Copyright (C) OpenMRS, LLC.  All Rights Reserved.
 */
package org.openmrs.module.openhmis.commons.api.entity.impl;

import org.apache.commons.lang3.StringUtils;
import org.hibernate.Criteria;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Restrictions;
import org.openmrs.OpenmrsMetadata;
import org.openmrs.OpenmrsObject;
import org.openmrs.User;
import org.openmrs.api.APIException;
import org.openmrs.api.context.Context;
import org.openmrs.module.openhmis.commons.api.PagingInfo;
import org.openmrs.module.openhmis.commons.api.Utility;
import org.openmrs.module.openhmis.commons.api.entity.IMetadataDataService;
import org.openmrs.module.openhmis.commons.api.entity.security.IMetadataAuthorizationPrivileges;
import org.openmrs.module.openhmis.commons.api.f.Action1;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.List;

/**
 * The base type for {@link org.openmrs.module.openhmis.commons.api.entity.IMetadataDataService}s.
 * @param <E> The entity metadata model type.
 */
@Transactional
public abstract class BaseMetadataDataServiceImpl<E extends OpenmrsMetadata>
		extends BaseObjectDataServiceImpl<E, IMetadataAuthorizationPrivileges>
		implements IMetadataDataService<E> {
	@Override
	@Transactional
	public E retire(E entity, String reason) throws APIException {
		IMetadataAuthorizationPrivileges privileges = getPrivileges();
		if (privileges != null && !StringUtils.isEmpty(privileges.getRetirePrivilege())) {
			Context.requirePrivilege(privileges.getRetirePrivilege());
		}

		if (entity == null) {
			throw new NullPointerException("The entity to retire cannot be null.");
		}
		if (StringUtils.isEmpty(reason)) {
			throw new IllegalArgumentException("The reason to retire must be defined.");
		}

		User user = Context.getAuthenticatedUser();
		Date dateRetired = new Date();
		setRetireProperties(entity, reason, user, dateRetired);

		Collection<? extends OpenmrsObject> relatedObjects = getRelatedObjects(entity);<commit_msg>Added a default sort by name for all metadata data services.<commit_after>/*
 * The contents of this file are subject to the OpenMRS Public 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://license.openmrs.org
 *
 * Software distributed under the License is distributed on an "AS IS"
 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
 * License for the specific language governing rights and limitations
 * under the License.
 *
 * Copyright (C) OpenMRS, LLC.  All Rights Reserved.
 */
package org.openmrs.module.openhmis.commons.api.entity.impl;

import org.apache.commons.lang3.StringUtils;
import org.hibernate.Criteria;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.openmrs.OpenmrsMetadata;
import org.openmrs.OpenmrsObject;
import org.openmrs.User;
import org.openmrs.api.APIException;
import org.openmrs.api.context.Context;
import org.openmrs.module.openhmis.commons.api.PagingInfo;
import org.openmrs.module.openhmis.commons.api.Utility;
import org.openmrs.module.openhmis.commons.api.entity.IMetadataDataService;
import org.openmrs.module.openhmis.commons.api.entity.security.IMetadataAuthorizationPrivileges;
import org.openmrs.module.openhmis.commons.api.f.Action1;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.List;

/**
 * The base type for {@link org.openmrs.module.openhmis.commons.api.entity.IMetadataDataService}s.
 * @param <E> The entity metadata model type.
 */
@Transactional
public abstract class BaseMetadataDataServiceImpl<E extends OpenmrsMetadata>
		extends BaseObjectDataServiceImpl<E, IMetadataAuthorizationPrivileges>
		implements IMetadataDataService<E> {
	@Override
	protected Order[] getDefaultSort() {
		// By default, use the name as the sorting column for metadata
		return new Order[] { Order.asc("name") };
	}

	@Override
	@Transactional
	public E retire(E entity, String reason) throws APIException {
		IMetadataAuthorizationPrivileges privileges = getPrivileges();
		if (privileges != null && !StringUtils.isEmpty(privileges.getRetirePrivilege())) {
			Context.requirePrivilege(privileges.getRetirePrivilege());
		}

		if (entity == null) {
			throw new NullPointerException("The entity to retire cannot be null.");
		}
		if (StringUtils.isEmpty(reason)) {
			throw new IllegalArgumentException("The reason to retire must be defined.");
		}

		User user = Context.getAuthenticatedUser();
		Date dateRetired = new Date();
		setRetireProperties(entity, reason, user, dateRetired);

		Collection<? extends OpenmrsObject> relatedObjects = getRelatedObjects(entity);
{'fraction_non_alphanumeric': 0.09622815087396504, 'fraction_numerical': 0.0014719411223551057, 'mean_word_length': 5.350467289719626, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, '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': '6894703', 'n_tokens_mistral': 1648, 'n_tokens_neox': 1591, 'n_words': 438}