All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.notima.businessobjects.adapter.json.JsonPaymentBatchChannelFactory Maven / Gradle / Ivy

Go to download

Adapter to convert JSON data to the common format found on https://github.com/notima/businessobjects

The newest version!
package org.notima.businessobjects.adapter.json;

import java.io.File;
import java.io.IOException;
import java.util.List;

import org.notima.businessobjects.adapter.json.impl.PaymentBatchChannelListImpl;
import org.notima.generic.businessobjects.TaxSubjectIdentifier;
import org.notima.generic.ifacebusinessobjects.PaymentBatchChannel;
import org.notima.generic.ifacebusinessobjects.PaymentBatchChannelFactory;
import org.notima.util.IDGenerator;

public class JsonPaymentBatchChannelFactory implements PaymentBatchChannelFactory {

	private PaymentBatchChannelListImpl channelList;
	
	public JsonPaymentBatchChannelFactory(String pathToJsonFile) throws IOException {
		
		channelList = PaymentBatchChannelListImpl.createFromFile(new File(pathToJsonFile));
		
	}
	
	@Override
	public List listChannelsForTenant(TaxSubjectIdentifier tenant) {
		return channelList.listChannelsForTenant(tenant);
	}

	@Override
	public List listChannelsWithSourceSystem(String systemName) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List listChannelsWithDestinationSystem(String systemName) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public PaymentBatchChannel persistChannel(PaymentBatchChannel pbc) throws IOException {
		// Create new ID
		if (pbc.getChannelId()==null || pbc.getChannelId().trim().length()==0) {
			pbc.setChannelId(IDGenerator.generateID(10));
			while (findChannelWithId(pbc.getChannelId())!=null) {
				pbc.setChannelId(IDGenerator.generateID(10));
			}
			channelList.getChannelList().add(pbc);
		} else {
			PaymentBatchChannel existing = findChannelWithId(pbc.getChannelId());
			if (existing!=null) {
				replace(pbc, existing);
			} else {
				channelList.getChannelList().add(pbc);
			}
		}
		
		// Save to underlying file system.
		channelList.writeToFile();
		
		return pbc;
	}
	
	private void replace(PaymentBatchChannel newPbc, PaymentBatchChannel oldPbc) {
		channelList.getChannelList().remove(oldPbc);
		channelList.getChannelList().add(newPbc);
	}
	
	@Override
	public String getSystemName() {
		return JsonAdapter.SYSTEM_NAME;
	}

	@Override
	public PaymentBatchChannel findChannelWithId(String id) {
		if (id==null || channelList==null) return null;
		
		for (PaymentBatchChannel ch : channelList.getChannelList()) {
			if (id.equals(ch.getChannelId())) {
				return ch;
			}
		}
		
		return null;
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy