<commit_before>/**
 *  Copyright (C) 2010 Cloud.com, Inc.  All rights reserved.
 * 
 * This software is licensed under the GNU General Public License v3 or later.
 * 
 * It is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or any later version.
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 * 
 */

package com.cloud.storage.dao;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.TimeZone;

import javax.ejb.Local;
import javax.naming.ConfigurationException;

import org.apache.log4j.Logger;

import com.cloud.host.Host;
import com.cloud.host.HostVO;
import com.cloud.host.dao.HostDao;
import com.cloud.storage.VMTemplateHostVO;
import com.cloud.storage.VMTemplateStorageResourceAssoc.Status;
import com.cloud.utils.DateUtil;
import com.cloud.utils.component.Inject;
import com.cloud.utils.db.GenericDaoBase;
import com.cloud.utils.db.JoinBuilder;
import com.cloud.utils.db.SearchBuilder;
import com.cloud.utils.db.SearchCriteria;
import com.cloud.utils.db.Transaction;

@Local(value={VMTemplateHostDao.class})
public class VMTemplateHostDaoImpl extends GenericDaoBase<VMTemplateHostVO, Long> implements VMTemplateHostDao {
	public static final Logger s_logger = Logger.getLogger(VMTemplateHostDaoImpl.class.getName());
    @Inject
    HostDao   _hostDao;
	protected final SearchBuilder<VMTemplateHostVO> HostSearch;
	protected final SearchBuilder<VMTemplateHostVO> TemplateSearch;
	protected final SearchBuilder<VMTemplateHostVO> HostTemplateSearch;
	protected final SearchBuilder<VMTemplateHostVO> HostTemplateStateSearch;
	protected final SearchBuilder<VMTemplateHostVO> HostDestroyedSearch;
	protected final SearchBuilder<VMTemplateHostVO> TemplateStatusSearch;
	protected final SearchBuilder<VMTemplateHostVO> TemplateStatesSearch;
	protected SearchBuilder<VMTemplateHostVO> ZONE_TEMPLATE_SEARCH;
	protected SearchBuilder<VMTemplateHostVO> LOCAL_SECONDARY_STORAGE_SEARCH;

	
	protected static final String UPDATE_TEMPLATE_HOST_REF =
		"UPDATE template_host_ref SET download_state = ?, download_pct= ?, last_updated = ? "
	+   ", error_str = ?, local_path = ?, job_id = ? "
	+   "WHERE host_id = ? and type_id = ?";
			
	protected static final String DOWNLOADS_STATE_DC=
		"SELECT t.id, t.host_id, t.template_id, t.created, t.last_updated, t.job_id, " 
	+   "t.download_pct, t.size, t.physical_size, t.download_state, t.error_str, t.local_path, "
	+   "t.install_path, t.url, t.destroyed, t.is_copy FROM template_host_ref t, host h " 
	+   "where t.host_id = h.id and h.data_center_id=? "
	+	" and t.template_id=? and t.download_state = ?" ;
	
	protected static final String DOWNLOADS_STATE_DC_POD=
		"SELECT * FROM template_host_ref t, host h where t.host_id = h.id and h.data_center_id=? and h.pod_id=? "
	+	" and t.template_id=? and t.download_state=?" ;
	
	protected static final String DOWNLOADS_STATE=
		"SELECT * FROM template_host_ref t "
	+	" where t.template_id=? and t.download_state=?";
	
	public VMTemplateHostDaoImpl () {
		HostSearch = createSearchBuilder();
		HostSearch.and("host_id", HostSearch.entity().getHostId(), SearchCriteria.Op.EQ);
		HostSearch.done();
		
		TemplateSearch = createSearchBuilder();
		TemplateSearch.and("template_id", TemplateSearch.entity().getTemplateId(), SearchCriteria.Op.EQ);
		TemplateSearch.and("destroyed", TemplateSearch.entity().getDestroyed(), SearchCriteria.Op.EQ);
		TemplateSearch.done();
		
		HostTemplateSearch = createSearchBuilder();
		HostTemplateSearch.and("host_id", HostTemplateSearch.entity().getHostId(), SearchCriteria.Op.EQ);
		HostTemplateSearch.and("template_id", HostTemplateSearch.entity().getTemplateId(), SearchCriteria.Op.EQ);
		HostTemplateSearch.done();
		
		HostDestroyedSearch = createSearchBuilder();
		HostDestroyedSearch.and("host_id", HostDestroyedSearch.entity().getHostId(), SearchCriteria.Op.EQ);
		HostDestroyedSearch.and("destroyed", HostDestroyedSearch.entity().getDestroyed(), SearchCriteria.Op.EQ);
		HostDestroyedSearch.done();		
		
		TemplateStatusSearch = createSearchBuilder();
		TemplateStatusSearch.and("template_id", TemplateStatusSearch.entity().getTemplateId(), SearchCriteria.Op.EQ);
		TemplateStatusSearch.and("download_state", TemplateStatusSearch.entity().getDownloadState(), SearchCriteria.Op.EQ);
		TemplateStatusSearch.done();

        TemplateStatesSearch = createSearchBuilder();
        TemplateStatesSearch.and("template_id", TemplateStatesSearch.entity().getTemplateId(), SearchCriteria.Op.EQ);
        TemplateStatesSearch.and("states", TemplateStatesSearch.entity().getDownloadState(), SearchCriteria.Op.IN);
        TemplateStatesSearch.done();

        HostTemplateStateSearch = createSearchBuilder();
        HostTemplateStateSearch.and("template_id", HostTemplateStateSearch.entity().getTemplateId(), SearchCriteria.Op.EQ);
        HostTemplateStateSearch.and("host_id", HostTemplateStateSearch.entity().getHostId(), SearchCriteria.Op.EQ);
        HostTemplateStateSearch.and("states", HostTemplateStateSearch.entity().getDownloadState(), SearchCriteria.Op.IN);
        HostTemplateStateSearch.done();

    }
	
	@Override
	public boolean configure(String name, Map<String, Object> params) throws ConfigurationException {
	    boolean result = super.configure(name, params);
	    ZONE_TEMPLATE_SEARCH = createSearchBuilder();
	    ZONE_TEMPLATE_SEARCH.and("template_id", ZONE_TEMPLATE_SEARCH.entity().getTemplateId(), SearchCriteria.Op.EQ);
	    ZONE_TEMPLATE_SEARCH.and("state", ZONE_TEMPLATE_SEARCH.entity().getDownloadState(), SearchCriteria.Op.EQ);
	    SearchBuilder<HostVO> hostSearch = _hostDao.createSearchBuilder();
	    hostSearch.and("zone_id", hostSearch.entity().getDataCenterId(), SearchCriteria.Op.EQ);
	    ZONE_TEMPLATE_SEARCH.join("tmplHost", hostSearch, hostSearch.entity().getId(), ZONE_TEMPLATE_SEARCH.entity().getHostId(), JoinBuilder.JoinType.INNER);
	    ZONE_TEMPLATE_SEARCH.done();
	    
	    LOCAL_SECONDARY_STORAGE_SEARCH = createSearchBuilder();
	    LOCAL_SECONDARY_STORAGE_SEARCH.and("template_id", LOCAL_SECONDARY_STORAGE_SEARCH.entity().getTemplateId(), SearchCriteria.Op.EQ);
	    LOCAL_SECONDARY_STORAGE_SEARCH.and("state", LOCAL_SECONDARY_STORAGE_SEARCH.entity().getDownloadState(), SearchCriteria.Op.EQ);
	    SearchBuilder<HostVO> localSecondaryHost = _hostDao.createSearchBuilder();
	    localSecondaryHost.and("private_ip_address", localSecondaryHost.entity().getPrivateIpAddress(), SearchCriteria.Op.EQ);
	    localSecondaryHost.and("state", localSecondaryHost.entity().getStatus(), SearchCriteria.Op.EQ);
	    localSecondaryHost.and("data_center_id", localSecondaryHost.entity().getDataCenterId(), SearchCriteria.Op.EQ);
	    localSecondaryHost.and("type", localSecondaryHost.entity().getType(), SearchCriteria.Op.EQ);
	    LOCAL_SECONDARY_STORAGE_SEARCH.join("host", localSecondaryHost, localSecondaryHost.entity().getId(), LOCAL_SECONDARY_STORAGE_SEARCH.entity().getHostId(), JoinBuilder.JoinType.INNER);
	    LOCAL_SECONDARY_STORAGE_SEARCH.done();
	    
	    return result;
	}
	@Override
    public void update(VMTemplateHostVO instance) {
        Transaction txn = Transaction.currentTxn();
		PreparedStatement pstmt = null;
		try {
			Date now = new Date();
			String sql = UPDATE_TEMPLATE_HOST_REF;
			pstmt = txn.prepareAutoCloseStatement(sql);
			pstmt.setString(1, instance.getDownloadState().toString());
			pstmt.setInt(2, instance.getDownloadPercent());
			pstmt.setString(3, DateUtil.getDateDisplayString(TimeZone.getTimeZone("GMT"), now));
			pstmt.setString(4, instance.getErrorString());
			pstmt.setString(5, instance.getLocalDownloadPath());
			pstmt.setString(6, instance.getJobId());
			pstmt.setLong(7, instance.getHostId());
			pstmt.setLong(8, instance.getTemplateId());
			pstmt.executeUpdate();
		} catch (Exception e) {
			s_logger.warn("Exception: ", e);
		}
	}	

	@Override
	public List<VMTemplateHostVO> listByHostId(long id) {
	    SearchCriteria<VMTemplateHostVO> sc = HostSearch.create();
	    sc.setParameters("host_id", id);
	    return listIncludingRemovedBy(sc);
	}

	@Override
	public List<VMTemplateHostVO> listByTemplateId(long templateId) {
	    SearchCriteria<VMTemplateHostVO> sc = TemplateSearch.create();
	    sc.setParameters("template_id", templateId);
	    sc.setParameters("destroyed", false);
	    return listIncludingRemovedBy(sc);
	}
	
	
	@Override
	public List<VMTemplateHostVO> listByOnlyTemplateId(long templateId) {
	    SearchCriteria<VMTemplateHostVO> sc = TemplateSearch.create();
	    sc.setParameters("template_id", templateId);	    
	    return listIncludingRemovedBy(sc);
	}

	@Override
	public VMTemplateHostVO findByHostTemplate(long hostId, long templateId) {
		SearchCriteria<VMTemplateHostVO> sc = HostTemplateSearch.create();
	    sc.setParameters("host_id", hostId);
	    sc.setParameters("template_id", templateId);
	    return findOneIncludingRemovedBy(sc);
	}
	
	@Override<commit_msg>SWIFT : fix, create vm on zone2<commit_after>/**
 *  Copyright (C) 2010 Cloud.com, Inc.  All rights reserved.
 * 
 * This software is licensed under the GNU General Public License v3 or later.
 * 
 * It is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or any later version.
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 * 
 */

package com.cloud.storage.dao;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.TimeZone;

import javax.ejb.Local;
import javax.naming.ConfigurationException;

import org.apache.log4j.Logger;

import com.cloud.host.Host;
import com.cloud.host.HostVO;
import com.cloud.host.dao.HostDao;
import com.cloud.storage.VMTemplateHostVO;
import com.cloud.storage.VMTemplateStorageResourceAssoc.Status;
import com.cloud.utils.DateUtil;
import com.cloud.utils.component.Inject;
import com.cloud.utils.db.GenericDaoBase;
import com.cloud.utils.db.JoinBuilder;
import com.cloud.utils.db.SearchBuilder;
import com.cloud.utils.db.SearchCriteria;
import com.cloud.utils.db.Transaction;

@Local(value={VMTemplateHostDao.class})
public class VMTemplateHostDaoImpl extends GenericDaoBase<VMTemplateHostVO, Long> implements VMTemplateHostDao {
	public static final Logger s_logger = Logger.getLogger(VMTemplateHostDaoImpl.class.getName());
    @Inject
    HostDao   _hostDao;
	protected final SearchBuilder<VMTemplateHostVO> HostSearch;
	protected final SearchBuilder<VMTemplateHostVO> TemplateSearch;
	protected final SearchBuilder<VMTemplateHostVO> HostTemplateSearch;
	protected final SearchBuilder<VMTemplateHostVO> HostTemplateStateSearch;
	protected final SearchBuilder<VMTemplateHostVO> HostDestroyedSearch;
	protected final SearchBuilder<VMTemplateHostVO> TemplateStatusSearch;
	protected final SearchBuilder<VMTemplateHostVO> TemplateStatesSearch;
	protected SearchBuilder<VMTemplateHostVO> ZONE_TEMPLATE_SEARCH;
	protected SearchBuilder<VMTemplateHostVO> LOCAL_SECONDARY_STORAGE_SEARCH;

	
	protected static final String UPDATE_TEMPLATE_HOST_REF =
		"UPDATE template_host_ref SET download_state = ?, download_pct= ?, last_updated = ? "
	+   ", error_str = ?, local_path = ?, job_id = ? "
	+   "WHERE host_id = ? and type_id = ?";
			
	protected static final String DOWNLOADS_STATE_DC=
		"SELECT t.id, t.host_id, t.template_id, t.created, t.last_updated, t.job_id, " 
	+   "t.download_pct, t.size, t.physical_size, t.download_state, t.error_str, t.local_path, "
	+   "t.install_path, t.url, t.destroyed, t.is_copy FROM template_host_ref t, host h " 
	+   "where t.host_id = h.id and h.data_center_id=? "
	+	" and t.template_id=? and t.download_state = ?" ;
	
	protected static final String DOWNLOADS_STATE_DC_POD=
		"SELECT * FROM template_host_ref t, host h where t.host_id = h.id and h.data_center_id=? and h.pod_id=? "
	+	" and t.template_id=? and t.download_state=?" ;
	
	protected static final String DOWNLOADS_STATE=
		"SELECT * FROM template_host_ref t "
	+	" where t.template_id=? and t.download_state=?";
	
	public VMTemplateHostDaoImpl () {
		HostSearch = createSearchBuilder();
		HostSearch.and("host_id", HostSearch.entity().getHostId(), SearchCriteria.Op.EQ);
		HostSearch.done();
		
		TemplateSearch = createSearchBuilder();
		TemplateSearch.and("template_id", TemplateSearch.entity().getTemplateId(), SearchCriteria.Op.EQ);
		TemplateSearch.and("destroyed", TemplateSearch.entity().getDestroyed(), SearchCriteria.Op.EQ);
		TemplateSearch.done();
		
		HostTemplateSearch = createSearchBuilder();
		HostTemplateSearch.and("host_id", HostTemplateSearch.entity().getHostId(), SearchCriteria.Op.EQ);
		HostTemplateSearch.and("template_id", HostTemplateSearch.entity().getTemplateId(), SearchCriteria.Op.EQ);
		HostTemplateSearch.done();
		
		HostDestroyedSearch = createSearchBuilder();
		HostDestroyedSearch.and("host_id", HostDestroyedSearch.entity().getHostId(), SearchCriteria.Op.EQ);
		HostDestroyedSearch.and("destroyed", HostDestroyedSearch.entity().getDestroyed(), SearchCriteria.Op.EQ);
		HostDestroyedSearch.done();		
		
		TemplateStatusSearch = createSearchBuilder();
		TemplateStatusSearch.and("template_id", TemplateStatusSearch.entity().getTemplateId(), SearchCriteria.Op.EQ);
		TemplateStatusSearch.and("download_state", TemplateStatusSearch.entity().getDownloadState(), SearchCriteria.Op.EQ);
		TemplateStatusSearch.done();

        TemplateStatesSearch = createSearchBuilder();
        TemplateStatesSearch.and("template_id", TemplateStatesSearch.entity().getTemplateId(), SearchCriteria.Op.EQ);
        TemplateStatesSearch.and("states", TemplateStatesSearch.entity().getDownloadState(), SearchCriteria.Op.IN);
        TemplateStatesSearch.done();

        HostTemplateStateSearch = createSearchBuilder();
        HostTemplateStateSearch.and("template_id", HostTemplateStateSearch.entity().getTemplateId(), SearchCriteria.Op.EQ);
        HostTemplateStateSearch.and("host_id", HostTemplateStateSearch.entity().getHostId(), SearchCriteria.Op.EQ);
        HostTemplateStateSearch.and("states", HostTemplateStateSearch.entity().getDownloadState(), SearchCriteria.Op.IN);
        HostTemplateStateSearch.done();

    }
	
	@Override
	public boolean configure(String name, Map<String, Object> params) throws ConfigurationException {
	    boolean result = super.configure(name, params);
	    ZONE_TEMPLATE_SEARCH = createSearchBuilder();
	    ZONE_TEMPLATE_SEARCH.and("template_id", ZONE_TEMPLATE_SEARCH.entity().getTemplateId(), SearchCriteria.Op.EQ);
	    ZONE_TEMPLATE_SEARCH.and("state", ZONE_TEMPLATE_SEARCH.entity().getDownloadState(), SearchCriteria.Op.EQ);
	    SearchBuilder<HostVO> hostSearch = _hostDao.createSearchBuilder();
	    hostSearch.and("zone_id", hostSearch.entity().getDataCenterId(), SearchCriteria.Op.EQ);
	    ZONE_TEMPLATE_SEARCH.join("tmplHost", hostSearch, hostSearch.entity().getId(), ZONE_TEMPLATE_SEARCH.entity().getHostId(), JoinBuilder.JoinType.INNER);
	    ZONE_TEMPLATE_SEARCH.done();
	    
	    LOCAL_SECONDARY_STORAGE_SEARCH = createSearchBuilder();
	    LOCAL_SECONDARY_STORAGE_SEARCH.and("template_id", LOCAL_SECONDARY_STORAGE_SEARCH.entity().getTemplateId(), SearchCriteria.Op.EQ);
	    LOCAL_SECONDARY_STORAGE_SEARCH.and("state", LOCAL_SECONDARY_STORAGE_SEARCH.entity().getDownloadState(), SearchCriteria.Op.EQ);
	    SearchBuilder<HostVO> localSecondaryHost = _hostDao.createSearchBuilder();
	    localSecondaryHost.and("private_ip_address", localSecondaryHost.entity().getPrivateIpAddress(), SearchCriteria.Op.EQ);
	    localSecondaryHost.and("state", localSecondaryHost.entity().getStatus(), SearchCriteria.Op.EQ);
	    localSecondaryHost.and("data_center_id", localSecondaryHost.entity().getDataCenterId(), SearchCriteria.Op.EQ);
	    localSecondaryHost.and("type", localSecondaryHost.entity().getType(), SearchCriteria.Op.EQ);
	    LOCAL_SECONDARY_STORAGE_SEARCH.join("host", localSecondaryHost, localSecondaryHost.entity().getId(), LOCAL_SECONDARY_STORAGE_SEARCH.entity().getHostId(), JoinBuilder.JoinType.INNER);
	    LOCAL_SECONDARY_STORAGE_SEARCH.done();
	    
	    return result;
	}
	@Override
    public void update(VMTemplateHostVO instance) {
        Transaction txn = Transaction.currentTxn();
		PreparedStatement pstmt = null;
		try {
			Date now = new Date();
			String sql = UPDATE_TEMPLATE_HOST_REF;
			pstmt = txn.prepareAutoCloseStatement(sql);
			pstmt.setString(1, instance.getDownloadState().toString());
			pstmt.setInt(2, instance.getDownloadPercent());
			pstmt.setString(3, DateUtil.getDateDisplayString(TimeZone.getTimeZone("GMT"), now));
			pstmt.setString(4, instance.getErrorString());
			pstmt.setString(5, instance.getLocalDownloadPath());
			pstmt.setString(6, instance.getJobId());
			pstmt.setLong(7, instance.getHostId());
			pstmt.setLong(8, instance.getTemplateId());
			pstmt.executeUpdate();
		} catch (Exception e) {
			s_logger.warn("Exception: ", e);
		}
	}	

	@Override
	public List<VMTemplateHostVO> listByHostId(long id) {
	    SearchCriteria<VMTemplateHostVO> sc = HostSearch.create();
	    sc.setParameters("host_id", id);
	    return listIncludingRemovedBy(sc);
	}

	@Override
	public List<VMTemplateHostVO> listByTemplateId(long templateId) {
	    SearchCriteria<VMTemplateHostVO> sc = TemplateSearch.create();
	    sc.setParameters("template_id", templateId);
	    sc.setParameters("destroyed", false);
	    return listIncludingRemovedBy(sc);
	}
	
	
	@Override
	public List<VMTemplateHostVO> listByOnlyTemplateId(long templateId) {
	    SearchCriteria<VMTemplateHostVO> sc = TemplateSearch.create();
	    sc.setParameters("template_id", templateId);	    
	    return listIncludingRemovedBy(sc);
	}

	@Override
	public VMTemplateHostVO findByHostTemplate(long hostId, long templateId) {
		SearchCriteria<VMTemplateHostVO> sc = HostTemplateSearch.create();
	    sc.setParameters("host_id", hostId);
	    sc.setParameters("template_id", templateId);
        sc.setParameters("download_state", Status.DOWNLOADED.toString());
	    return findOneIncludingRemovedBy(sc);
	}
	
	@Override
{'fraction_non_alphanumeric': 0.1130558183538316, 'fraction_numerical': 0.0016293493114685168, 'mean_word_length': 5.625, 'pattern_counts': {'":': 0, '<': 45, '<?xml version=': 0, '>': 45, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 33, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '675769', 'n_tokens_mistral': 5954, 'n_tokens_neox': 5780, 'n_words': 1076}