Commit 6e8daeeb authored by Matija Obreza's avatar Matija Obreza
Browse files

Service impls

parent 57752f32
/**
* Copyright 2013 Global Crop Diversity Trust
*
* 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.
**/
package org.crophub.rest.common.service.impl;
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.lang.ArrayUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.crophub.rest.common.model.impl.DataSet;
import org.crophub.rest.common.model.impl.Descriptor;
import org.crophub.rest.common.model.impl.SparseData;
import org.crophub.rest.common.model.impl.SparseEntry;
import org.crophub.rest.common.persistence.domain.SparseDataRepository;
import org.crophub.rest.common.persistence.domain.SparseEntryRepository;
import org.crophub.rest.common.service.SparseDataService;
import org.crophub.rest.common.service.SparseStringService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@Service
@Transactional(readOnly = true)
public class SparseDataServiceImpl implements SparseDataService {
public static final Log LOG = LogFactory
.getLog(SparseDataServiceImpl.class);
@Autowired
SparseEntryRepository entryRepository;
@Autowired
SparseDataRepository dataRepository;
@Autowired
SparseStringService stringService;
// FIXME This code does not handle column grouping
@Override
@Transactional(readOnly = false)
public void writeEntries(final DataSet dataSet, final Descriptor[] descriptors,
final List<String[]> datas) {
final List<SparseEntry> sparseEntries = new ArrayList<SparseEntry>(
datas.size());
final List<SparseData> sparseDatas = new ArrayList<SparseData>(
descriptors.length);
for (int j = 0; j < datas.size(); j++) {
// New entry
final SparseEntry sparseEntry = new SparseEntry();
sparseEntry.setDataSet(dataSet);
sparseEntries.add(sparseEntry);
}
// save the lot
entryRepository.save(sparseEntries);
for (int j = 0; j < datas.size(); j++) {
final String[] data = datas.get(j);
final long sparseEntryId = sparseEntries.get(j).getId();
if (data.length < descriptors.length) {
LOG.warn("Funny input: " + ArrayUtils.toString(data));
}
// All traits
int valueCount = 0;
for (int i = 0; i < descriptors.length && i < data.length; i++) {
if (data[i] != null && descriptors[i] != null) {
valueCount++;
final SparseData sparseData = new SparseData();
sparseData.setSparseEntryId(sparseEntryId);
sparseData.setDescriptorId(descriptors[i].getId());
sparseData.setValue(data[i]);
sparseDatas.add(sparseData);
}
}
if (valueCount == 0) {
entryRepository.delete(sparseEntries.remove(j));
}
}
dataRepository.save(sparseDatas);
}
}
/**
* Copyright 2013 Global Crop Diversity Trust
*
* 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.
**/
package org.crophub.rest.common.service.impl;
import org.crophub.rest.common.model.impl.SparseString;
import org.crophub.rest.common.persistence.domain.SparseStringRepository;
import org.crophub.rest.common.service.SparseStringService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@Service
@Transactional(readOnly = true)
public class SparseStringServiceImpl implements SparseStringService {
@Autowired
SparseStringRepository stringRepository;
@Override
@Transactional(readOnly = false)
@Cacheable(value = "strings", key = "#value")
public SparseString getString(final String value) {
if (value == null) {
return null;
}
SparseString sparseString = stringRepository.find(value.hashCode(),
value.length(), value);
if (sparseString == null) {
sparseString = new SparseString(value);
stringRepository.save(sparseString);
}
return sparseString;
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment